QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#235343#7613. Inverse ProblemJryno1AC ✓36366ms201444kbC++144.3kb2023-11-02 17:39:432023-11-02 17:39:44

Judging History

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

  • [2023-11-02 17:39:44]
  • 评测
  • 测评结果:AC
  • 用时:36366ms
  • 内存:201444kb
  • [2023-11-02 17:39:43]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define mkp(x,y) make_pair(x,y)
#define fi first
#define se second
int mod=1e9+7;
const int maxn=130;
int C[maxn][maxn],g[maxn][maxn],fac[maxn],f[maxn],qp[maxn][maxn][maxn],fa[maxn];
int Q[maxn];
int find(int x){
    if(fa[x]==x)return x;
    return fa[x]=find(fa[x]);
}
struct Fun{
	int tot;
	vector<pair<int,int> > tr;
}ans[maxn];
int qpow(int x,int y){
    int res=1;
    for(;y;y>>=1){
        if(y&1)res=res*x%mod;
        x=x*x%mod;
    }
    return res;
}
int inv(int x){
    return qpow(x,mod-2);
}
void init(){
    for(int i=0;i<maxn;i++){
        C[i][0]=1;
        for(int j=1;j<=i;j++)C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
    }
    fac[0]=1;
    for(int i=1;i<maxn;i++)fac[i]=fac[i-1]*i%mod;
    for(int i=1;i<maxn;i++){
        f[i]=i*(i-1);
        for(int j=0;j<i-1;j++){
            g[i][j]=C[i-2][j]*fac[j]%mod;
     		for(int k=0;k<maxn;k++)qp[i][j][k]=qpow(g[i][j],k); 
			g[i][j]=inv(g[i][j]);	
	    }
    }
}
int n,cc=0;
vector<int>V[maxn],S[maxn];
void dfsA(int pos,int degs,int res){
//	cout<<pos<<" "<<degs<<" "<<res<<endl;
	V[degs].push_back(res);
//	if(res==649346304)cout<<degs<<" "<<pos<<endl;
//	cout<<degs<<" "<<res<<endl;
	if(degs==n-2||pos==7||pos>n)return;
	for(int i=0;i<=n;i++){
		if(degs+pos*i>n-2)return;
		dfsA(pos+1,degs+pos*i,res*qp[n][pos][i]%mod);
	}
}
void dfsB(int now,int degs,int res){
	S[degs].push_back(res);
	for(int i=now;i<=n-2-degs;i++)dfsB(i,degs+i,res*g[n][i]%mod);
}
vector<pair<int,int> >ra,rb;
int fla=0,flb=0,deg[maxn],cdeg;
void finda(int targdeg,int targres,int pos,int degs,int res){
	if(degs==targdeg&&res==targres){
		fla=1;
//		for(int i=0;i<ra.size();i++)cout<<ra[i].fi<<" "<<ra[i].se<<" === ";
//		cout<<endl;
		return;
	}
	if(degs==n-2||pos==7)return;
	for(int i=0;i<=n;i++){
		if(degs+pos*i>n-2)return;
		ra.push_back(mkp(pos,i));
		finda(targdeg,targres,pos+1,degs+pos*i,res*qp[n][pos][i]%mod);
		if(fla)break;
		ra.pop_back();
	}
}
void findb(int targdeg,int targres,int pos,int degs,int res){
	if(degs>targdeg||flb)return;
	if(degs==targdeg&&res==targres){
		flb=1;
		return;
	}
	for(int i=pos;i<=n-2-degs;i++){
		rb.push_back(mkp(i,1));
		findb(targdeg,targres,i,degs+i,res*g[n][i]%mod);
		if(flb)break;
		rb.pop_back();
	}
}
int vis[maxn];
void dfs3(int x,int v){
	vector<int>SSS;
	for(int i=1;i<=n;i++){
		if(vis[i])continue;
		if(!deg[i]||!deg[x])continue;
		deg[x]--;
		ans[v].tr.push_back(mkp(x,i));
		SSS.push_back(i);
		vis[i]=1;
		deg[i]--;
	}
	for(int i=0;i<SSS.size();i++){
//		cout<<"XX"<<x<<" "<<SSS[i]<<endl;
		dfs3(SSS[i],v);
	}
}
void BT(int x){
    ans[x].tot=n;
    sort(deg+1,deg+1+n);
    reverse(deg+1,deg+1+n);
    memset(vis,0,sizeof(vis));
    vis[1]=1;
	dfs3(1,x);
}
signed main(){
	int T,tots=0;
	cin>>T;
	tots=T;
	for(int i=1;i<=T;i++){
		cin>>Q[i];
		if(Q[i]==1){
			ans[i].tot=1;
			Q[i]=0;
			tots--;
		}
	}
	init();
	for(n=1;n<=125;n++){
//		cout<<n<<endl;
		if(!tots)break;
		for(int i=0;i<=125;i++)S[i].clear(),V[i].clear(),S[i].push_back(mod+1);
		dfsA(1,0,1);
		dfsB(7,0,1);
		for(int i=0;i<=125;i++)sort(S[i].begin(),S[i].end());
		for(int q=1;q<=T;q++){
			if(!Q[q])continue;
			int R=inv(Q[q]*inv(f[n])%mod);
			for(int sa=0;sa<=n-2;sa++){
				if(!Q[q])break;
				for(int j=0;j<V[sa].size();j++){
					int now=V[sa][j],nd=R*now%mod;
//					cout<<now<<" "<<nd<<" "<<n-sa-2<<endl;
					int pos=lower_bound(S[n-sa-2].begin(),S[n-sa-2].end(),nd)-S[n-sa-2].begin();
//					cout<<"out"<<endl;
//					cout<<S[n-sa-2][0]<<endl;
					if(S[n-sa-2][pos]==nd){
//						cout<<"f"<<endl;
						ra.clear(),rb.clear(),fla=0,flb=0;
//						cout<<sa<<" "<<n-2-sa<<" "<<now<<endl;
						finda(sa,now,1,0,1);
						findb(n-sa-2,nd,7,0,1);
						memset(deg,0,sizeof(deg));
						cdeg=0;
						for(int i=0;i<ra.size();i++){
							while(ra[i].se)deg[++cdeg]=ra[i].fi,ra[i].se--;
						}
						for(int i=0;i<rb.size();i++)deg[++cdeg]=rb[i].fi;
						for(int i=1;i<=n;i++)deg[i]++;
//						cout<<endl;
						BT(q);
						tots--,Q[q]=0;
						break;
					}
				}
			}
		}
	}
	for(int i=1;i<=T;i++){
		cout<<ans[i].tot<<"\n";
		for(int j=0;j<ans[i].tr.size();j++){
			cout<<ans[i].tr[j].fi<<" "<<ans[i].tr[j].se<<"\n";
		}
	}
} 

详细

Test #1:

score: 100
Accepted
time: 27ms
memory: 12624kb

input:

4
2
360
1
509949433

output:

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

result:

ok OK (4 test cases)

Test #2:

score: 0
Accepted
time: 19513ms
memory: 199572kb

input:

9
185396120
468170792
837583517
696626231
338497514
762842660
800028852
928391161
733524004

output:

14
1 2
1 3
1 4
2 5
2 6
5 7
7 8
8 9
9 10
10 11
6 12
3 13
4 14
122
1 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
2 32
2 33
2 34
2 35
2 36
2 37
2 38
2 39
2 40
2 41
2 42
2 43
2 44
2 45
3 46
3 47
3 48
3 49
3 5...

result:

ok OK (9 test cases)

Test #3:

score: 0
Accepted
time: 36366ms
memory: 201444kb

input:

10
338497514
733524004
447182954
415993605
453460670
50499055
648088551
986982752
907925397
315315230

output:

124
1 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
2 37
2 38
2 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
3 52
3 53
3 54
3 55
3 56
3 57
3 58
3 59
3 60
4 61
4 62...

result:

ok OK (10 test cases)

Test #4:

score: 0
Accepted
time: 4643ms
memory: 75348kb

input:

10
1
2
3
4
5
6
7
8
9
10

output:

1
2
1 2
102
1 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
2 26
2 27
2 28
2 29
2 30
2 31
2 32
2 33
2 34
2 35
2 36
2 37
2 38
2 39
2 40
2 41
2 42
2 43
3 44
3 45
3 46
3 47
3 48
3 49
3 50
3 51
3 52
3 53
3 54
3 55
3 56
4 57
4 58
4 59
4 60
4...

result:

ok OK (10 test cases)

Test #5:

score: 0
Accepted
time: 1214ms
memory: 42140kb

input:

10
269199917
392009324
753889928
751355133
472639410
132096559
331333826
40414701
72847302
475706026

output:

55
1 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 55
84
1 2
1 3
1 4
1 5
1 6
1 7
1 8
2 9
...

result:

ok OK (10 test cases)

Extra Test:

score: 0
Extra Test Passed