Submission #19424

# Submission time Handle Problem Language Result Execution time Memory
19424 2016-02-24T12:05:08 Z Qwaz Ω (kriii4_P3) C++14
100 / 100
0 ms 1084 KB
#include <cstdio>

typedef long long ll;
const int MOD = 1000000007, MAX = 110;;

ll modpow(ll a, ll x) {
	ll ret = 1;
	a = a % MOD;

	while (x) {
		if (x & 1) ret = ret * a % MOD;
		a = a * a % MOD;
		x >>= 1;
	}

	return ret;
}

int p, q, n, k;

ll coeff[MAX], constant[MAX], val[MAX];

int main() {
	scanf("%d%d%d%d", &p, &q, &n, &k);

	if (k == 0) puts("0");
	else if (k == n) puts("1");
	else {
		ll x = q * modpow(p, MOD-2) % MOD;
		ll y = 1 - x;
		if (y < 0) y += MOD;

		coeff[n] = 0;
		constant[n] = 1;
		for (int i = n-1; i > 0; i--) {
			ll div = 1 - y * coeff[i+1];
			div %= MOD;
			if (div < 0) div += MOD;
			div = modpow(div, MOD-2);

			coeff[i] = x * div % MOD;
			constant[i] = (y * constant[i+1] % MOD) * div % MOD;
		}

		val[0] = 0;
		val[1] = constant[1];
		for (int i = 2; i < n; i++) {
			val[i] = (coeff[i] * val[i-1] + constant[i]) % MOD;
		}

		printf("%lld\n", val[k]);
	}

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1084 KB Output is correct
2 Correct 0 ms 1084 KB Output is correct
3 Correct 0 ms 1084 KB Output is correct
4 Correct 0 ms 1084 KB Output is correct
5 Correct 0 ms 1084 KB Output is correct
6 Correct 0 ms 1084 KB Output is correct
7 Correct 0 ms 1084 KB Output is correct
8 Correct 0 ms 1084 KB Output is correct
9 Correct 0 ms 1084 KB Output is correct
10 Correct 0 ms 1084 KB Output is correct
11 Correct 0 ms 1084 KB Output is correct
12 Correct 0 ms 1084 KB Output is correct
13 Correct 0 ms 1084 KB Output is correct
14 Correct 0 ms 1084 KB Output is correct
15 Correct 0 ms 1084 KB Output is correct
16 Correct 0 ms 1084 KB Output is correct
17 Correct 0 ms 1084 KB Output is correct
18 Correct 0 ms 1084 KB Output is correct
19 Correct 0 ms 1084 KB Output is correct
20 Correct 0 ms 1084 KB Output is correct
21 Correct 0 ms 1084 KB Output is correct
22 Correct 0 ms 1084 KB Output is correct
23 Correct 0 ms 1084 KB Output is correct
24 Correct 0 ms 1084 KB Output is correct
25 Correct 0 ms 1084 KB Output is correct
26 Correct 0 ms 1084 KB Output is correct
27 Correct 0 ms 1084 KB Output is correct
28 Correct 0 ms 1084 KB Output is correct
29 Correct 0 ms 1084 KB Output is correct
30 Correct 0 ms 1084 KB Output is correct