Submission #1993792


Source Code Expand

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;

const int N=5005;
const int mod=1000000007;

int n,ans;
int dp[2][N][N],d[N],fac[N],inv[N];

int quickpow(int x,int y){
	int s=1;
	for (;y;y>>=1,x=1ll*x*x%mod)
		if (y&1) s=1ll*s*x%mod;
	return s;
}

void init(){
	int i;
	fac[0]=inv[0]=1;
	for (i=1;i<=n;i++) fac[i]=1ll*fac[i-1]*i%mod;
	inv[n]=quickpow(fac[n],mod-2);
	for (i=n-1;i;i--) inv[i]=1ll*inv[i+1]*(i+1)%mod;
}

int main(){
	int i,j,k,now,pre,i2;
	scanf("%d",&n);
	for (i=1;i<=n;i++) scanf("%d",&d[i]);
	init();
	dp[0][0][0]=1; now=0; pre=1;
	for (i=1;i<=n;i++){
		now^=1; pre^=1;
		for (j=0;j<=n+n;j++)
			for (k=0;k<i;k++){
				if (!dp[pre][j][k]) continue;
				(dp[now][j][k]+=1ll*dp[pre][j][k]*inv[d[i]-1]%mod)%=mod;
				if (d[i]>1&&j+d[i]-2<=n+n)
				(dp[now][j+d[i]-2][k+1]+=1ll*dp[pre][j][k]*inv[d[i]-2]%mod)%=mod;
			}
		memset(dp[pre],0,sizeof(dp[pre]));
	}
	i2=quickpow(2,mod-2);
	for (j=0;j<=n+n;j++)
		for (k=3;k<=n;k++){
			if (!dp[now][j][k]) continue;
			if (k<n&&j==0) continue;
			if (k==n&&j==0){
				(ans+=1ll*i2*dp[now][j][k]%mod*fac[k-1]%mod)%=mod;
				continue;
			}
			(ans+=1ll*i2*dp[now][j][k]%mod*fac[k-1]%mod*fac[n-k-1]%mod*j%mod)%=mod;
		}
	printf("%d\n",ans);
	return 0;
}

Submission Info

Submission Time
Task F - Unicyclic Graph Counting
User wdyhy
Language C++14 (GCC 5.4.1)
Score 700
Code Size 1349 Byte
Status TLE
Exec Time 2104 ms
Memory 195968 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:32:16: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
                ^
./Main.cpp:33:38: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  for (i=1;i<=n;i++) scanf("%d",&d[i]);
                                      ^

Judge Result

Set Name Sample Subtask1 Subtask2 Subtask3 All
Score / Max Score 0 / 0 200 / 200 200 / 200 300 / 300 0 / 300
Status
AC × 2
AC × 9
AC × 18
AC × 28
AC × 31
TLE × 8
Set Name Test Cases
Sample 00_example_01.txt, 00_example_02.txt
Subtask1 00_example_01.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s1_07.txt, s1_08.txt
Subtask2 00_example_01.txt, 00_example_02.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s1_07.txt, s1_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt, s2_13.txt, s2_14.txt, s2_15.txt, s2_16.txt
Subtask3 00_example_01.txt, 00_example_02.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s1_07.txt, s1_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt, s2_13.txt, s2_14.txt, s2_15.txt, s2_16.txt, s3_17.txt, s3_18.txt, s3_19.txt, s3_20.txt, s3_21.txt, s3_22.txt, s3_23.txt, s3_24.txt, s3_25.txt, s3_26.txt
All 00_example_01.txt, 00_example_02.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s1_07.txt, s1_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt, s2_13.txt, s2_14.txt, s2_15.txt, s2_16.txt, s3_17.txt, s3_18.txt, s3_19.txt, s3_20.txt, s3_21.txt, s3_22.txt, s3_23.txt, s3_24.txt, s3_25.txt, s3_26.txt, s4_27.txt, s4_28.txt, s4_29.txt, s4_30.txt, s4_31.txt, s4_32.txt, s4_33.txt, s4_34.txt, s4_35.txt, s4_36.txt, s4_37.txt
Case Name Status Exec Time Memory
00_example_01.txt AC 83 ms 195968 KB
00_example_02.txt AC 209 ms 195968 KB
s1_01.txt AC 60 ms 195968 KB
s1_02.txt AC 72 ms 195968 KB
s1_03.txt AC 72 ms 195968 KB
s1_04.txt AC 83 ms 195968 KB
s1_05.txt AC 83 ms 195968 KB
s1_06.txt AC 83 ms 195968 KB
s1_07.txt AC 72 ms 195968 KB
s1_08.txt AC 83 ms 195968 KB
s2_09.txt AC 208 ms 195968 KB
s2_10.txt AC 219 ms 195968 KB
s2_11.txt AC 230 ms 195968 KB
s2_12.txt AC 231 ms 195968 KB
s2_13.txt AC 219 ms 195968 KB
s2_14.txt AC 230 ms 195968 KB
s2_15.txt AC 232 ms 195968 KB
s2_16.txt AC 229 ms 195968 KB
s3_17.txt AC 431 ms 195968 KB
s3_18.txt AC 431 ms 195968 KB
s3_19.txt AC 488 ms 195968 KB
s3_20.txt AC 420 ms 195968 KB
s3_21.txt AC 263 ms 195968 KB
s3_22.txt AC 589 ms 195968 KB
s3_23.txt AC 589 ms 195968 KB
s3_24.txt AC 591 ms 195968 KB
s3_25.txt AC 597 ms 195968 KB
s3_26.txt AC 588 ms 195968 KB
s4_27.txt AC 1743 ms 195968 KB
s4_28.txt AC 1105 ms 195968 KB
s4_29.txt TLE 2104 ms 195968 KB
s4_30.txt AC 1413 ms 195968 KB
s4_31.txt TLE 2104 ms 195968 KB
s4_32.txt TLE 2104 ms 195968 KB
s4_33.txt TLE 2104 ms 195968 KB
s4_34.txt TLE 2104 ms 195968 KB
s4_35.txt TLE 2104 ms 195968 KB
s4_36.txt TLE 2104 ms 195968 KB
s4_37.txt TLE 2104 ms 195968 KB