QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#232279#7613. Inverse ProblemEdwin_VanCleefTL 305ms3376kbC++142.4kb2023-10-30 09:12:112023-10-30 09:12:12

Judging History

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

  • [2023-10-30 09:12:12]
  • 评测
  • 测评结果:TL
  • 用时:305ms
  • 内存:3376kb
  • [2023-10-30 09:12:11]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
namespace my_IO{
    #define ll long long
    #define ull unsigned long long
    #define ld long double
    #define spc putchar(' ')
    #define et putchar('\n')
    template<class T>
    void read(T &num){
        T x=0,f=1;
        char c=getchar();
        while(!isdigit(c)){
            if(c=='-') f=-1;
            c=getchar();
        }
        while(isdigit(c)){
            x=(x<<3)+(x<<1)+c-48;
            c=getchar();
        }
        num=f*x;
    }
    template<class T>
    void write(T x){
        static char buf[40];
        static int len=-1;
        if(x<0) putchar('-'),x=-x;
        do{
            buf[++len]=x%10+48;
            x/=10;
        }while(x);
        while(len>=0) putchar(buf[len--]);
    }
}
using namespace my_IO;
const ll mod=1e9+7;
const ll maxn=130;
ll fa[maxn],head[maxn],tot,d[maxn],f[maxn];
struct edge{
    ll t,nxt;
}e[maxn<<1];
void add(ll st,ll ed){
    e[++tot]={ed,head[st]};
    head[st]=tot;
}
ll ksm(ll a,ll b){
    ll res=1;
    while(b){
        if(b&1) res=res*a%mod;
        b>>=1;
        a=a*a%mod;
    }
    return res;
}
ll ni(ll x){
    return ksm(x,mod-2);
}
ll C(ll n,ll m){
    if(n<0||m<0||n<m) return 0;
    return f[n]*ni(f[n-m])%mod*ni(f[m])%mod;
}
ll cnt(ll u,ll fa,ll n){
    ll res;
    if(u==1) res=C(n,d[1]+1)*f[d[1]+1]%mod;
    else res=C(n-2,d[u]-1)*f[d[u]-1]%mod;
    for(ll i=head[u];i;i=e[i].nxt){
        if(e[i].t!=fa){
            res=res*cnt(e[i].t,u,n)%mod;
        }
    }
    return res;
}
bool dfs(ll now,ll n,ll ans){
    if(now==n+1){
        for(ll i=1;i<=n;i++) head[i]=d[i]=0;
        tot=0;
        // cout<<n<<":\n";
        for(ll i=2;i<=n;i++){
            // cout<<fa[i]<<" "<<i<<"\n";
            add(i,fa[i]);
            add(fa[i],i);
            d[i]++,d[fa[i]]++;
        }
        // cout<<cnt(1,0,n)<<"\n";
        return(cnt(1,0,n)==ans);
    }
    for(ll i=1;i<now;i++){
        fa[now]=i;
        if(dfs(now+1,n,ans)) return true;
    }
    return false;
}
void print(ll n){
    write(n),et;
    for(ll i=2;i<=n;i++) write(fa[i]),spc,write(i),et;
}
void solve(){
    ll r;
    read(r);
    for(ll i=1;;i++){
        if(dfs(2,i,r)){
            print(i);
            break;
        }
    }
    // et;
}
int main(){
    f[0]=1;
    for(ll i=1;i<=120;i++) f[i]=f[i-1]*i%mod;
    int t=1;
    read(t);
    while(t--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 305ms
memory: 3376kb

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: