Submission #19446

#TimeUsernameProblemLanguageResultExecution timeMemory
19446xhaeΩ (kriii4_P3)C++14
100 / 100
0 ms1724 KiB
#include <iostream> #include <cmath> #include <algorithm> using namespace std; const long long MOD = 1000000007; long long P, Q, N, K; long long p[101], q[101], d[101]; long long inv(long long tar) { long long ret = 1; for(int i = 30; i >= 0; i--) { ret = ret * ret % MOD; if((1ll << i) & (MOD - 2)) ret = ret * tar % MOD; } return ret; } int main(void) { cin >> P >> Q >> N >> K; if(K == 0) printf("0\n"); else if(K == N) printf("1\n"); else { long long gcdv = __gcd(Q, P); long long a = Q / gcdv * inv(P / gcdv) % MOD; gcdv = __gcd(P - Q, P); long long b = (P - Q) / gcdv * inv(P / gcdv) % MOD; p[N - 1] = a, q[N - 1] = b; for(int i = N - 2; i > 0; i--) { long long base = ((1 - b * p[i + 1] % MOD) + MOD) % MOD; p[i] = a * inv(base) % MOD; q[i] = b * q[i + 1] % MOD * inv(base) % MOD; } d[0] = 0; for(int i = 1; i < N; i++) d[i] = (d[i - 1] * p[i] % MOD + q[i]) % MOD; cout << d[K]; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...