QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#224891 | #7613. Inverse Problem | ucup-team004# | TL | 1ms | 3620kb | C++20 | 2.9kb | 2023-10-23 16:51:16 | 2023-10-23 16:51:18 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1000000007;
int ksm(ll a,int b,int c=1){
for(;b;b/=2,a=a*a%mod)
if(b&1)c=c*a%mod;
return c;
}
const int N=210;
vector<pair<int,int> >res;
int a[N];
int g[N],ig[N];
int last_ans;
const int B=4;
vector<int>resL_list,resR_list;
int ans_flag;
bool dfsL(int s,int x,int f,int n,int l){
if(s==0){
if(ans_flag){
if(f!=ans_flag)
return 0;
for(int i=1;i<x;++i)
res.emplace_back(a[i]+1,res.size()+1);
return 1;
}else{
resL_list.push_back(f);
return 0;
}
}
for(int i=min(s,l);i>=1;--i){
a[x]=i;
if(dfsL(s-i,x+1,(ll)f*ig[i]%mod,n,i))
return 1;
}
return 0;
}
bool dfsR(int s,int x,int f,int n,int l){
if(s==0){
if(ans_flag){
if(f!=ans_flag)
return 0;
for(int i=1;i<x;++i)
res.emplace_back(a[i]+1,res.size()+1);
return 1;
}else{
resR_list.push_back(f);
return 0;
}
}
for(int i=l;i<=s;++i){
a[x]=i;
if(dfsR(s-i,x+1,(ll)f*g[i]%mod,n,i))
return 1;
}
return 0;
}
void output(int n){
// for(auto i:res)
// cout<<i.first<<' '<<i.second<<"!\n";
cout<<n<<"\n";
while(res.size()<n)
res.emplace_back(0,res.size()+1);
for(;;){
if(res.size()<=1)
return;
sort(res.begin(),res.end());
reverse(res.begin(),res.end());
cout<<res[0].second<<' '<<res[res.size()-1].second<<'\n';
res[0].first--;
res.pop_back();
}
}
void solve(int x){
if(x==1){
cout<<"1\n";
return;
}
for(int i=2;;++i){
assert(i<N);
g[0]=1;
for(int j=1;j<=i;++j)
g[j]=(ll)g[j-1]*(i-j-1)%mod;
for(int j=0;j<=i;++j)
ig[j]=ksm(g[j],mod-2);
int rx=ksm(i*(i-1),mod-2,x);
for(int j=0;j<=i-2;++j){
ans_flag=0;
resL_list.clear();
resR_list.clear();
dfsL(j,1,1,i,B);
dfsR(i-2-j,1,1,i,B+1);
sort(resR_list.begin(),resR_list.end());
for(int k:resL_list){
int u=(ll)k*rx%mod;
auto it=lower_bound(resR_list.begin(),resR_list.end(),u);
if(it!=resR_list.end()&&*it==u){
res.clear();
ans_flag=k;
dfsL(j,1,1,i,B);
ans_flag=u;
dfsR(i-2-j,1,1,i,B+1);
output(i);
return;
}
}
}
}
}
int T;
int main(){
ios::sync_with_stdio(0);cin.tie(0);
for(cin>>T;T --> 0;){
int x;cin>>x;
solve(x);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3620kb
input:
4 2 360 1 509949433
output:
2 2 1 5 1 3 2 4 1 5 2 1 1 10 8 9 7 10 6 7 5 6 4 5 3 4 2 3 1 2 8 1
result:
ok OK (4 test cases)
Test #2:
score: -100
Time Limit Exceeded
input:
9 185396120 468170792 837583517 696626231 338497514 762842660 800028852 928391161 733524004