QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#490748#7613. Inverse Problemucup-team052AC ✓7702ms860452kbC++234.2kb2024-07-25 16:20:252024-07-25 16:20:25

Judging History

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

  • [2024-07-25 16:20:25]
  • 评测
  • 测评结果:AC
  • 用时:7702ms
  • 内存:860452kb
  • [2024-07-25 16:20:25]
  • 提交

answer

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int md = 1e9 + 7;
const int mod = 1e9 + 7;

inline int add(int x, int y) {
	if (x + y >= md) return x + y - md;
	return x + y;
}

inline int sub(int x, int y) {
	if (x < y) return x - y + md;
	return x - y;
}

inline int mul(int x, int y) {
	return 1ull * x * y % md;
}
inline int mul(int x,int y,int z) {return mul(mul(x,y),z);}

inline int qpow(int x, int y) {
	int ans = 1;
	while (y) {
		if (y & 1) ans = mul(ans, x);
		y >>= 1; x = mul(x, x);
	}
	return ans;
}
int Inv(int x) {return qpow(x,mod-2);}
const int L=130;
const int LIM=5;
int n,dv[L+5][L+5],idv[L+5][L+5];
int Q,q[15],ok[15],sok;
vector<int> ans[15];
int ansn[15];
vector<pair<int,__int128>> small[L],large[L];
bitset<mod> vis;
void dfs_small(int sum,int cur,int prod,__int128 hsh)
{
	small[sum].emplace_back(prod,hsh);
	for(int i=cur;i<=LIM&&sum+i<=n-2;i++)
	{
		dfs_small(sum+i,i,mul(prod,dv[n-2][i]),((hsh<<1)|1)<<(i-1));
	}
}
void dfs_large(int sum,int cur,int prod,__int128 hsh)
{
	large[sum].emplace_back(prod,hsh);
	for(int i=cur;sum+i<=n-2;i++)
	{
		dfs_large(sum+i,i,mul(prod,dv[n-2][i]),((hsh<<1)|1)<<(i-1));
	}
}
void init()
{
	for(int i=0;i<=n;i++) small[i].clear(),large[i].clear();
	dfs_small(0,1,1,0);
	dfs_large(0,LIM+1,1,0);
}
vector<int> getvec(int L,__int128 hl,int R,__int128 hr)
{
	vector<int> ans;
	int cur=0;
	while(hl)
	{
		cur++;
		if(hl&1) ans.push_back(cur),cur=0;
		hl>>=1;
	}
	cur=0;
	while(hr)
	{
		cur++;
		if(hr&1) ans.push_back(cur),cur=0;
		hr>>=1;
	}
	return ans;
}
int main() {
 #ifdef xay5421
	freopen("a.in","r",stdin);
 #endif
	for(int i=1;i<=L;i++)
	{
		dv[i][0]=1;
		for(int j=1;j<=L;j++) dv[i][j]=mul(dv[i][j-1],i-j+1),idv[i][j]=Inv(dv[i][j]);
	}
	cin>>Q;
	for(int i=1;i<=Q;i++)
	{
		cin>>q[i];
		if(q[i]==1) ok[i]=1,sok++;
		if(q[i]==2)
		{
			ok[i]=1,sok++;
		}
	}
	for(n=3;n<=125&&sok<Q;n++)
	{
		fprintf(stderr,"n = %d\n",n);
		init();
		int adw=mul(n,n-1);
		for(int L=0;L<=n-2;L++)
		{
			int R=n-2-L;
			// printf("%d %d\n",(int)small[L].size(),(int)large[R].size());
			if((int)small[L].size()<(int)large[R].size())
			{
				for(auto [p,hsh]:large[R]) vis.set(p);
				for(auto [p,hsh]:small[L])
				{
					int cv=mul(adw,p),icv=Inv(cv);
					for(int qid=1;qid<=Q;qid++)
					{
						if(ok[qid]) continue;
						int ned=mul(q[qid],icv);
						if(vis[ned])
						{
							for(auto [p_,hl]:large[R]) if(p_==ned)
							{
								// printf("%d : %d %d : %d %d\n",qid,p,p_,(int)hl,(int)hsh);
								ans[qid]=getvec(L,hl,R,hsh);
								ansn[qid]=n;
								ok[qid]=1,sok++;
								break;
							}
						}
					}
				}
				for(auto [p,hsh]:large[R]) vis.reset(p);
			}
			else
			{
				for(auto [p,hsh]:small[L]) vis.set(p);
				for(auto [p,hsh]:large[R])
				{
					int cv=mul(adw,p),icv=Inv(cv);
					for(int qid=1;qid<=Q;qid++)
					{
						if(ok[qid]) continue;
						int ned=mul(q[qid],icv);
						if(vis[ned])
						{
							for(auto [p_,hl]:small[L]) if(p_==ned)
							{
								// printf("%d : %d %d : %d %d\n",qid,p,p_,(int)hl,(int)hsh);
								ans[qid]=getvec(L,hl,R,hsh);
								ansn[qid]=n;
								ok[qid]=1,sok++;
								break;
							}
						}
					}
				}
				for(auto [p,hsh]:small[L]) vis.reset(p);
			}
		}
	}
	for(int i=1;i<=Q;i++)
	{
		if(q[i]==1) printf("1\n");
		else if(q[i]==2) printf("2\n1 2\n");
		else
		{
			printf("%d\n",ansn[i]);
			while((int)ans[i].size()<ansn[i]) ans[i].push_back(0);
			int sum=0;
			for(int j=0;j<(int)ans[i].size();j++) sum+=ans[i][j];
			assert(sum==ansn[i]-2);
	//		for(int j=0;j<(int)ans[i].size();j++) ans[i][j]++;
			for(int j=0;j<ansn[i]-1;j++)
			{
				for(int c=0;c<(int)ans[i].size();c++) for(int d=0;d<(int)ans[i].size();d++)
				{
					if(ans[i][c]==0&&ans[i][d]>0)
					{
						ans[i][c]--,ans[i][d]--;
						printf("%d %d\n",c+1,d+1);
					}
				}
			}
			for(int c=0;c<(int)ans[i].size();c++) for(int d=c+1;d<(int)ans[i].size();d++)
			{
				if(ans[i][c]==0&&ans[i][d]==0)
				{
					ans[i][c]--,ans[i][d]--;
					printf("%d %d\n",c+1,d+1);
				}
			}
		}
	}
	return 0;
}

詳細信息

Test #1:

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

input:

4
2
360
1
509949433

output:

2
1 2
5
3 1
4 1
5 2
1 2
1
10
9 1
10 2
1 3
2 4
3 5
4 6
5 7
6 8
7 8

result:

ok OK (4 test cases)

Test #2:

score: 0
Accepted
time: 7681ms
memory: 860452kb

input:

9
185396120
468170792
837583517
696626231
338497514
762842660
800028852
928391161
733524004

output:

14
11 1
12 1
13 2
14 2
1 3
2 4
3 5
4 6
5 7
6 8
7 9
8 10
9 10
122
21 1
22 1
23 1
24 1
25 1
26 1
27 1
28 1
29 1
30 1
31 1
32 1
33 1
34 1
35 1
36 1
37 1
38 1
39 1
40 1
41 1
42 1
43 1
44 1
45 1
46 1
47 1
48 1
49 1
50 2
51 2
52 2
53 2
54 2
55 2
56 2
57 2
58 2
59 2
60 2
61 2
62 2
63 2
64 3
65 3
66 3
67 3
...

result:

ok OK (9 test cases)

Test #3:

score: 0
Accepted
time: 7702ms
memory: 852200kb

input:

10
338497514
733524004
447182954
415993605
453460670
50499055
648088551
986982752
907925397
315315230

output:

124
23 1
24 1
25 1
26 1
27 1
28 1
29 1
30 1
31 1
32 1
33 1
34 1
35 1
36 1
37 1
38 1
39 1
40 1
41 1
42 1
43 1
44 1
45 1
46 1
47 1
48 1
49 1
50 1
51 1
52 1
53 1
54 1
55 1
56 1
57 2
58 2
59 2
60 2
61 2
62 2
63 2
64 2
65 2
66 2
67 2
68 2
69 2
70 2
71 2
72 3
73 3
74 3
75 3
76 3
77 3
78 3
79 3
80 3
81 4
8...

result:

ok OK (10 test cases)

Test #4:

score: 0
Accepted
time: 1422ms
memory: 277672kb

input:

10
1
2
3
4
5
6
7
8
9
10

output:

1
2
1 2
102
16 1
17 1
18 1
19 1
20 1
21 1
22 1
23 1
24 1
25 1
26 1
27 1
28 1
29 1
30 1
31 1
32 1
33 1
34 1
35 1
36 1
37 1
38 1
39 2
40 2
41 2
42 2
43 2
44 2
45 2
46 2
47 2
48 2
49 2
50 2
51 2
52 2
53 2
54 2
55 2
56 2
57 3
58 3
59 3
60 3
61 3
62 3
63 3
64 3
65 3
66 3
67 3
68 3
69 3
70 4
71 4
72 4
73 ...

result:

ok OK (10 test cases)

Test #5:

score: 0
Accepted
time: 554ms
memory: 173848kb

input:

10
269199917
392009324
753889928
751355133
472639410
132096559
331333826
40414701
72847302
475706026

output:

55
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
11 1
12 1
13 1
14 1
15 1
16 1
17 1
18 1
19 1
20 1
21 1
22 1
23 1
24 1
25 1
26 1
27 1
28 1
29 1
30 1
31 1
32 1
33 1
34 1
35 1
36 1
37 1
38 1
39 1
40 1
41 1
42 1
43 1
44 1
45 1
46 1
47 1
48 1
49 1
50 1
51 1
52 1
53 1
54 1
1 55
84
36 1
37 1
38 1
39 1
40 2
41 2
42...

result:

ok OK (10 test cases)

Extra Test:

score: 0
Extra Test Passed