QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#235308#7613. Inverse ProblemJryno1WA 19777ms199604kbC++144.0kb2023-11-02 17:09:182023-11-02 17:09:18

Judging History

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

  • [2023-11-02 17:09:18]
  • 评测
  • 测评结果:WA
  • 用时:19777ms
  • 内存:199604kb
  • [2023-11-02 17:09:18]
  • 提交

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){
	V[degs].push_back(res);
//	cout<<degs<<" "<<res<<endl;
	if(degs==n-2||pos==7||pos>n)return;
	for(int i=0;i<=n;i++){
		if(pos+1,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||pos==7||fla)return;
	if(degs==targdeg&&res==targres){
		fla=1;
		return;
	}
	for(int i=0;i<=n;i++){
		if(pos+1,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;
						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]++;
						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";
		}
	}
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 31ms
memory: 12636kb

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

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:

wrong answer Graf nie jest drzewem!