QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#223013 | #7613. Inverse Problem | ucup-team191# | TL | 7ms | 23848kb | C++23 | 3.1kb | 2023-10-21 20:50:39 | 2023-10-21 20:50:39 |
Judging History
answer
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define x first
#define y second
using namespace std;
using namespace __gnu_pbds;
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vl=vector<ll>;
using ld=long double;
#define all(a) begin(a),end(a)
#define pb push_back
const int N=310,MOD=1e9+7;
const char en='\n';
const ll LLINF=1ll<<60;
int mul(int a,int b)
{
return a*1ll*b%MOD;
}
int pot(int n,int k)
{
if (k==0) return 1;
int r=pot(n,k/2);
r=mul(r,r);
if (k%2) r=mul(r,n);
return r;
}
int inv(int x)
{
return pot(x,MOD-2);
}
int t,rrem,rem,n,ms,efn;
vi mog;
vi cur;
int cuu;
//vector<pair<int,vi>> svp[N][N];
gp_hash_table<int,int> rsv[N][N];
vector<vi> zar[N];
bool bil;
vi ans;
void bact(int s,int la)
{
if (s<efn/3 && mul(cuu,mog[efn-s])==rem)
{
ans=cur;
ans.pb(efn-s);
//cout<<s<<en;
//for (auto x: ans) cout<<x<<' ';
//cout<<en;
return;
}
if (s>=efn/3)
{
if (!bil && rsv[efn][s].find(cuu)==rsv[efn][s].end())
{
//svp[efn][s].pb({cuu,cur});
auto it=lower_bound(all(zar[s]),cur);
if (it==zar[s].end() || *it!=cur)
{
rsv[efn][s][cuu]=zar[s].size();
zar[s].pb(cur);
}
else rsv[efn][s][cuu]=it-zar[s].begin();
}
else return;
}
if (s<=(efn-1)/2) for (int ne=la;ne>=1;--ne) if (s+ne<=ms)
{
cur.pb(ne);
int scuu=cuu;
cuu=mul(cuu,mog[ne]);
bact(s+ne,ne);
cuu=scuu;
cur.pop_back();
}
}
void out()
{
cout<<n<<endl;
cout<<1<<' '<<2<<endl;
int cu=2;
for (auto x: ans)
{
for (int i=1;i<=x;++i) cout<<cu<<' '<<cu+i<<endl;
cu+=x;
}
}
bool pok()
{
rem=mul(rrem,mul(inv(n),inv(n-1)));
mog.clear();
mog.pb(1);
mog.pb(n-2);
while (mog.back()>1) mog.pb(mog.back()-1);
for (int i=1;i<(int)mog.size();++i) mog[i]=mul(mog[i],mog[i-1]);
//cout<<n<<' '<<rem<<endl;
//for (auto x: mog) cout<<x<<' ';
//cout<<endl<<endl;
ans.clear();
efn=n-2;
ms=(efn*2+2)/3;
cur.clear();
cuu=1;
//cout<<n<<endl;
bil=rsv[efn][efn/3].size()>0;
bact(0,efn);
//cout<<"nap bact"<<endl;
int s1=0,s2=0;
/*if (!bil) for (int i=efn/3;i<=ms;++i)
{
if (i<=efn/2) s1+=svp[efn][i].size();
else s2+=svp[efn][i].size();
sort(all(svp[efn][i]));
}*/
//cout<<n<<' '<<s1<<' '<<s2<<endl;
if (ans.size())
{
out();
return 1;
}
int jed=0;
for (int sz=(efn+1)/2;sz<=ms;++sz) for (auto x: rsv[efn][sz])
{
++jed;
int potr=mul(rem,inv(x.x));
auto it=rsv[efn][efn-sz].find(potr);
if (it==rsv[efn][efn-sz].end()) continue;
//cout<<sz<<en;
//for (auto x: svp[sz][i].y) cout<<x<<' ';
//cout<<en;
//cout<<efn-sz<<en;
//for (auto x: it->y) cout<<x<<' ';
//cout<<en;
ans=zar[sz][x.y];
for (auto x: zar[efn-sz][it->y]) ans.pb(x);
out();
return 1;
}
//cout<<n<<' '<<jed<<endl;
return 0;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>t;
while (t--)
{
cin>>rrem;
if (rrem==1)
{
cout<<1<<en;
continue;
}
if (rrem==2)
{
cout<<2<<en<<"1 2"<<en;
continue;
}
for (n=3;;++n)
{
if (pok()) break;
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 7ms
memory: 23848kb
input:
4 2 360 1 509949433
output:
2 1 2 5 1 2 2 3 2 4 4 5 1 10 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 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:
14 1 2 2 3 2 4 4 5 4 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14