Submission #826572


Source Code Expand

#include<cstdio>
#include<algorithm>

using namespace std;

const long long mod = 1000000007;

long long fact[4000400];
long long ifact[4000400];
long long invs[4000400];

const int MA = 4000300;

void init(){
	//factorial
	invs[1] = 1;
	for(int i = 2; i < MA; ++i){
		long long k = mod / i;
		long long l = mod % i;
		long long tmp = -k * invs[l];
		tmp %= mod;
		if(tmp < 0) tmp += mod;
		invs[i] = tmp;
	}
	
	fact[0] = 1;
	ifact[0] = 1;
	for(int i = 1; i < MA; ++i){
		fact[i] = (fact[i - 1] * i) % mod;
		ifact[i] = (ifact[i - 1] * invs[i]) % mod;
	}
}

long long C(int n, int k){
	if(k < 0 || k > n) return 0;
	long long res = fact[n];
	res *= ifact[k];
	res %= mod;
	res *= ifact[n - k];
	res %= mod;
	return res;
}

int N, K;

long long dp[2020][2020];
long long sum[2020][2020];

long long solve(){
	if(K == 1) return 1;
	for(int i = 0; i <= N; ++i){
		for(int j = 0; j <= N; ++j){
			dp[i][j] = 0;
			sum[i][j] = 0;
		}
	}
	dp[N][1] = 1;
	sum[N][1] = 1;
	sum[N][0] = 1;
	for(int i = N - 1; i >= 1; --i){
		for(int j = 1; j <= N; ++j){
			int d = N - i;
			if(j > d + 1){
				dp[i][j] = 0;
				continue;
			}
			int num = d * K - (j - 1);
			int cur = K - 1;
			long long coe = C(num + cur - 1, cur - 1);
			dp[i][j] += dp[i + 1][j - 1] * coe;
			dp[i][j] %= mod;
			
			coe = C(num + cur - 1, cur - 1);
			dp[i][j] += sum[i + 1][j] * coe;
			dp[i][j] %= mod;
		}
		sum[i][N] = dp[i][N];
		for(int j = N - 1; j >= 0; --j){
			sum[i][j] = (sum[i][j + 1] + dp[i][j]) % mod;
		}
	}
	long long ans = 0;
	for(int i = 0; i <= N; ++i){
		ans += dp[1][i];
		ans %= mod;
	}
	ans *= fact[N];
	ans %= mod;
	return ans;
}

int main(){
	scanf("%d%d", &N, &K);
	init();
	long long ans = solve();
	printf("%lld\n", ans);
	return 0;
}

Submission Info

Submission Time
Task F - Leftmost Ball
User wo01
Language C++14 (GCC 5.4.1)
Score 1600
Code Size 1825 Byte
Status AC
Exec Time 636 ms
Memory 157056 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:93:23: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &K);
                       ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1600 / 1600
Status
AC × 4
AC × 24
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt
Case Name Status Exec Time Memory
0_00.txt AC 400 ms 93952 KB
0_01.txt AC 413 ms 93952 KB
0_02.txt AC 416 ms 93952 KB
0_03.txt AC 633 ms 157056 KB
1_00.txt AC 417 ms 93952 KB
1_01.txt AC 400 ms 93952 KB
1_02.txt AC 417 ms 93952 KB
1_03.txt AC 615 ms 157056 KB
1_04.txt AC 598 ms 155776 KB
1_05.txt AC 636 ms 156928 KB
1_06.txt AC 636 ms 156160 KB
1_07.txt AC 620 ms 154368 KB
1_08.txt AC 618 ms 154624 KB
1_09.txt AC 593 ms 154752 KB
1_10.txt AC 599 ms 154624 KB
1_11.txt AC 629 ms 156032 KB
1_12.txt AC 428 ms 102656 KB
1_13.txt AC 582 ms 150144 KB
1_14.txt AC 425 ms 95232 KB
1_15.txt AC 471 ms 112640 KB
1_16.txt AC 508 ms 125696 KB
1_17.txt AC 605 ms 151296 KB
1_18.txt AC 425 ms 98176 KB
1_19.txt AC 635 ms 156544 KB