QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#56077#4888. Decoding The MessageSorting#WA 7ms3912kbC++3.3kb2022-10-16 20:46:182022-10-16 20:46:20

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 20:46:20]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:3912kb
  • [2022-10-16 20:46:18]
  • 提交

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,m;
ll pw(ll x,ll y,ll 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){
	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){//mod 256
	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){
	auto cur=F(x);
	if(cur.se>8) return 0;
	else return (cur.fi<<cur.se)%256;
}
int C(ll x,ll 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++;
		}
	}
	{
		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 << ' ';
			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();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: -100
Wrong Answer
time: 7ms
memory: 3876kb

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
26215
32896
6426
48060
26215
43570
1
48060
32640
26215
6426
26215
50116
48060
48060
21846
21846
1
48060
26215
21846
21846
32896
48060
48060
1
50116
26215
1
48060
21846
6426
50116
48060
21846
21846
6426
21846
21846
6426
21846
1
26215
26215
26215
54741
15420
48060
22101
26215
26215
1...

result:

wrong answer 4th numbers differ - expected: '59110', found: '26215'