QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#234658#7613. Inverse ProblemJryno1TL 3ms9280kbC++145.6kb2023-11-01 20:30:562023-11-01 20:30:58

Judging History

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

  • [2023-11-01 20:30:58]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:9280kb
  • [2023-11-01 20:30:56]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=305,mn=126,mod=1e9+7;
int T,f[maxn],g[maxn][maxn],C[maxn][maxn],fac[maxn],n,fa[maxn];
int find(int x){
    if(fa[x]==x)return x;
    return fa[x]=find(fa[x]);
}
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;
        }
    }
}
int A[maxn],B[maxn],ca,cb,res,pa[maxn],pb[maxn],dga[maxn],dgb[maxn];
int deg[maxn];
int ai=-1,aj,ar,bi,bj,br;
void dfsa(int nodes,int cdeg,int res){
    if(dga[1])return;
    if(nodes==ai){
        for(int i=1;i<=ca;i++){
            int nex=A[i]-1;
            if(cdeg+nex==aj&&res*g[n][nex]%mod==ar){
                pa[nodes]=nex+1;
                for(int i=1;i<=ai;i++)dga[i]=pa[i];
                break;
            }
        }
    } else {
        for(int i=1;i<=ca;i++){
            int nex=A[i]-1;
            if(cdeg+nex>aj)break;
            pa[nodes]=nex+1;
            dfsa(nodes+1,cdeg+nex,res*g[n][nex]%mod);
        }
    }
}
void dfsb(int nodes,int cdeg,int res){
    if(dgb[1])return;
    if(nodes==bi){
        for(int i=1;i<=cb;i++){
            int nex=B[i]-1;
            if(cdeg+nex==bj&&res*g[n][nex]%mod==br){
                pb[nodes]=nex+1;
                for(int i=1;i<=bi;i++)dgb[i]=pb[i];
                break;
            }
        }
    } else {
        for(int i=1;i<=cb;i++){
            int nex=B[i]-1;
            if(cdeg+nex>bj)break;
            pb[nodes]=nex+1;
            dfsb(nodes+1,cdeg+nex,res*g[n][nex]%mod);
        }
    }
}
void BT(){
    cout<<n<<"\n";
    sort(deg+1,deg+1+n);
    reverse(deg+1,deg+1+n);
    for(int i=1;i<=n;i++)fa[i]=i;
    for(int i=1;i<=n;i++){
        for(int j=i+1;j<=n;j++){
            if(!deg[i])break;
            if(!deg[j]||find(i)==find(j))continue;
            cout<<i<<" "<<j<<"\n";
            fa[find(i)]=find(j);
            deg[i]--,deg[j]--;
        }
    }
}
signed main(){
    init();
    cin.tie(0),cout.tie(0),ios::sync_with_stdio(false);
    cin>>T;
    while(T--){
        cin>>res;
        if(res==1){
            cout<<1<<"\n";
            continue;
        }
        for(n=2;n<=mn;n++){
            ca=0,cb=0;
            int nd=res*inv(f[n])%mod;
            for(int i=1;i<=n-1;i+=2)A[++ca]=i;
            for(int i=2;i<=n-1;i+=2)B[++cb]=i;
            vector<int>va[maxn][maxn],vb[maxn][maxn];
            va[0][0].push_back(1),vb[0][0].push_back(1);
            for(int i=0;i<n;i++){
                for(int j=0;j<=n-2;j++){
                    sort(va[i][j].begin(),va[i][j].end());
                    for(int k=0;k<va[i][j].size();k++){
                        int now=va[i][j][k];
                        for(int p=1;p<=ca;p++){
                            int nex=A[p]-1;
                            if(j+nex>n-2)break;
                            va[i+1][j+nex].push_back(now*g[n][nex]%mod);
                        }
                    }
                }
            }
            for(int i=0;i<=n;i++){
                for(int j=0;j<=n-2;j++){
                    if(i<n){
                        sort(vb[i][j].begin(),vb[i][j].end());
                        for(int k=0;k<vb[i][j].size();k++){
                            int now=vb[i][j][k];
                            for(int p=1;p<=cb;p++){
                                int nex=B[p]-1;
                                if(j+nex>n-2)break;
                                vb[i+1][j+nex].push_back(now*g[n][nex]%mod);
                            }
                        }
                    }
                    vb[i][j].push_back(mod+1);
                }
            }
            ai=-1;
            for(int i=0;i<=n;i++){
                for(int j=0;j<=n-2;j++){
                    for(int k=0;k<va[i][j].size();k++){
                        int nowval=va[i][j][k];
                        // cout<<i<<" "<<j<<" "<<nowval<<endl;
                        if(nowval==mod+1)continue;
                        int pos=lower_bound(vb[n-i][n-2-j].begin(),vb[n-i][n-2-j].end(),nd*inv(nowval)%mod)-vb[n-i][n-2-j].begin();
                        // cout<<i<<" "<<j<<" "<<va[i][j][k]<<"\n";
                        // cout<<pos<<" "<<vb[n-i][n-2-j].size()<<endl;
                        if(vb[n-i][n-2-j][pos]==nd*inv(nowval)%mod){
                            ai=i,aj=j,ar=nowval;
                            bi=n-i,bj=n-2-j,br=nd*inv(nowval)%mod;
                            break;
                        }
                    }
                    // cout<<i<<" "<<j<<" "<<endl;
                    if(ai!=-1)break;
                }
                if(ai!=-1)break;
            }
            // cout<<"out"<<endl;
            if(ai==-1)continue;
            memset(dga,0,sizeof(dga)),memset(dgb,0,sizeof(dgb));
            memset(pa,0,sizeof(pa)),memset(pb,0,sizeof(pb));
            if(ai)dfsa(1,0,1);
            if(bi)dfsb(1,0,1);
            for(int i=1;i<=ai;i++)deg[i]=dga[i];
            for(int i=1;i<=bi;i++)deg[i+ai]=dgb[i];
            // cout<<br<<endl;
            // cout<<dgb[1]<<endl;
            // for(int i=1;i<=n;i++)cout<<deg[i]<<" ";
            BT();
            break;
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 9280kb

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
3 5
4 6
5 7
6 8
7 9
8 10

result:

ok OK (4 test cases)

Test #2:

score: -100
Time Limit Exceeded

input:

9
185396120
468170792
837583517
696626231
338497514
762842660
800028852
928391161
733524004

output:


result: