QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#56083#4888. Decoding The MessageSorting#WA 19ms3908kbC++3.5kb2022-10-16 21:08:092022-10-16 21:08:10

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-16 21:08:10]
  • 评测
  • 测评结果:WA
  • 用时:19ms
  • 内存:3908kb
  • [2022-10-16 21:08:09]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef unsigned char uc;
typedef long long ll;
#define fi first
#define se second
const int iu=256;
pair<ll,int>f[iu];
ll n;
ll pw(ll x,ll y,ll z){//computes x^y mod z
	if(y==0) return 1;
	if(y%2) return x*pw(x,y-1,z)%z;
	ll res=pw(x,y/2,z);
	return res*res%z;
}
ll pwf(ll x,ll y,ll z){//computes x^(y!) mod z, where z = 3,5 or 17
	if(x==0) return 0;
	if(y>=6) return 1;
	ll res=x;
	for(int i=1; i<=y ;i++) res=pw(res,i,z);
	return res;
}
uc w[2][257][iu];
bool c[2][257][iu];
int ff[iu+1],inv[iu+1];
void pre(){
	ff[0]=1;
	for(int i=1; i<=iu ;i++){
		if(i%2==0){
			ff[i]=ff[i-1];continue;
		}
		while(i*inv[i]%iu!=1){
			inv[i]++;
			//cout << i << ' ' << inv[i] << endl;
		}
		ff[i]=ff[i-1]*i%iu;
	}
}
pair<int,int>F(int x){// x! = u*2^v, returns (u mod 256,v)
	if(x<2) return {1,0};
	auto duh=F(x/2);
	duh.se+=x/2;
	duh.fi=(duh.fi*ff[x%256])%256;
	return duh;
}
int F2(int x){//x! mod 256
	auto cur=F(x);
	if(cur.se>8) return 0;
	else return (cur.fi<<cur.se)%256;
}
int C(ll x,ll y){// C(x,y) mod 256
	//cout << "C " << x << ' ' << y << "=";
	if(y<0 || y>x) return 0;
	auto a=F(x);
	auto b=F(y);
	auto c=F(x-y);
	int cur=a.fi*inv[b.fi]*inv[c.fi]%256;
	int di=a.se-b.se-c.se;
	if(di>8) return 0;
	//cout << (cur<<di)%256 << endl;
	return (cur<<di)%256;
}
void solve(){
	n=0;
	ll s=0;
	for(int i=0; i<iu ;i++) f[i]={0,i};
	{
		int k;cin >> k;
		for(int i=0; i<k ;i++){
			int x,y;cin >> x >> y;f[x]={y,x};
			n+=y;
			s+=x*y;
		}
	}
	sort(f,f+iu);
	ll z255=0,z257=0;
	{//compute mod 255
		ll z3=pwf(s%3,n,3);
		ll z5=pwf(s%5,n,5);
		ll z17=pwf(s%17,n,17);
		while(true){//CRT
			if(z255%3==z3 && z255%5==z5 && z255%17==z17) break;
			z255++;
		}
	}
	{//compute mod 257
		if(n>=2*iu && n-f[iu-1].fi>=iu);//mod 257 = 0 
		else{
			for(int i=0; i<2 ;i++){
				for(int j=0; j<=256 ;j++){
					for(int k=0; k<iu ;k++){
						w[i][j][k]=0;c[i][j][k]=0;
					}
				}
			}
			w[0][0][0]=1;c[0][0][0]=1;
			int cur=0,prv=1;
			for(int i=0; i<iu-1 ;i++){
				for(int j=0; j<f[i].fi ;j++){
					cur^=1;prv^=1;
					int x=f[i].se;
					for(int j=0; j<257 ;j++){
						for(int k=0; k<iu ;k++){
							w[cur][j][k]=0;c[cur][j][k]=0;
						}
					}
					for(int j=0; j<257 ;j++){
						for(int k=0; k<iu ;k++){
							int nj=((j+x)>=257)?j+x-257:j+x;
							if(!c[prv][j][k]) continue;
							if(k+1<iu) w[cur][nj][k+1]+=w[prv][j][k],c[cur][nj][k+1]=true;//use
							w[cur][j][k]+=w[prv][j][k],c[cur][j][k]=true;//not use
						}
					}
					
				}
			}
			bool die=false;
			z257=1;
			for(int i=0; i<257 ;i++){
				for(int j=0; j<iu ;j++){
					int x=f[iu-1].fi;
					int y=n/2-j;
					if(y>x || y<0 || !c[cur][i][j]) continue;
					//cout << "?? " << i << ' ' << j << ' ' << x << ' ' << y << endl;
					int z=(i+y*f[iu-1].se)%257;
					z=(s+2*(257-z))%257;
					if(z==0) z257=0;
					int ways=C(x,y)*w[cur][i][j]%256;
					
					//cout << s << ' ' << z << ' ' << ways << ' ' << i << ' ' << j << endl;
					z257=z257*pw(z,ways,257)%257;
					if(z257==0){
						die=true;break;
					}
					
				}
				if(die) break;
			}
			//cout << z257 << ' '<<  n << ' ';
			if(z257!=0){
				
				z257=pw(z257,F2(n/2),257);
				z257=pw(z257,F2((n+1)/2),257);
			}
			//cout << z257 << endl;
		}
	}
	
	//cout << z255 << ' ' << z257 << endl;
	while(z255%257!=z257) z255+=255;
	cout << z255 << '\n';
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	pre();
	int t;cin >> t;
	while(t--) solve();
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 3908kb

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: 4ms
memory: 3864kb

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
Wrong Answer
time: 19ms
memory: 3740kb

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:

21846
26215
1
21846
21846
32896
21846
21846
54741
26215
1
32896
26215
21846
59110
1
32896
1
21846
21846
1
21846
54741
15420
32896
32896
32896
54741
1
48060
21846
1
26215
21846
1
1
48060
26215
1
1
54741
1
21846
32896
26215
21846
21846
1
54741
1
1
26215
1
26215
32896
1
1
50116
26215
54741
1
48060
5474...

result:

wrong answer 1st numbers differ - expected: '1', found: '21846'