# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
19324 | cki86201 | Ω (kriii4_P3) | C++14 | 0 ms | 1816 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <iostream>
#include <functional>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pi;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define sz(x) (int)x.size()
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) x.begin(),x.end()
const ll MOD = 1e9 + 7;
ll pw(ll a,ll b){
a %= MOD;
ll res = 1;
while(b){
if(b&1)res = (res * a) % MOD;
a = (a * a) % MOD;
b >>= 1;
}
return res;
}
ll b[110];
ll M[110][110];
int main(){
int p, q, n, k;
scanf("%d%d%d%d", &p, &q, &n, &k);
if(k == 0)printf("0");
else if(k == n)printf("1");
else{
ll p1 = (q * pw(p, MOD-2)) % MOD;
ll p2 = (MOD + 1 - p1) % MOD;
for(int i=1;i<n;i++){
M[i][i] = 1;
if(i != n)M[i][i+1] = (MOD - p2) % MOD;
if(i != 1)M[i][i-1] = (MOD - p1) % MOD;
}
b[n-1] = p2;
--n;
for(int i=1;i<=n;i++){
ll tmp = pw(M[i][i], MOD-2) % MOD;
for(int j=i;j<=n;j++){
M[i][j] = (M[i][j] * tmp) % MOD;
}
b[i] = (b[i] * tmp) % MOD;
for(int j=i+1;j<=n;j++){
if(M[i][j] == 0)continue;
ll mul = M[j][i];
for(int k=i;k<=n;k++)M[j][k] = ((M[j][k] - mul * M[i][k]) % MOD + MOD) % MOD;
b[j] = ((b[j] - mul * b[i]) % MOD + MOD) % MOD;
}
}
//for(int i=1;i<=n;i++){for(int j=1;j<=n;j++)printf("%lld ", M[i][j]);puts("");}
for(int i=n;i>=1;i--){
for(int j=i-1;j>=1;j--){
b[j] -= b[i] * M[j][i] % MOD;
b[j] = (b[j] + MOD) % MOD;
}
}
printf("%lld", b[k]);
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |