QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#227625 | #7613. Inverse Problem | ucup-team1004 | TL | 0ms | 0kb | C++14 | 4.3kb | 2023-10-27 20:06:44 | 2023-10-27 20:06:44 |
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<typename T>
ostream& operator << (ostream &out,const vector<T> &x){
if(x.empty())return out<<"[]";
out<<'['<<x[0];
for(int i=1,len=x.size();i<len;i++)out<<','<<x[i];
return out<<']';
}
template<typename T1,typename T2,typename T3>
ostream& operator << (ostream &out,const tuple<T1,T2,T3> &x){
return out<<'('<<get<0>(x)<<','<<get<1>(x)<<','<<get<2>(x)<<')';
}
template<typename T>
vector<T> ary(const T *a,int l,int r){
return vector<T>{a+l,a+1+r};
}
template<typename T>
void debug(T x){
cerr<<x<<endl;
}
template<typename T,typename ... S>
void debug(T x,S...y){
cerr<<x<<' ',debug(y...);
}
const int N=150,M=15,mod=1e9+7;
int n,m,cnt,goal[N];
pair<int,vector<int> >ans[N];
int fac[N],ifac[N],inv[N];
ll qpow(ll x,ll y=mod-2){
ll ans=1;
for(;y;(x*=x)%=mod,y>>=1)if(y&1)(ans*=x)%=mod;
return ans;
}
ll f[N][N][N],g[N][N][N],F[N][N],G[N][N];
void init(int n=N-1){
for(int i=inv[1]=fac[0]=1;i<=n;i++)fac[i]=1ll*fac[i-1]*i%mod;
for(int i=2;i<=n;i++)inv[i]=1ll*inv[mod%i]*(mod-mod/i)%mod;
for(int i=ifac[0]=1;i<=n;i++)ifac[i]=1ll*ifac[i-1]*inv[i]%mod;
for(int lim=1;lim<=n;lim++){
f[lim][0][0]=1;
for(int r=1;r<=lim;r++){
memcpy(f[lim][r],f[lim][r-1],sizeof f[lim][r]);
for(int j=r;j<=lim;j++){
f[lim][r][j]+=f[lim][r][j-r];
}
F[lim][r]=accumulate(f[lim][r],f[lim][r]+1+lim,0ll);
}
}
for(int lim=1;lim<=n;lim++){
g[lim][lim+1][0]=1;
for(int r=lim;r>=1;r--){
memcpy(g[lim][r],g[lim][r+1],sizeof g[lim][r]);
for(int j=r;j<=lim;j++){
g[lim][r][j]+=g[lim][r][j-r];
}
G[lim][r]=accumulate(g[lim][r],g[lim][r]+1+lim,0ll);
}
}
// for(int lim=1;lim<=n;lim++){
// debug("F",ary(F[lim],1,lim));
// debug("G",ary(G[lim],1,lim));
// }
}
int P(int n,int m){
if(0>m||m>n)return 0;
return 1ll*fac[n]*ifac[n-m]%mod;
}
int iP(int n,int m){
if(0>m||m>n)return 0;
return 1ll*ifac[n]*fac[n-m]%mod;
}
int val[N],ival[N];
vector<pair<int,vector<int> > >S[N],T[N];
vector<int>now;
void dfs1(int n,int l,int r,int sum,int mul){
if(sum<n&&sum+l>n)return;
if(l>r){
return S[sum].push_back({mul,now});
}
dfs1(n,l+1,r,sum,mul);
if(sum+l<=n){
now.push_back(l);
dfs1(n,l,r,sum+l,1ll*mul*ival[l]%mod);
now.pop_back();
}
}
void dfs2(int n,int l,int r,int sum,int mul){
if(sum<n&&sum+l>n)return;
if(l>r){
return T[sum].push_back({mul,now});
}
dfs2(n,l+1,r,sum,mul);
if(sum+l<=n){
now.push_back(l);
dfs2(n,l,r,sum+l,1ll*mul*val[l]%mod);
now.pop_back();
}
}
bitset<mod>vis;
int t3;
void solve(int n){
clock_t st=clock();
int B=0;
ll mn=1e18;
for(int i=1;i<n;i++){
ll now=max(F[n][i]*cnt,G[n][i+1]);
if(now<mn)mn=now,B=i;
}
for(int i=1;i<=n;i++)val[i]=P(n,i),ival[i]=iP(n,i);
for(int i=0;i<=n;i++)T[i].clear(),S[i].clear();
dfs1(n,1,B,0,1);
dfs2(n,B+1,n,0,1);
// debug(ary(val,1,n),ary(ival,1,n));
// sort(S.begin(),S.end());
// sort(T.begin(),T.end());
// debug(S),debug(T);
int t1=0,t2=0,las=cnt;
for(int l=0;l<=n;l++){
int r=n-l;
for(auto &Tx:T[r]){
vis[Tx.first]=1;
t2++;
}
for(int i=1;i<=m;i++)if(!ans[i].first){
int x=1ll*goal[i]*inv[n+1]%mod*inv[n+2]%mod;
for(auto &Sx:S[l]){
int mul=1ll*x*Sx.first%mod;
t1++;
if(!vis[mul])continue;
for(auto &Tx:T[r]){
t3++;
if(Tx.first!=mul)continue;
ans[i].first=n+2;
vector<int>res(Sx.second);
for(int x:Tx.second)res.push_back(x);
res[0]++;
for(;res.size()<n+2;res.push_back(0));
ans[i].second=res,cnt--;
debug("ok",i,ans[i].first,ans[i].second);
break;
}
break;
}
}
for(auto &Tx:T[r]){
vis[Tx.first]=0;
}
}
debug("n",1.0*(clock()-st)/CLOCKS_PER_SEC,n,t1,t2,t3,F[n][B]*las,G[n][B+1]);
}
int main(){
cin>>m,init();
for(int i=1;i<=m;i++)cin>>goal[i];
cnt=m;
for(int i=1;i<=m;i++){
if(goal[i]==1)cnt--,ans[i]={1,{0}};
else if(goal[i]==2)cnt--,ans[i]={2,{1,0}};
if(ans[i].first){
debug("ok",i,ans[i].first,ans[i].second);
}
}
for(n=3;cnt;n++)solve(n-2);
for(int i=1;i<=m;i++){
// debug(ans[i].first,ans[i].second);
int n=ans[i].first;
printf("%d\n",n);
for(int j=0,x=1;j<n;j++){
for(;ans[i].second[j]--;){
printf("%d %d\n",j+1,x+++1);
}
}
}
debug(1.0*clock()/CLOCKS_PER_SEC);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
4 2 360 1 509949433