QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#404777 | #7607. The Doubling Game 2 | ucup-team3396# | WA | 4ms | 18008kb | C++14 | 2.6kb | 2024-05-04 17:58:34 | 2024-05-04 17:58:35 |
Judging History
answer
#pragma GCC optimize(2,3,"Ofast","inline")
#include <bits/stdc++.h>
#define int unsigned long long
#define double long double
#define lowbit(i) (i&(-i))
#define mid ((l+r)>>1)
#define add(i,j) (((i+j)>=mod)?i+j-mod:i+j)
using namespace std;
int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int mod=1e9+7;
int siz[300005];
vector<int> vc[300005];
int cnt[300005];
int dp[300005][35][3];
int f[2400005][2],g[2400005][2];
int tot=0;
void dfs(int now,int fa){
for(auto v:vc[now]){
if(v==fa) continue;
dfs(v,now);
}
for(auto v:vc[now]) if(v!=fa) cnt[siz[v]]++;
for(int i=20;i>=0;i--){
cnt[i]+=cnt[i+1];
if(i==0) break;
}
for(int i=20;i>=0;i--){
bool ok=1;
for(int j=0;j<i;j++){
if(cnt[j]<i-j) ok=0;
}
if(ok){
siz[now]=i;
break;
}
if(i==0) break;
}
tot+=(1<<(siz[now]+1));
// if(siz[now]>20) exit(2);
// if(tot>=16000000) exit(2);
for(int i=0;i<=20;i++) cnt[i]=0;
for(int i=0;i<(1<<(siz[now]+1));i++) f[i][0]=f[i][1]=0;
f[0][0]=1;
for(auto v:vc[now]){
if(v==fa) continue;
// for(int i=0;i<(1<<(siz[now]+2));i++) g[i][0]=g[i][1]=0;
int tot=0;
for(int j=0;j<=siz[v];j++) (tot+=dp[v][j][0]+dp[v][j][2])%=mod;
for(int j=0;j<=min(siz[v],siz[now]);j++){
for(int i=0;i<(1<<(siz[now]+1));i++){
if(!((i>>j)&1)){
g[i|(1<<j)][0]=add(g[i|(1<<j)][0],f[i][0]*dp[v][j][0]);
if((1<<j)<i) g[i|(1<<j)][1]=add(g[i|(1<<j)][1],f[i][1]*dp[v][j][0]);
if((1<<j)>i) g[i|(1<<j)][1]=add(g[i|(1<<j)][1],f[i][0]*dp[v][j][1]);
}
}
}
for(int i=0;i<(1<<(siz[now]+1));i++) f[i][0]=(f[i][0]*tot+g[i][0])%mod,f[i][1]=(f[i][1]*tot+g[i][1])%mod,g[i][0]=0,g[i][1]=0;
}
for(int i=0;i<=siz[now];i++){
dp[now][i][0]=f[(1<<i)-1][0];
dp[now][i][2]=f[(1<<i)-1+(1<<i)][1];
}
for(int i=0;i<=siz[now]+1;i++){
for(int j=0;j<i;j++){
dp[now][j][1]=add(dp[now][j][1],add(f[(1<<i)-1-(1<<j)][0],f[(1<<i)-1-(1<<j)+(1<<i)][1]));
// if(now==4&&i==1&&j==0) cout<<"OK"<<f[(1<<i)-1-(1<<j)][0]<<" "<<f[(1<<i)-1-(1<<j)+(1<<i)][1]<<" "<<dp[now][j][1]<<" ";
}
}
// cout<<now<<" "<<siz[now]<<"\n";
// for(int i=0;i<=siz[now]+1;i++) cout<<dp[now][i][0]<<" "<<dp[now][i][1]<<" "<<dp[now][i][2]<<"\n";
}
signed main(){
int n=read(); //cin>>n;
for(int i=1;i<n;i++){
int u=read(),v=read(); //cin>>u>>v;
vc[u].push_back(v);
vc[v].push_back(u);
}
dfs(1,0);
int ans=0;
for(int i=0;i<=siz[1];i++) (ans+=dp[1][i][0]+dp[1][i][2])%=mod;
cout<<ans;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 17932kb
input:
5 1 2 1 3 1 4 4 5
output:
21
result:
ok single line: '21'
Test #2:
score: 0
Accepted
time: 4ms
memory: 17808kb
input:
1
output:
1
result:
ok single line: '1'
Test #3:
score: -100
Wrong Answer
time: 2ms
memory: 18008kb
input:
128 11 32 116 81 65 4 117 47 5 81 104 30 61 8 82 59 95 20 92 29 29 127 97 39 123 33 59 128 115 33 83 67 74 16 77 33 64 73 124 123 8 127 61 51 101 122 35 90 119 116 112 27 81 93 109 123 54 1 119 100 116 16 65 47 67 27 22 105 76 87 36 39 27 96 72 31 91 123 21 105 118 12 110 48 121 72 14 115 24 16 106 ...
output:
174933150
result:
wrong answer 1st lines differ - expected: '508800953', found: '174933150'