Submission #19488

#TimeUsernameProblemLanguageResultExecution timeMemory
19488algoshipdaΩ (kriii4_P3)C++14
100 / 100
18 ms1724 KiB
#include <bits/stdc++.h> using namespace std; typedef long long lld; const int MOD = 1e9 + 7; int fpow(int n, int k) { if(k == 0) return 1; if(k % 2){ return 1ll * n * fpow(n, k - 1) % MOD; } int h = fpow(n, k / 2); return 1ll * h * h % MOD; } int inv(int n) { return 1ll * fpow(n, MOD - 2) % MOD; } inline void mult(int& a, int b) { a = 1ll * a * b % MOD; } int mult2(int a, int b) { return 1ll * a * b % MOD; } inline void sub(int& a, int b) { a = ((a - b) % MOD + MOD) % MOD; } int rational(int a, int b) { return 1ll * a * inv(b) % MOD; } void add(int &a, int b) { a = ((a + b) % MOD + MOD) % MOD; } vector<vector<int>> inverse(const vector<vector<int>>& mat) { vector<vector<int>> I(mat.size(), vector<int>(mat.size(), 0)); vector<vector<int>> Q = mat; int n = mat.size(); for(int i = 0; i < n; ++i) I[i][i] = 1; for(int i = 0; i < n; ++i){ if(Q[i][i] == 0){ for(int j = i + 1; j < n; ++j){ if(Q[j][i] != 0){ for(int k = 0; k < n; ++k){ add(Q[i][k], Q[j][k]); add(I[i][k], I[j][k]); } break; } } } int b = inv(Q[i][i]); for(int j = 0; j < n; ++j){ mult(Q[i][j], b); mult(I[i][j], b); } for(int j = 0; j < n; ++j){ if(i == j) continue; int w = Q[j][i]; for(int k = 0; k < n; ++k){ sub(Q[j][k], mult2(Q[i][k], w)); sub(I[j][k], mult2(I[i][k], w)); } } } return I; } vector<vector<int>> mult(const vector<vector<int>>& a, const vector<vector<int>>& b) { vector<vector<int>> ret(a.size(), vector<int>(b[0].size())); for(int i = 0; i < a.size(); ++i){ for(int j = 0; j < b[0].size(); ++j){ int sum = 0; for(int k = 0; k < a[0].size(); ++k){ add(sum, (1ll * a[i][k] * b[k][j]) % MOD); } ret[i][j] = sum; } } return ret; } int main() { int p, q, n, k; cin >> p >> q >> n >> k; if(k == 0){ cout << 0 << '\n'; return 0; } if(k == n){ cout << 1 << '\n'; return 0; } vector<vector<int>> Q(n - 1, vector<int>(n - 1, 0)); for(int i = 0; i + 1 < Q.size(); ++i){ Q[i][i+1] = rational(p - q,p); Q[i+1][i] = rational(q, p); } for(int i = 0; i < Q.size(); ++i){ for(int j = 0; j < Q.size(); ++j){ Q[i][j] = ((i == j) - Q[i][j] + MOD) % MOD; } } vector<vector<int>> N = inverse(Q); vector<vector<int>> R(n - 1, vector<int>(2, 0)); R[0][0] = rational(q, p); R[n - 2][1] = rational(p - q, p); vector<vector<int>> B = mult(N, R); cout << B[k - 1][1] << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...