QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#232089#7607. The Doubling Game 2PhantomThresholdTL 0ms21056kbC++202.6kb2023-10-29 20:52:332023-10-29 20:52:33

Judging History

你现在查看的是最新测评结果

  • [2023-10-29 20:52:33]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:21056kb
  • [2023-10-29 20:52:33]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int maxn = 310000;
const int maxb = 22;
const int mod  = 1e9+7;
inline void add(int &a,const int &b){ a+=b;if(a>=mod)a-=mod; }
inline void sub(int &a,const int &b){ a-=b;if(a<0)a+=mod; }

int n;
vector<int>E[maxn];
int f[maxn][maxb],g[maxn][maxb],mxb[maxn];
int fx[2][maxn],gx[2][maxn];

inline bool cmp(const int x,const int y)
{
	return mxb[x]<mxb[y];
}
void dp(const int x,const int fa)
{
	vector<int>pi;
	for(auto y:E[x]) if(y!=fa)
	{
		dp(y,x);
		pi.push_back(y);
	}
	sort(pi.begin(),pi.end(),cmp);
	
	if(pi.size()>0)
	{
		int now=0,nowsiz= 1<<mxb[pi[0]];
		for(int i=0;i<nowsiz;i++) fx[now][i]=gx[now][i]=0;
		fx[now][0]=1;
		
		for(int sz=pi.size(),i=0;i<sz;i++)
		{
			int y=pi[i];
			
			now=!now;
			for(int j=0;j<(1<<mxb[y]);j++) fx[now][j]=gx[now][j]=0;
			
			for(int j=0;j<nowsiz;j++) if(fx[!now][j]) for(int by=0;by<=mxb[y];by++)
			{
				add(fx[now][j],(ll)fx[!now][j]*f[y][by]%mod);
				if( by>0 && !(j>>(by-1)&1))
					add(fx[now][j|1<<(by-1)],(ll)fx[!now][j]*f[y][by]%mod);
				
				if( by>0 && (1+j) <= (1<<(by-1)) )
					add(gx[now][j|1<<(by-1)],(ll)fx[!now][j]*g[y][by]%mod);
			}
			for(int j=0;j<nowsiz;j++) if(gx[!now][j])
			{
				int k= j==0?-1: 31-__builtin_clz(j);
				for(int by=0;by<=mxb[y];by++)
				{
					add(gx[now][j],(ll)gx[!now][j]*f[y][by]%mod);
					if( by>0 && by-1<k && !(j>>(by-1)&1) )
						add(gx[now][j|1<<(by-1)],(ll)gx[!now][j]*f[y][by]%mod);
				}
			}
			
			nowsiz=1<<mxb[y];
		}
		
		for(int j=0;j<nowsiz;j++)
		{
			/*cerr<<j<<endl;
			cerr<<__builtin_clz((int)j)<<endl;
			cerr<<__builtin_clz((unsigned int)j)<<endl;
			cerr<<__builtin_clz(0)<<endl;*/
			
			int k= j==0?-1: 31-__builtin_clz(j);
			int num= __builtin_popcount(j);
			
			//f
			if(k+1==num)
			{
				add(f[x][k+2],fx[now][j]);
				add(f[x][0],gx[now][j]);
			}
			//g
			if(k+1==num+1)
			{
				for(int l=0;l<k;k++) if( !(j>>l&1) )
				{
					add(g[x][l+1],gx[now][j]);
					break;
				}
			}
		}
		for(int b=1;b<maxb;b++) if(f[x][b])
			add(g[x][b],f[x][b]);
	}
	else f[x][1]=g[x][1]=1;
	for(int b=0;b<maxb;b++) if(f[x][b]||g[x][b])
		mxb[x]=b;
}

signed main()
{
	/////////////////////////////////////////////////////////////////////////
	//freopen("tmp.in","r",stdin);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	//cerr<< ( 31-__builtin_clz(0) ) <<endl;
	
	cin>>n;
	for(int i=1;i<n;i++)
	{
		int x,y; cin>>x>>y;
		E[x].push_back(y);
		E[y].push_back(x);
	}
	dp(1,0);
	
	int ans=0;
	for(int i=0;i<=mxb[1];i++) 
		add(ans,f[1][i]);
	cout<<ans<<endl;
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 21056kb

input:

5
1 2
1 3
1 4
4 5

output:

21

result:

ok single line: '21'

Test #2:

score: 0
Accepted
time: 0ms
memory: 17520kb

input:

1

output:

1

result:

ok single line: '1'

Test #3:

score: -100
Time Limit Exceeded

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:


result: