# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
19337 | gs14004 | Ω (kriii4_P3) | C++14 | 5 ms | 1808 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 <math.h>
#include <limits.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <numeric>
#include <deque>
#include <utility>
#include <bitset>
#include <assert.h>
#include <iostream>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;
const int mod = 1e9 + 7;
lint arr[105][105];
int n;
lint ipow(lint x, lint p){
lint ret = 1, piv = x % mod;
while(p){
if(p&1) ret *= piv;
piv *= piv;
ret %= mod;
piv %= mod;
p >>= 1;
}
return ret;
}
vector<lint> gauss_jordan(vector<int> &x){ // solve Ax = B. thanks to 프로그래밍 콘테스트 챌린징
for(int i=0; i<n; i++) arr[i][n] = x[i];
for(int i=0; i<n; i++){
int piv = i;
for(int j=i; j<n; j++){
if(abs(arr[j][i]) > abs(arr[piv][i])){
piv = j;
}
}
swap(x[i], x[piv]);
if(abs(arr[i][i]) == 0){
while(1);
}
for(int j=i+1; j<=n; j++){
arr[i][j] = (arr[i][j] * ipow(arr[i][i], mod - 2)) % mod;
}
for(int j=0; j<n; j++){
if(i != j){
for(int k=i+1; k<=n; k++){
arr[j][k] += mod - arr[j][i] * arr[i][k] % mod;
arr[j][k] %= mod;
}
}
}
}
vector<lint> ret;
for(int i=0; i<n; i++) ret.push_back(arr[i][n]);
return ret;
}
int main(){
int p, q, k;
cin >> p >> q >> n >> k;
vector<int> v(n+1);
v[n] = 1;
arr[0][0] = arr[n][n] = 1;
for(int i=1; i<n; i++){
arr[i][i-1] = q;
arr[i][i] = mod-p;
arr[i][i+1] = p-q;
}
n++;
vector<lint> ret = gauss_jordan(v);
printf("%lld",ret[k]);
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |