QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#319026#7613. Inverse ProblemsuperguymjTL 1ms3768kbC++201.8kb2024-02-01 15:49:452024-02-01 15:49:46

Judging History

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

  • [2024-02-01 15:49:46]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3768kb
  • [2024-02-01 15:49:45]
  • 提交

answer

#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x; i<=y; ++i)
#define repd(i,x,y) for(int i=x; i>=y; --i)
#define mid ((l+r)>>1)
#define lch (rt<<1)
#define rch (rt<<1|1)
#define pb push_back

using namespace std;
typedef long long LL;
typedef __int128 i128;
typedef long double ld;
const int N=2005,mod=1000000007;
int n,a[N],jdg;
LL ans,flv[N],inv[N];

int getint()
{
	char ch;
	int f=1;
	while(!isdigit(ch=getchar())) if(ch=='-') f=-1;
	int x=ch-48;
	while(isdigit(ch=getchar())) x=x*10+ch-48;
	return x*f;
}

LL getLL()
{
	char ch;
	int f=1;
	while(!isdigit(ch=getchar())) if(ch=='-') f=-1;
	LL x=ch-48;
	while(isdigit(ch=getchar())) x=x*10+ch-48;
	return x*f;
}

LL getmi(LL a,LL x)
{
	LL rt=1;
	while(x)
	{
		if(x&1) rt=rt*a%mod;
		a=a*a%mod,x>>=1;
	}
	return rt;
}

LL C(int n,int m)
{
	return n<m?0:flv[n]*inv[m]%mod*inv[n-m]%mod;
}

LL P(int n,int m)
{
	return n<m?0:flv[n]*inv[n-m]%mod;
}

void dfs(int x,int tot,int lst,LL ans)
{
	if(x==n+1)
	{
		if(tot==2*n-2 && ans==::ans) jdg=1;
		return;
	}
	int mn=(2*n-2-tot)/(n-x+1);
	if(mn<lst) return;
	rep(i,lst,mn)
	{
		a[x]=i,dfs(x+1,tot+i,i,ans*P(n-2,i-1)%mod);
		if(jdg) return;
	}
}

void prt(int n,int a[])
{
	vector <int> leaves;
	printf("%d\n",n);
	rep(i,1,n)
	{
		while(a[i]>1) printf("%d %d\n",leaves.back(),i),--a[i],leaves.pop_back();
		leaves.pb(i);
	}
	printf("%d %d\n",leaves[0],leaves[1]);
}				

void solve()
{
	ans=getint();
	if(ans==1)
	{
		puts("1");
		return;
	}
	jdg=0;
	rep(i,2,2000)
	{
		n=i,dfs(1,0,1,n*(n-1ll)%mod);
		if(jdg)
		{
			prt(n,a);
			return;
		}
	}
}

int main()
{
	int T=getint(),n=N-1;
	flv[0]=1;
	rep(i,1,n) flv[i]=flv[i-1]*i%mod;
	inv[n]=getmi(flv[n],mod-2);
	repd(i,n,1) inv[i-1]=inv[i]*i%mod;
	while(T--) solve();
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3768kb

input:

4
2
360
1
509949433

output:

2
1 2
5
3 4
4 5
2 5
1 5
1
10
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
1 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: