Submission #19498

# Submission time Handle Problem Language Result Execution time Memory
19498 2016-02-24T14:15:04 Z algoshipda Ω (kriii4_P3) C++14
100 / 100
18 ms 1724 KB
#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){
						swap(Q[i][k], Q[j][k]);
						swap(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 time Memory Grader output
1 Correct 0 ms 1724 KB Output is correct
2 Correct 0 ms 1724 KB Output is correct
3 Correct 0 ms 1724 KB Output is correct
4 Correct 0 ms 1724 KB Output is correct
5 Correct 0 ms 1724 KB Output is correct
6 Correct 0 ms 1724 KB Output is correct
7 Correct 0 ms 1724 KB Output is correct
8 Correct 0 ms 1724 KB Output is correct
9 Correct 0 ms 1724 KB Output is correct
10 Correct 0 ms 1724 KB Output is correct
11 Correct 10 ms 1724 KB Output is correct
12 Correct 0 ms 1724 KB Output is correct
13 Correct 9 ms 1724 KB Output is correct
14 Correct 17 ms 1724 KB Output is correct
15 Correct 0 ms 1724 KB Output is correct
16 Correct 14 ms 1724 KB Output is correct
17 Correct 0 ms 1724 KB Output is correct
18 Correct 16 ms 1724 KB Output is correct
19 Correct 0 ms 1724 KB Output is correct
20 Correct 18 ms 1724 KB Output is correct
21 Correct 0 ms 1724 KB Output is correct
22 Correct 13 ms 1724 KB Output is correct
23 Correct 0 ms 1724 KB Output is correct
24 Correct 3 ms 1724 KB Output is correct
25 Correct 8 ms 1724 KB Output is correct
26 Correct 6 ms 1724 KB Output is correct
27 Correct 0 ms 1724 KB Output is correct
28 Correct 4 ms 1724 KB Output is correct
29 Correct 0 ms 1724 KB Output is correct
30 Correct 18 ms 1724 KB Output is correct