QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#326980#4888. Decoding The MessagezhouhuanyiTL 3ms4056kbC++142.8kb2024-02-14 16:55:012024-02-14 16:55:01

Judging History

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

  • [2024-02-14 16:55:01]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:4056kb
  • [2024-02-14 16:55:01]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<bitset>
#define N 257
#define mod 65535
#define mod2 32768
using namespace std;
const int inf=(int)(1e9);
const int inv2=(mod+1)>>1;
const int delta[4]={3,5,17,257};
int read()
{
	char c=0;
	int sum=0;
	while (c<'0'||c>'9') c=getchar();
	while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
	return sum;
}
int MD(int x)
{
	return x>=mod?x-mod:x;
}
int MD2(int x)
{
	return x<0?x+mod:x;
}
void Adder(int &x,int d)
{
	x+=d;
	if (x>=mod) x-=mod;
	return;
}
void Adder2(int &x,int d)
{
	x+=d;
	if (x<0) x+=mod;
	return;
}
int T,k,ans,ans2,a,b,n,cnt[N+1],scnt[N+1],tong[N+1];
bitset<N+1>used[N+1];
bitset<N+1>used2[N+1];
int fast_pow(int a,int b)
{
	int res=1,mul=a;
	while (b)
	{
		if (b&1) res=1ll*res*mul%mod;
		mul=1ll*mul*mul%mod,b>>=1;
	}
	return res;
}
int main()
{
	int maxn,rt,rcnt,res,ps;
	long long rst;
	bool op;
	T=read();
	while (T--)
	{
		k=read(),n=ans=0,ans2=1;
		for (int i=0;i<=255;++i) cnt[i]=0;
		for (int i=1;i<=k;++i) cnt[read()]=read();
		for (int i=0;i<=255;++i) n+=cnt[i],Adder(ans,1ll*i*cnt[i]%mod);
		if (n<=18)
		{
			ps=0,ans=1;
			for (int i=0;i<=255;++i)
				for (int j=1;j<=cnt[i];++j)
					tong[++ps]=i;
			for (int i=0;i<(1<<n);++i)
				if (__builtin_popcount(i)==((n+1)>>1))
				{
					res=0;
					for (int j=1;j<=n;++j)
					{
						if (!((i>>(j-1))&1)) res=MD(res+256*tong[j]%mod);
						else Adder(res,tong[j]);
					}
					ans=1ll*ans*res%mod;
				}
			rst=1;
			for (int i=1;i<=(n>>1);++i) rst*=i;
			for (int i=1;i<=((n+1)>>1);++i) rst*=i;
			if (rst>=mod2) rst=rst%mod2+mod2;
			ans=fast_pow(ans,rst),printf("%d\n",ans);
		}
		else
		{
			ans=fast_pow(ans,mod2);
			for (int i=0;i<=3;++i)
			{
				maxn=-inf,rt=res=op=rcnt=0;
				for (int j=0;j<delta[i];++j) scnt[j]=0;
				for (int j=0;j<=255;++j) scnt[j%delta[i]]+=cnt[j];
				for (int j=0;j<delta[i];++j) res=(res+1ll*j*scnt[j])%delta[i];
				res=1ll*res*((delta[i]+1)>>1)%delta[i];
				for (int j=0;j<delta[i];++j)
					if (scnt[j]>maxn)
						maxn=scnt[j],rt=j;
				for (int j=0;j<delta[i];++j)
					if (rt!=j)
						rcnt+=scnt[j];
				if (min(rcnt,n>>1)>=delta[i]) op=1;
				else
				{
					for (int j=0;j<delta[i];++j) used[j].reset();
					used[0][0]=1;
					for (int j=0;j<delta[i];++j)
						if (rt!=j)
						{
							for (int k=1;k<=scnt[i];++k)
							{
								for (int t=0;t<delta[i];++t) used2[(t+j)%delta[i]]=used[t]<<1;
								for (int t=0;t<delta[i];++t) used[t]|=used2[t];
							}
						}
					for (int j=0;j<delta[i];++j)
						for (int k=0;k<=min(((n+1)>>1),rcnt);++k)
							if (used[j][k]&&(1ll*(((n+1)>>1)-k)*rt+j)%delta[i]==res)
								op=1;
				}
				if (op) ans2=1ll*ans2*fast_pow(delta[i],mod2)%mod;
			}
			a=1ll*MD(ans+ans2)*inv2%mod,b=1ll*MD2(ans-ans2)*inv2%mod,printf("%d\n",MD(a+256ll*b%mod));
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5
1
42 1
2
0 1
1 1
1
239 2
2
1 1
2 1
3
1 1
2 2
3 2

output:

42
256
514
1284
61726

result:

ok 5 number(s): "42 256 514 1284 61726"

Test #2:

score: 0
Accepted
time: 3ms
memory: 4056kb

input:

100
1
213 79
1
54 69
1
218 55
1
248 80
1
101 8
1
153 79
1
240 45
1
112 70
1
217 5
1
208 64
1
48 30
1
0 19
1
53 40
1
63 17
1
179 65
1
221 22
1
135 84
1
138 20
1
54 29
1
114 19
1
253 94
1
240 36
1
40 94
1
244 93
1
239 24
1
133 8
1
105 91
1
45 43
1
241 74
1
206 17
1
100 73
1
133 44
1
57 70
1
56 72
1
47...

output:

21846
21846
26215
59110
32896
6426
48060
59110
43570
32896
15420
0
59110
6426
26215
17476
15420
15420
21846
21846
32896
15420
59110
21846
54741
32896
48060
48060
32896
50116
26215
32896
15420
54741
6426
17476
15420
21846
54741
39321
54741
54741
6426
54741
1
59110
59110
26215
54741
15420
15420
22101
...

result:

ok 100 numbers

Test #3:

score: -100
Time Limit Exceeded

input:

100
1
208 74003161
1
108 37880052
1
102 289342450
1
190 16254190
1
20 507132853
1
1 842472902
1
212 226854721
1
33 797732105
1
213 114087750
1
128 914592259
1
27 779924279
1
203 425018504
1
217 458915584
1
139 710603120
1
226 84538604
1
50 602470204
1
150 228443262
1
48 593328022
1
24 35949157
1
148...

output:


result: