QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#379118 | #8570. Idola-Tree | ucup-team087# | AC ✓ | 2049ms | 113592kb | C++20 | 23.0kb | 2024-04-06 16:16:08 | 2024-04-06 16:16:09 |
Judging History
answer
#ifndef LOCAL
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#endif
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define int ll
bool dbg=false;
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
using pi=pair<int,int>;
using vi=vc<int>;
using vvi=vc<vc<int>>;
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
return os<<"{"<<p.a<<","<<p.b<<"}";
}
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
os<<"{";
for(auto e:v)os<<e<<",";
return os<<"}";
}
#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ";
dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif
using uint=unsigned;
using ull=unsigned long long;
template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
return os<<vc<t>(all(a));
}
ll rand_int(ll l, ll r) { //[l, r]
//#ifdef LOCAL
static mt19937_64 gen;
/*#else
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
#endif*/
return uniform_int_distribution<ll>(l, r)(gen);
}
ll rand_int(ll k){ //[0,k)
return rand_int(0,k-1);
}
string rand_string(int n,char lw,char up){
string s(n,'?');
rep(i,n)s[i]=rand_int(lw,up);
return s;
}
int current_run_id,run_batch_size=1000;
int calc_random_limit(){
return current_run_id/run_batch_size+1;
}
template<class t>
void generate_single(t&a){
a=rand_int(1,calc_random_limit());
}
void generate_single(string&a){
int n;generate_single(n);
a=rand_string(n,'a','b');
}
template<class t,class u>
void generate_single(pair<t,u>&a){
generate_single(a.a);
generate_single(a.b);
}
//https://trap.jp/post/1224/
template<class... Args>
void input(Args&... a){
if(dbg){
(generate_single(a),...);
}else{
(cin >> ... >> a);
}
}
#define INT(...) int __VA_ARGS__;input(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;input(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__;input(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;input(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;input(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;input(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;input(__VA_ARGS__)
#define overload3(a,b,c,d,...) d
#define VI2(name,size) vi name(size);rep(i_##name,size)input(name[i_##name]);
#define VI3(name,size,offset) vi name(size);rep(i_##name,size)input(name[i_##name]),name[i_##name]+=offset;
#define VI(...) overload3(__VA_ARGS__,VI3,VI2)(__VA_ARGS__)
#define VPI(name,size) vc<pi> name(size);rep(i_##name,size)input(name[i_##name]);
#define VVI(name,sizeN,sizeM) vvi name(sizeN,vi(sizeM));\
rep(i_##name,sizeN)rep(j_##name,sizeM)input(name[i_##name][j_##name]);
#define VVT(type,name,sizeN,sizeM) vvc<type> name(sizeN,vc<type>(sizeM));
template<int i,class T>
void print_tuple(ostream&,const T&){
}
template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
if(i)os<<",";
os<<get<i>(t);
print_tuple<i+1,T,Args...>(os,t);
}
template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
os<<"{";
print_tuple<0,tuple<Args...>,Args...>(os,t);
return os<<"}";
}
ll read(){
ll i;
cin>>i;
return i;
}
vi readvi(int n,int off=0){
vi v(n);
rep(i,n)v[i]=read()+off;
return v;
}
pi readpi(int off=0){
int a,b;cin>>a>>b;
return pi(a+off,b+off);
}
template<class t>
void print_single(t x,int suc=1){
cout<<x;
if(suc==1){
if(dbg)cout<<endl;
else cout<<"\n";
}
if(suc==2)
cout<<" ";
}
template<class t,class u>
void print_single(const pair<t,u>&p,int suc=1){
print_single(p.a,2);
print_single(p.b,suc);
}
template<class T>
void print_single(const vector<T>&v,int suc=1){
rep(i,v.size())
print_single(v[i],i==int(v.size())-1?suc:2);
}
template<class T>
void print_offset(const vector<T>&v,ll off,int suc=1){
rep(i,v.size())
print_single(v[i]+off,i==int(v.size())-1?suc:2);
}
template<class T,size_t N>
void print_single(const array<T,N>&v,int suc=1){
rep(i,N)
print_single(v[i],i==int(N)-1?suc:2);
}
template<class T>
void print(const T&t){
print_single(t);
}
template<class T,class ...Args>
void print(const T&t,const Args&...args){
print_single(t,2);
print(args...);
}
template<class T>
void printvv(const vvc<T>&vs){
for(const auto&row:vs)print(row);
}
string readString(){
string s;
cin>>s;
return s;
}
template<class T>
T sq(const T& t){
return t*t;
}
void YES(bool ex=true){
cout<<"YES\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void NO(bool ex=true){
cout<<"NO\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void Yes(bool ex=true){
cout<<"Yes\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void No(bool ex=true){
cout<<"No\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
//#define CAPITAL
/*
void yes(bool ex=true){
#ifdef CAPITAL
cout<<"YES"<<"\n";
#else
cout<<"Yes"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void no(bool ex=true){
#ifdef CAPITAL
cout<<"NO"<<"\n";
#else
cout<<"No"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}*/
void possible(bool ex=true){
#ifdef CAPITAL
cout<<"POSSIBLE"<<"\n";
#else
cout<<"Possible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void impossible(bool ex=true){
#ifdef CAPITAL
cout<<"IMPOSSIBLE"<<"\n";
#else
cout<<"Impossible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
constexpr ll ten(int n){
return n==0?1:ten(n-1)*10;
}
const ll infLL=LLONG_MAX/3;
#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif
int topbit(signed t){
return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
return t==0?-1:63-__builtin_clzll(t);
}
int topbit(ull t){
return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
return a==0?64:__builtin_ctzll(a);
}
int botbit(ull a){
return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
return __builtin_popcount(t);
}
int popcount(ll t){
return __builtin_popcountll(t);
}
int popcount(ull t){
return __builtin_popcountll(t);
}
int bitparity(ll t){
return __builtin_parityll(t);
}
bool ispow2(int i){
return i&&(i&-i)==i;
}
ll mask(int i){
return (ll(1)<<i)-1;
}
ull umask(int i){
return (ull(1)<<i)-1;
}
ll minp2(ll n){
if(n<=1)return 1;
else return ll(1)<<(topbit(n-1)+1);
}
bool inc(int a,int b,int c){
return a<=b&&b<=c;
}
template<class S> void mkuni(S&v){
sort(all(v));
v.erase(unique(all(v)),v.ed);
}
template<class t> bool isuni(vc<t> v){
int s=si(v);
mkuni(v);
return si(v)==s;
}
template<class t>
void myshuffle(vc<t>&a){
rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
}
template<class S,class u>
int lwb(const S&v,const u&a){
return lower_bound(all(v),a)-v.bg;
}
template<class t,class u>
bool bis(const vc<t>&v,const u&a){
return binary_search(all(v),a);
}
//VERIFY: yosupo
//KUPC2017J
//AOJDSL1A
//without rank
struct unionfind{
vi p,s;
int c;
unionfind(int n):p(n,-1),s(n,1),c(n){}
void clear(){
fill(all(p),-1);
fill(all(s),1);
c=si(p);
}
int find(int a){
return p[a]==-1?a:(p[a]=find(p[a]));
}
//set b to a child of a
bool unite(int a,int b){
a=find(a);
b=find(b);
if(a==b)return false;
p[b]=a;
s[a]+=s[b];
c--;
return true;
}
bool same(int a,int b){
return find(a)==find(b);
}
int sz(int a){
return s[find(a)];
}
};
vvc<int> readGraph(int n,int m){
vvc<int> g(n);
rep(i,m){
int a,b;
cin>>a>>b;
//sc.read(a,b);
a--;b--;
g[a].pb(b);
g[b].pb(a);
}
return g;
}
vvc<int> readTree(int n){
if(dbg){
vvc<int> t(n);
unionfind uf(n);
while(uf.c>1){
int a=rand_int(n);
int b=rand_int(n);
if(uf.unite(a,b)){
t[a].pb(b);
t[b].pb(a);
}
}
return t;
}else{
return readGraph(n,n-1);
}
}
void printTree(const vvc<int> t){
int n=si(t);
int degsum=0;
rep(i,n)degsum+=si(t[i]);
if(degsum==n-1){
//directed
rep(i,si(t))for(auto j:t[i]){
print(i+1,j+1);
}
}else if(degsum==2*(n-1)){
//undirected
rep(i,si(t))for(auto j:t[i])if(i<j){
print(i+1,j+1);
}
}else{
assert(false);
}
}
template<class t>
vc<t> presum(const vc<t>&a){
vc<t> s(si(a)+1);
rep(i,si(a))s[i+1]=s[i]+a[i];
return s;
}
vc<ll> presum(const vi&a){
vc<ll> s(si(a)+1);
rep(i,si(a))s[i+1]=s[i]+a[i];
return s;
}
//BIT で数列を管理するときに使う (CF850C)
template<class t>
vc<t> predif(vc<t> a){
gnr(i,1,si(a))a[i]-=a[i-1];
return a;
}
template<class t>
vvc<ll> imos(const vvc<t>&a){
int n=si(a),m=si(a[0]);
vvc<ll> b(n+1,vc<ll>(m+1));
rep(i,n)rep(j,m)
b[i+1][j+1]=b[i+1][j]+b[i][j+1]-b[i][j]+a[i][j];
return b;
}
//verify してないや
void transvvc(int&n,int&m){
swap(n,m);
}
template<class t,class... Args>
void transvvc(int&n,int&m,vvc<t>&a,Args&...args){
assert(si(a)==n);
vvc<t> b(m,vi(n));
rep(i,n){
assert(si(a[i])==m);
rep(j,m)b[j][i]=a[i][j];
}
a.swap(b);
transvvc(n,m,args...);
}
//CF854E
void rotvvc(int&n,int&m){
swap(n,m);
}
template<class t,class... Args>
void rotvvc(int&n,int&m,vvc<t>&a,Args&...args){
assert(si(a)==n);
vvc<t> b(m,vi(n));
rep(i,n){
assert(si(a[i])==m);
rep(j,m)b[m-1-j][i]=a[i][j];
}
a.swap(b);
rotvvc(n,m,args...);
}
//ソートして i 番目が idx[i]
//CF850C
template<class t>
vi sortidx(const vc<t>&a){
int n=si(a);
vi idx(n);iota(all(idx),0);
sort(all(idx),[&](int i,int j){return a[i]<a[j];});
return idx;
}
//vs[i]=a[idx[i]]
//例えば sortidx で得た idx を使えば単にソート列になって返ってくる
//CF850C
template<class t>
vc<t> a_idx(const vc<t>&a,const vi&idx){
int n=si(a);
assert(si(idx)==n);
vc<t> vs(n);
rep(i,n)vs[i]=a[idx[i]];
return vs;
}
//CF850C
vi invperm(const vi&p){
int n=si(p);
vi q(n);
rep(i,n)q[p[i]]=i;
return q;
}
template<class t,class s=t>
s SUM(const vc<t>&a){
return accumulate(all(a),s(0));
}
template<class t,size_t K,class s=t>
s SUM(const array<t,K>&a){
return accumulate(all(a),s(0));
}
template<class t>
t MAX(const vc<t>&a){
return *max_element(all(a));
}
template<class t>
pair<t,int> MAXi(const vc<t>&a){
auto itr=max_element(all(a));
return mp(*itr,itr-a.bg);
}
template<class A>
auto MIN(const A&a){
return *min_element(all(a));
}
template<class t>
pair<t,int> MINi(const vc<t>&a){
auto itr=min_element(all(a));
return mp(*itr,itr-a.bg);
}
vi vid(int n){
vi res(n);iota(all(res),0);
return res;
}
template<class S>
void soin(S&s){
sort(all(s));
}
template<class S,class F>
void soin(S&s,F&&f){
sort(all(s),forward<F>(f));
}
template<class S>
S soout(S s){
soin(s);
return s;
}
template<class S>
void rein(S&s){
reverse(all(s));
}
template<class S>
S reout(S s){
rein(s);
return s;
}
template<class t,class u>
pair<t,u>&operator+=(pair<t,u>&a,pair<t,u> b){
a.a+=b.a;a.b+=b.b;return a;}
template<class t,class u>
pair<t,u>&operator-=(pair<t,u>&a,pair<t,u> b){
a.a-=b.a;a.b-=b.b;return a;}
template<class t,class u>
pair<t,u> operator+(pair<t,u> a,pair<t,u> b){return mp(a.a+b.a,a.b+b.b);}
template<class t,class u>
pair<t,u> operator-(pair<t,u> a,pair<t,u> b){return mp(a.a-b.a,a.b-b.b);}
template<class t,class u,class v>
pair<t,u>&operator*=(pair<t,u>&a,v b){
a.a*=b;a.b*=b;return a;}
template<class t,class u,class v>
pair<t,u> operator*(pair<t,u> a,v b){return a*=b;}
template<class t,class u>
pair<t,u> operator-(pair<t,u> a){return mp(-a.a,-a.b);}
namespace std{
template<class t,class u>
istream&operator>>(istream&is,pair<t,u>&a){
return is>>a.a>>a.b;
}
}
template<class t>
t gpp(vc<t>&vs){
assert(si(vs));
t res=move(vs.back());
vs.pop_back();
return res;
}
template<class t,class u>
void pb(vc<t>&a,const vc<u>&b){
a.insert(a.ed,all(b));
}
template<class t,class...Args>
vc<t> cat(vc<t> a,Args&&...b){
(pb(a,forward<Args>(b)),...);
return a;
}
template<class t,class u>
vc<t>& operator+=(vc<t>&a,u x){
for(auto&v:a)v+=x;
return a;
}
template<class t,class u>
vc<t> operator+(vc<t> a,u x){
return a+=x;
}
template<class t>
vc<t> operator+(const vc<t>&a,const vc<t>&b){
vc<t> c(max(si(a),si(b)));
rep(i,si(a))c[i]+=a[i];
rep(i,si(b))c[i]+=b[i];
return c;
}
template<class t,class u>
vc<t>& operator-=(vc<t>&a,u x){
for(auto&v:a)v-=x;
return a;
}
template<class t,class u>
vc<t>& operator-(vc<t> a,u x){
return a-=x;
}
template<class t,class u>
vc<t>& operator*=(vc<t>&a,u x){
for(auto&v:a)v*=x;
return a;
}
template<class t,class u>
vc<t>& operator*(vc<t> a,u x){
return a*=x;
}
template<class t,class u>
void remval(vc<t>&a,const u&v){
a.erase(remove(all(a),v),a.ed);
}
//消した要素の個数を返してくれる
//UCUP 2-8-F
template<class t,class F>
int remif(vc<t>&a,F f){
auto itr=remove_if(all(a),f);
int res=a.ed-itr;
a.erase(itr,a.ed);
return res;
}
template<class VS,class u>
void fila(VS&vs,const u&a){
fill(all(vs),a);
}
template<class t,class u>
int findid(const vc<t>&vs,const u&a){
auto itr=find(all(vs),a);
if(itr==vs.ed)return -1;
else return itr-vs.bg;
}
template<class t>
void rtt(vc<t>&vs,int i){
rotate(vs.bg,vs.bg+i,vs.ed);
}
//Multiuni2023-8 C
//f(lw)=false,...,f(n-1)=false,f(n)=true,...,f(up)=true,
//のときに n を返す
template<class F>
int find_min_true(int lw,int up,F f){
while(up-lw>1){
const int mid=(lw+up)/2;
if(f(mid))up=mid;
else lw=mid;
}
return up;
}
//f(lw)=true,f(up)=false
template<class F>
int find_max_true(int lw,int up,F f){
while(up-lw>1){
const int mid=(lw+up)/2;
if(f(mid))lw=mid;
else up=mid;
}
return lw;
}
template<class t> using pqmin=priority_queue<t,vc<t>,greater<t>>;
template<class t> using pqmax=priority_queue<t>;
using T=tuple<int,int,int>;
//mint107 は verify してねえ
//#define DYNAMIC_MOD
struct modinfo{uint mod,root;
#ifdef DYNAMIC_MOD
constexpr modinfo(uint m,uint r):mod(m),root(r),im(0){set_mod(m);}
ull im;
constexpr void set_mod(uint m){
mod=m;
im=ull(-1)/m+1;
}
uint product(uint a,uint b)const{
ull z=ull(a)*b;
uint x=((unsigned __int128)z*im)>>64;
uint v=uint(z)-x*mod;
return v<mod?v:v+mod;
}
#endif
};
template<modinfo const&ref>
struct modular{
static constexpr uint const &mod=ref.mod;
static modular root(){return modular(ref.root);}
uint v;
//modular(initializer_list<uint>ls):v(*ls.bg){}
modular(ll vv=0){s(vv%mod+mod);}
modular& s(uint vv){
v=vv<mod?vv:vv-mod;
return *this;
}
modular operator-()const{return modular()-*this;}
modular& operator+=(const modular&rhs){return s(v+rhs.v);}
modular&operator-=(const modular&rhs){return s(v+mod-rhs.v);}
modular&operator*=(const modular&rhs){
#ifndef DYNAMIC_MOD
v=ull(v)*rhs.v%mod;
#else
v=ref.product(v,rhs.v);
#endif
return *this;
}
modular&operator/=(const modular&rhs){return *this*=rhs.inv();}
modular operator+(const modular&rhs)const{return modular(*this)+=rhs;}
modular operator-(const modular&rhs)const{return modular(*this)-=rhs;}
modular operator*(const modular&rhs)const{return modular(*this)*=rhs;}
modular operator/(const modular&rhs)const{return modular(*this)/=rhs;}
modular pow(ll n)const{
if(n<0)return inv().pow(-n);
modular res(1),x(*this);
while(n){
if(n&1)res*=x;
x*=x;
n>>=1;
}
return res;
}
modular inv()const{return pow(mod-2);}
/*modular inv()const{
int x,y;
int g=extgcd<ll>(v,mod,x,y);
assert(g==1);
if(x<0)x+=mod;
return modular(x);
}*/
friend modular operator+(ll x,const modular&y){
return modular(x)+y;
}
friend modular operator-(ll x,const modular&y){
return modular(x)-y;
}
friend modular operator*(ll x,const modular&y){
return modular(x)*y;
}
friend modular operator/(ll x,const modular&y){
return modular(x)/y;
}
friend ostream& operator<<(ostream&os,const modular&m){
return os<<m.v;
}
friend istream& operator>>(istream&is,modular&m){
ll x;is>>x;
m=modular(x);
return is;
}
bool operator<(const modular&r)const{return v<r.v;}
bool operator==(const modular&r)const{return v==r.v;}
bool operator!=(const modular&r)const{return v!=r.v;}
explicit operator bool()const{
return v;
}
};
#ifndef DYNAMIC_MOD
extern constexpr modinfo base{998244353,3};
//extern constexpr modinfo base{1000000007,0};
//extern constexpr modinfo base{2147483579,1689685080};//2^31 未満の最大の安全素数
//modinfo base{1,0};
#ifdef USE_GOOD_MOD
static_assert(base.mod==998244353);
#endif
#else
modinfo base(1,0);
extern constexpr modinfo base107(1000000007,0);
using mint107=modular<base107>;
#endif
using mint=modular<base>;
mint parity(int i){
return i%2==0?1:-1;
}
#ifdef LOCAL
const int vmax=10010;
#else
const int vmax=(1<<21)+10;
#endif
mint fact[vmax],finv[vmax],invs[vmax];
void initfact(){
fact[0]=1;
rng(i,1,vmax){
fact[i]=fact[i-1]*i;
}
finv[vmax-1]=fact[vmax-1].inv();
for(int i=vmax-2;i>=0;i--){
finv[i]=finv[i+1]*(i+1);
}
for(int i=vmax-1;i>=1;i--){
invs[i]=finv[i]*fact[i-1];
}
}
mint choose(int n,int k){
return inc(0,k,n)?fact[n]*finv[n-k]*finv[k]:0;
}
mint binom(int a,int b){
return 0<=a&&0<=b?fact[a+b]*finv[a]*finv[b]:0;
}
mint catalan(int n){
return binom(n,n)-(n-1>=0?binom(n-1,n+1):0);
}
//対角線を超えず (x,y) に至る方法の数
mint catalan(int x,int y){
assert(y<=x);
return binom(x,y)-binom(x+1,y-1);
}
//y=x+c を超えず (x,y) に至る方法の数
mint catalan(int x,int y,int c){
assert(y<=x+c);
return binom(x,y)-binom(x+c+1,y-c-1);
}
/*
const int vmax=610;
mint fact[vmax+1],binbuf[vmax+1][vmax+1];
mint choose(int n,int k){
return 0<=k&&k<=n?binbuf[n-k][k]:0;
}
mint binom(int a,int b){
return 0<=a&&0<=b?binbuf[a][b]:0;
}
void initfact(int n){
fact[0]=1;
rep(i,n)fact[i+1]=fact[i]*(i+1);
rep(i,n+1)rep(j,n+1){
if(i==0&&j==0){
binbuf[i][j]=1;
}else{
binbuf[i][j]=0;
if(i)binbuf[i][j]+=binbuf[i-1][j];
if(j)binbuf[i][j]+=binbuf[i][j-1];
}
}
}
*/
mint p2[vmax],p2inv[vmax];
void initp2(){
p2[0]=1;
rep(i,vmax-1)p2[i+1]=p2[i]*2;
p2inv[vmax-1]=p2[vmax-1].inv();
per(i,vmax-1)p2inv[i]=p2inv[i+1]*2;
}
//有向木じゃダメ!!
//AGC034E
//XX Opencup GP of Korea
//CF446D
//int(E)->to
//par[0] が -1 でないことに注意してほしい
//sub[v] -> vの部分木情報
//bus[v] -> p を根にした部分木 (v) 含まずの情報
//Universal Cup 2023 Stage 1
//頂点や辺の情報を重複して入れないようにした
//数え上げなどに使える
//CF902E
//辺の向きに意味がある問題でverify
template<class E,class N>
struct treedp{
const vvc<E>& g;
vc<N> a,b,res,sub,bus;
vc<E> par;
void dfs1(int v,int p){
a[v].init(v);
for(auto e:g[v])
if(e!=p){
dfs1(e,v);
b[e]=a[v];
a[v]=a[v]+a[e];
}else{
par[v]=e;
}
sub[v]=a[v];
if(p!=-1)a[v]=a[v].up(par[v]);
}
void dfs2(int v,int p,N cur){
bool f=p==-1;
per(i,g[v].size()){
auto e=g[v][i];
if(e==p)continue;
if(f){
bus[e]=b[e];
cur=a[e];
f=false;
}else{
bus[e]=cur+b[e];
cur=cur+a[e];
}
dfs2(e,v,bus[e].up(e));
}
N tmp;tmp.init(v);
if(f)res[v]=tmp;
else res[v]=cur+tmp;
}
treedp(const vvc<E>&gg):g(gg),a(si(g)),b(si(g)),res(si(g)),sub(si(g)),bus(si(g)),par(si(g)){
{//make sure the graph is undirected
int sum=0;
rep(i,si(gg))sum+=si(g[i]);
assert(sum==2*(si(gg)-1));
}
dfs1(0,-1);
dfs2(0,-1,N());
}
N& getval(int v,int p){
if(par[v]==p)return sub[v];
else return bus[p];
}
};
struct N{
int vs,sum;
void init(int){
vs=1;
sum=0;
}
N up(int)const{
return N{vs,sum+vs};
}
N operator+(const N&r)const{
return N{vs+r.vs,sum+r.sum};
}
};
vi arithmetic_sort_brute(vi a,int d,int k){
pqmin<int> pq;
for(auto v:a)pq.push(v);
vi res;
rep(i,k){
int v=pq.top();pq.pop();
res.pb(v);
pq.push(v+d);
}
return res;
}
vi arithmetic_sort(vi a,int d,int k){
int n=si(a);
soin(a);
int head=1;
vi to=vid(n);
int cur=0;
vi res(k);
rep(i,k){
res[i]=a[cur];
a[cur]+=d;
int nx=to[cur];
if(head<n&&a[nx]>a[head]){
to[cur]=head;
to[head]=nx;
head++;
}
cur=to[cur];
}
return res;
}
template<class F>
void arithmetic_sort_do(vi a,int d,int k,F f){
int n=si(a);
soin(a);
int head=1;
vi to=vid(n);
int cur=0;
rep(i,k){
f(a[cur]);
a[cur]+=d;
int nx=to[cur];
if(head<n&&a[nx]>a[head]){
to[cur]=head;
to[head]=nx;
head++;
}
cur=to[cur];
}
}
void slv(){
INT(n,C);
auto t=readTree(n);
treedp<int,N> dp(t);
mint cur=0;
rng(i,1,n){
int v=i,p=dp.par[i];
N x=dp.getval(v,p);
N y=dp.getval(p,v);
cur+=x.sum*y.vs;
cur+=y.sum*x.vs;
cur+=x.vs*y.vs;
}
C-=(n-1);
C++;
vi w;
if(n==2){
w.pb(2);
}else{
rep(i,n)if(si(t[i])==1){
w.pb(dp.res[i].sum*2);
}
}
int m=si(w);
vi d(m);
//pqmin<int> pq;
rep(i,m){
d[i]=w[i]+(n-2);
//pq.push(w[i]+(n-2));
}
mint ans=0;
/*vi z=arithmetic_sort(d,2*(n-2),C);
rep(c,C){
mint val=cur+c*c;
ans+=val*val*val;
cur+=z[c];
}*/
int c=0;
arithmetic_sort_do(d,2*(n-2),C,[&](int dif){
mint val=cur+c*c;
dmp(val);
ans+=val*val*val;
cur+=dif;
c++;
});
print(ans);
}
signed main(signed argc,char*argv[]){
if(argc>1&&strcmp(argv[1],"D")==0)dbg=true;
cin.tie(0);
ios::sync_with_stdio(0);
cout<<fixed<<setprecision(20);
if(0){
const int V=10;
rng(n,1,10){
rng(k,1,100){
rep(itr,100){
vi a(n);
rep(i,n)a[i]=rand_int(V);
int d=rand_int(1,V);
vi ans=arithmetic_sort(a,d,k);
vi god=arithmetic_sort_brute(a,d,k);
if(ans!=god){
cerr<<n<<" "<<d<<endl;
cerr<<a<<endl;
cerr<<ans<<endl;
cerr<<god<<endl;
}
assert(ans==god);
}
}
}
}
if(dbg){
while(1){
if(current_run_id%run_batch_size==0){
cerr<<"Current Run "<<current_run_id<<endl;
}
slv();
current_run_id++;
}
}else{
int t;cin>>t;rep(_,t)
slv();
}
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 21ms
memory: 44592kb
input:
2 4 3 1 4 1 3 2 1 4 4 1 4 1 3 2 1
output:
3375 25327
result:
ok 2 tokens
Test #2:
score: 0
Accepted
time: 17ms
memory: 44548kb
input:
4 4 3 1 4 1 3 2 1 4 4 1 4 1 3 2 1 5 4 1 4 1 3 1 2 5 4 5 5 1 4 1 3 1 2 5 4
output:
3375 25327 54872 249984
result:
ok 4 tokens
Test #3:
score: 0
Accepted
time: 1672ms
memory: 103424kb
input:
4 300000 50000000 216838 200677 44849 12926 125086 157230 26195 29767 241694 21336 21336 24457 105861 84565 184655 45583 175336 97945 286044 30927 295273 249694 109469 1566 193560 251229 176229 288707 206166 13532 166218 264413 299977 95587 159105 48116 57912 82606 97296 193689 115029 121192 68068 1...
output:
968050473 546482457 9595680 694101802
result:
ok 4 tokens
Test #4:
score: 0
Accepted
time: 1791ms
memory: 104304kb
input:
4 300000 49999999 260729 131757 51432 46938 257303 178692 218606 108144 299084 259822 82091 231405 101255 218808 287507 101249 29597 151244 43164 198032 122336 162072 177969 62812 277824 35758 182087 258797 235155 33806 188695 76915 289006 141702 39100 183183 224949 156588 9827 173233 27349 286822 1...
output:
283884918 837489229 12901428 323340145
result:
ok 4 tokens
Test #5:
score: 0
Accepted
time: 1861ms
memory: 106516kb
input:
4 300000 50000000 187086 121551 163664 292500 40569 125891 280767 56246 127162 299705 207527 280767 252551 217178 73197 278560 274282 31937 121290 280767 220367 278560 187814 40569 187372 278560 95157 131908 119390 161684 202404 283778 226289 130192 294021 278560 50286 102006 21592 222623 285215 237...
output:
4850582 364539310 683543335 559022036
result:
ok 4 tokens
Test #6:
score: 0
Accepted
time: 1849ms
memory: 101088kb
input:
4 300000 50000000 225743 104304 178831 182636 22896 17048 96503 294029 294029 28084 38933 104195 294029 1583 224079 175121 8797 197089 81985 139893 184175 103991 39351 217336 127576 82268 294029 292994 145502 294859 63119 104069 294029 41437 236951 199792 157992 294029 249477 284128 136077 294029 65...
output:
536508840 693675397 413103274 759128961
result:
ok 4 tokens
Test #7:
score: 0
Accepted
time: 2049ms
memory: 102872kb
input:
4 300000 50000000 246788 228391 158979 271301 96237 233512 276470 143087 132635 100991 189228 201152 1506 10986 75594 100408 279681 127708 140194 83425 50807 272520 277215 107214 249687 77890 261893 11907 109314 121422 76821 170561 11602 233066 80094 28275 27473 70572 130573 16191 13008 289605 25353...
output:
229607225 752154895 217060859 960988279
result:
ok 4 tokens
Test #8:
score: 0
Accepted
time: 1979ms
memory: 99152kb
input:
4 300000 50000000 282645 78888 157049 242271 143611 216821 287822 256908 2275 263546 49651 72865 31980 207555 107608 204451 138876 149271 175134 283496 8765 266276 288773 161294 250433 198847 292442 23211 93376 281917 221760 81331 133157 239663 276759 27628 115322 150583 89351 283086 291870 12125 68...
output:
62551856 430534707 675000909 391405102
result:
ok 4 tokens
Test #9:
score: 0
Accepted
time: 1999ms
memory: 113592kb
input:
4 300000 50000000 294569 56297 172540 287752 61940 152205 197674 254245 24085 113380 82637 123004 47497 92473 32184 178733 277456 157565 21776 156798 137130 134095 110025 202055 174662 297197 60043 118646 4537 248467 273141 53510 238177 252865 234966 233515 289687 229746 298433 191752 120484 36179 2...
output:
195781417 673490465 850215681 815198198
result:
ok 4 tokens
Test #10:
score: 0
Accepted
time: 1243ms
memory: 99776kb
input:
4 36778 50000000 17602 27103 25759 23876 24338 4623 29585 32937 25324 18644 2950 25005 25234 8990 10680 32086 9568 25870 23421 16592 1809 18727 29491 17171 22903 27836 4496 20939 25152 3804 12390 16492 3484 31766 6824 25795 1411 28354 3077 17532 6033 36029 11689 15579 20720 35844 5484 2622 15596 536...
output:
135800108 231398541 778024906 624480729
result:
ok 4 tokens
Test #11:
score: 0
Accepted
time: 1213ms
memory: 93236kb
input:
4 300000 29286167 155087 68009 83184 149687 206954 8699 86662 141944 237475 242716 124487 225583 168790 57088 207434 196893 573 4579 71930 257825 193317 77567 143825 182638 294310 270719 180399 209962 32628 203666 250650 234364 254067 255639 14682 227300 84292 38937 95079 54215 132911 56983 286874 1...
output:
741171004 852827875 22231673 170720066
result:
ok 4 tokens
Test #12:
score: 0
Accepted
time: 983ms
memory: 93248kb
input:
4 300000 300000 175617 119821 181516 243657 160218 215766 162419 187680 293075 168627 290423 228281 274959 98317 107502 76378 79894 239145 45063 32200 69024 209908 1914 38016 94743 179968 32123 245964 295205 76517 97609 38830 189696 241669 137230 69860 66658 181410 70572 238631 156044 108622 108815 ...
output:
791655481 435035749 574582056 506992226
result:
ok 4 tokens
Test #13:
score: 0
Accepted
time: 1035ms
memory: 44772kb
input:
4 6 47918926 4 3 6 1 1 5 5 4 5 2 6 49261475 6 4 5 6 5 3 4 2 6 1 6 47347415 3 6 6 4 2 6 6 5 1 5 6 46149726 1 2 5 3 5 4 2 4 2 6
output:
593706496 305830436 556194176 708898110
result:
ok 4 tokens
Test #14:
score: 0
Accepted
time: 1003ms
memory: 44588kb
input:
4 6 49429002 2 1 2 4 2 6 6 5 3 6 7 45760900 5 7 7 2 6 2 7 1 3 2 4 7 6 47061835 6 5 6 3 6 1 2 6 6 4 4 47410821 1 4 1 3 3 2
output:
536172407 533054157 561317786 749859281
result:
ok 4 tokens
Test #15:
score: 0
Accepted
time: 1047ms
memory: 44820kb
input:
4 6 49726215 1 6 2 6 6 3 6 5 2 4 2 50000000 1 2 8 49331348 7 3 7 8 5 6 4 3 6 3 2 4 1 7 5 45313556 2 3 4 3 3 5 5 1
output:
429791300 656547393 307298104 165844147
result:
ok 4 tokens
Test #16:
score: 0
Accepted
time: 746ms
memory: 44788kb
input:
4 2 1 2 1 6 49045599 4 2 6 1 6 2 2 3 6 5 7 47403164 5 4 4 7 6 4 6 1 3 1 2 1 5 45061409 2 4 2 1 5 4 3 4
output:
1 403846093 637856203 454170631
result:
ok 4 tokens
Test #17:
score: 0
Accepted
time: 1025ms
memory: 44592kb
input:
4 4 48454794 2 4 4 1 3 2 5 48326937 5 1 5 4 3 2 3 1 5 48676454 3 4 4 1 5 1 1 2 6 48862565 6 2 4 2 5 2 2 1 2 3
output:
346937787 946110101 893634681 642310081
result:
ok 4 tokens
Test #18:
score: 0
Accepted
time: 996ms
memory: 44552kb
input:
4 5 46095989 3 1 4 1 1 2 1 5 5 46073375 4 2 5 4 3 4 1 4 5 48531505 1 5 4 5 2 5 5 3 6 47558014 2 3 6 4 3 5 4 1 5 6
output:
128951803 883389586 226899121 189625473
result:
ok 4 tokens
Test #19:
score: 0
Accepted
time: 1013ms
memory: 44636kb
input:
4 4 45855350 1 4 2 1 4 3 3 49740257 1 2 3 2 4 46480829 1 2 3 1 4 2 4 49937459 1 2 4 2 4 3
output:
605823624 478005949 142328825 551484866
result:
ok 4 tokens
Test #20:
score: 0
Accepted
time: 1025ms
memory: 44572kb
input:
4 4 48451769 4 3 1 3 2 4 6 48177189 6 2 4 3 1 2 3 5 6 3 5 48577030 4 5 2 4 1 5 3 1 5 49630910 3 4 1 5 4 2 4 1
output:
280695405 350730179 81027477 157985696
result:
ok 4 tokens
Test #21:
score: 0
Accepted
time: 1014ms
memory: 44788kb
input:
4 8 49687604 2 8 7 4 3 1 5 6 8 3 7 6 5 1 3 45328134 1 2 1 3 6 47773101 1 4 3 2 6 4 2 4 5 4 6 46139567 2 6 2 3 6 4 2 5 1 2
output:
851791018 997976249 56174545 995045254
result:
ok 4 tokens
Test #22:
score: 0
Accepted
time: 1055ms
memory: 44596kb
input:
4 5 49796273 2 5 1 3 5 1 3 4 5 46614868 3 4 5 4 2 4 1 4 4 49876006 3 4 2 1 3 2 5 49208056 2 1 4 2 2 5 1 3
output:
844326175 597753235 196308105 418510131
result:
ok 4 tokens
Test #23:
score: 0
Accepted
time: 1012ms
memory: 44788kb
input:
4 4 45326185 3 1 2 3 3 4 3 50000000 2 3 1 3 8 48919728 6 1 4 8 4 3 7 8 2 1 8 1 1 5 4 45882405 4 1 1 3 2 4
output:
830463674 893434183 29400274 505479262
result:
ok 4 tokens
Test #24:
score: 0
Accepted
time: 1026ms
memory: 44548kb
input:
4 4 45981477 4 1 1 3 1 2 4 50000000 2 3 3 4 1 4 6 49644522 5 4 1 2 6 3 1 6 3 5 4 46917732 2 4 3 2 2 1
output:
441930004 891846827 12660622 308147414
result:
ok 4 tokens
Test #25:
score: 0
Accepted
time: 1004ms
memory: 44584kb
input:
4 5 46558871 2 4 3 1 3 5 5 4 7 48040830 6 7 1 2 5 2 7 3 7 4 7 1 5 48622955 4 3 1 4 4 5 1 2 8 48009463 3 5 3 4 1 8 7 4 1 4 6 3 4 2
output:
819848891 31449847 684770567 371025692
result:
ok 4 tokens
Test #26:
score: 0
Accepted
time: 1004ms
memory: 44584kb
input:
4 6 48976172 6 4 6 1 2 5 1 5 2 3 5 47341555 1 5 4 1 2 3 3 4 5 47504872 5 1 5 3 2 4 2 5 5 45523046 4 1 4 5 3 4 5 2
output:
62444387 286055466 933929990 389184808
result:
ok 4 tokens
Test #27:
score: 0
Accepted
time: 763ms
memory: 44544kb
input:
4 6 47135616 3 5 1 6 1 4 1 2 3 2 4 48171807 3 1 3 2 2 4 7 46881593 5 3 4 1 1 3 7 5 5 2 1 6 2 2 1 2
output:
647945243 159077979 837172093 65
result:
ok 4 tokens
Test #28:
score: 0
Accepted
time: 1003ms
memory: 44600kb
input:
4 6 45340510 6 1 2 4 5 1 3 4 4 5 5 49475059 5 2 4 1 1 3 4 2 4 45780627 4 3 3 1 1 2 4 49033871 4 1 4 3 2 4
output:
287357295 935929856 762506483 789460951
result:
ok 4 tokens
Test #29:
score: 0
Accepted
time: 1032ms
memory: 44604kb
input:
4 6 47361583 2 6 5 1 5 3 5 6 4 2 6 49346013 5 1 1 4 6 1 5 2 6 3 85 50000000 51 44 13 20 14 9 46 70 5 13 7 34 33 16 23 58 33 30 67 13 3 70 8 33 38 42 7 44 40 58 70 31 23 26 50 27 7 79 28 68 74 44 37 76 23 84 58 62 59 58 12 65 59 57 60 66 81 83 20 45 24 72 29 70 70 1 64 73 63 47 53 23 27 44 74 19 38 6...
output:
557083956 521912976 508811400 923917013
result:
ok 4 tokens
Test #30:
score: 0
Accepted
time: 981ms
memory: 44592kb
input:
4 4 48048505 3 2 4 1 1 2 6 45752957 4 3 6 5 1 2 2 6 6 4 7 45027209 4 1 6 4 3 2 2 1 5 7 6 7 2 45788884 2 1
output:
797762478 105794984 368141529 925907990
result:
ok 4 tokens
Extra Test:
score: 0
Extra Test Passed