QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#232519 | #7613. Inverse Problem | AFewSuns | AC ✓ | 20241ms | 203900kb | C++14 | 3.5kb | 2023-10-30 15:52:36 | 2023-10-30 15:52:37 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
namespace my_std{
#define ll long long
#define bl bool
ll my_pow(ll a,ll b,ll mod){
ll res=1;
if(!b) return 1;
while(b){
if(b&1) res=(res*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return res;
}
ll qpow(ll a,ll b){
ll res=1;
if(!b) return 1;
while(b){
if(b&1) res*=a;
a*=a;
b>>=1;
}
return res;
}
#define db double
#define pf printf
#define pc putchar
#define fr(i,x,y) for(register ll i=(x);i<=(y);i++)
#define pfr(i,x,y) for(register ll i=(x);i>=(y);i--)
#define go(u) for(ll i=head[u];i;i=e[i].nxt)
#define enter pc('\n')
#define space pc(' ')
#define fir first
#define sec second
#define MP make_pair
#define il inline
#define inf 8e18
#define random(x) rand()*rand()%(x)
#define inv(a,mod) my_pow((a),(mod-2),(mod))
il ll read(){
ll sum=0,f=1;
char ch=0;
while(!isdigit(ch)){
if(ch=='-') f=-1;
ch=getchar();
}
while(isdigit(ch)){
sum=sum*10+(ch^48);
ch=getchar();
}
return sum*f;
}
il void write(ll x){
if(x<0){
x=-x;
pc('-');
}
if(x>9) write(x/10);
pc(x%10+'0');
}
il void writeln(ll x){
write(x);
enter;
}
il void writesp(ll x){
write(x);
space;
}
}
using namespace my_std;
#define mod 1000000007
vector<pair<ll,ll> > ans[11];
vector<ll> vec1[220],vec2[220];
ll q,n=2,a[11],f[220],ff[220],b[220],cntb,c[220],cntc,d[220];
void dfs1(ll now,ll sum,ll mul){
vec1[sum].push_back(mul);
fr(i,now,min(5ll,n-2-sum)) dfs1(i,sum+i,mul*f[i]%mod);
}
void dfs2(ll now,ll sum,ll mul){
vec2[sum].push_back(mul);
fr(i,now,n-2-sum) dfs2(i,sum+i,mul*ff[i]%mod);
}
bl dfs3(ll now,ll sum,ll mul,ll nsum,ll nmul){
if(nsum==sum&&nmul==mul) return 1;
if(sum>=nsum) return 0;
fr(i,now,min(5ll,n-2-sum)){
b[++cntb]=i;
if(dfs3(i,sum+i,mul*f[i]%mod,nsum,nmul)) return 1;
cntb--;
}
return 0;
}
bl dfs4(ll now,ll sum,ll mul,ll nsum,ll nmul){
if(nsum==sum&&nmul==mul) return 1;
if(sum>=nsum) return 0;
fr(i,now,n-2-sum){
c[++cntc]=i;
if(dfs4(i,sum+i,mul*ff[i]%mod,nsum,nmul)) return 1;
cntc--;
}
return 0;
}
il void solve(){
// pf("%lld start\n",n);
ll tmp=1;
fr(i,0,n-2){
f[i]=tmp;
tmp=tmp*(n-2-i)%mod;
}
fr(i,0,n-2) ff[i]=inv(f[i],mod);
dfs1(1,0,1);
dfs2(6,0,1);
fr(i,0,n-2) sort(vec2[i].begin(),vec2[i].end());
fr(i,1,q){
if(!ans[i].empty()) continue;
tmp=n*(n-1)%mod*inv(a[i],mod)%mod;
fr(j,0,n-2){
ll res1=-1,res2=-1;
fr(k,0,(ll)vec1[j].size()-1){
ll now=vec1[j][k]*tmp%mod,pos=lower_bound(vec2[n-2-j].begin(),vec2[n-2-j].end(),now)-vec2[n-2-j].begin();
if(pos<(ll)vec2[n-2-j].size()&&vec2[n-2-j][pos]==now){
res1=vec1[j][k];
res2=now;
break;
}
}
if(res1==-1) continue;
// pf("query #%lld done\n",i);
cntb=cntc=0;
dfs3(1,0,1,j,res1);
dfs4(6,0,1,n-2-j,res2);
fr(k,1,n) d[k]=0;
fr(k,1,cntb) d[k]=b[k];
fr(k,1,cntc) d[cntb+k]=c[k];
ll now=1;
fr(k,1,n){
while(d[k]--) ans[i].push_back(MP(k,++now));
if(k==1) ans[i].push_back(MP(k,++now));
}
break;
}
}
fr(i,0,n-2) vec1[i].clear();
fr(i,0,n-2) vec2[i].clear();
// pf("%lld end\n",n);
}
int main(){
q=read();
fr(i,1,q){
a[i]=read();
if(a[i]==1) ans[i].push_back(MP(0,0));
}
while(1){
bl ck=0;
fr(i,1,q) if(ans[i].empty()) ck=1;
if(!ck) break;
solve();
n++;
}
fr(i,1,q){
if(!ans[i][0].fir) writeln(1);
else{
writeln((ll)ans[i].size()+1);
fr(j,0,(ll)ans[i].size()-1) pf("%lld %lld\n",ans[i][j].fir,ans[i][j].sec);
}
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3752kb
input:
4 2 360 1 509949433
output:
2 1 2 5 1 2 1 3 2 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: 0
Accepted
time: 15830ms
memory: 198152kb
input:
9 185396120 468170792 837583517 696626231 338497514 762842660 800028852 928391161 733524004
output:
14 1 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 9 12 10 13 10 14 122 1 2 1 3 2 4 3 5 4 6 5 7 6 8 6 9 7 10 7 11 8 12 8 13 9 14 9 15 9 16 10 17 10 18 10 19 11 20 11 21 11 22 12 23 12 24 12 25 12 26 13 27 13 28 13 29 13 30 14 31 14 32 14 33 14 34 14 35 15 36 15 37 15 38 15 39 15 40 15 41 15 42 16 43 16 44...
result:
ok OK (9 test cases)
Test #3:
score: 0
Accepted
time: 20241ms
memory: 203900kb
input:
10 338497514 733524004 447182954 415993605 453460670 50499055 648088551 986982752 907925397 315315230
output:
124 1 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 11 13 11 14 12 15 12 16 12 17 13 18 13 19 13 20 13 21 13 22 14 23 14 24 14 25 14 26 14 27 14 28 15 29 15 30 15 31 15 32 15 33 15 34 15 35 16 36 16 37 16 38 16 39 16 40 16 41 16 42 17 43 17 44 17 45 17 46 17 47 17 48 17 49 18 50 18 51 18 52 18 53 18...
result:
ok OK (10 test cases)
Test #4:
score: 0
Accepted
time: 2643ms
memory: 38308kb
input:
10 1 2 3 4 5 6 7 8 9 10
output:
1 2 1 2 102 1 2 1 3 2 4 3 5 4 6 4 7 5 8 5 9 6 10 6 11 7 12 7 13 8 14 8 15 9 16 9 17 9 18 9 19 9 20 9 21 10 22 10 23 10 24 10 25 10 26 10 27 10 28 10 29 11 30 11 31 11 32 11 33 11 34 11 35 11 36 11 37 11 38 12 39 12 40 12 41 12 42 12 43 12 44 12 45 12 46 12 47 12 48 13 49 13 50 13 51 13 52 13 53 13 5...
result:
ok OK (10 test cases)
Test #5:
score: 0
Accepted
time: 642ms
memory: 15340kb
input:
10 269199917 392009324 753889928 751355133 472639410 132096559 331333826 40414701 72847302 475706026
output:
55 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 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 84 1 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 ...
result:
ok OK (10 test cases)
Extra Test:
score: 0
Extra Test Passed