# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
19246 | kaTkaHr | Ω (kriii4_P3) | C++14 | 31 ms | 1428 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 <algorithm>
#include <map>
using namespace std;
typedef long long ll;
const int MX = 105, MM = 1000000007;
ll rv(ll A){
ll R = 1, B = MM-2;
while(B){
if( B&1 ) R = R * A % MM;
A = A * A % MM; B /= 2;
}
return R;
}
struct frac{
ll A, B;
frac(ll A, ll B):A(A), B(B){}
frac(){A = 0, B = 1;}
frac operator+ (const frac &l)const{
return frac((A * l.B + B * l.A) % MM, B * l.B % MM);
}
frac operator*(const frac &l)const{
return frac(A*l.A % MM, B*l.B % MM);
}
frac operator/(const frac &l)const{
return frac(A*l.B % MM, B*l.A % MM);
}
frac operator- (const frac &l)const{
return frac((A*l.B - B*l.A%MM + MM) % MM, B*l.B % MM);
}
ll v(){ return A * rv(B) % MM; }
};
frac D[MX][MX], I[MX][MX];
int P, Q, N, K;
void print()
{
for(int i = 1; i <= N; i++){
for(int j = 1; j <= N; j++){
printf("%lld,%lld ", D[i][j].A, D[i][j].B);
}
printf("\n");
}
printf("\n");
}
int main()
{
scanf("%d%d%d%d", &P, &Q, &N, &K);
for(int i = 0; i <= N; i++){
D[i][i] = I[i][i] = frac(1, 1);
if( i <= N-2 ) D[i][i+1] = frac(MM-Q, P);
if( i >= 2) D[i][i-1] = frac(MM-P+Q, P);
}
for(int i = 0; i <= N; i++){
for(int j = i+1; j <= N; j++){
frac p = D[j][i] / D[i][i];
for(int k = 0; k <= N; k++){
D[j][k] = D[j][k] - D[i][k] * p;
I[j][k] = I[j][k] - I[i][k] * p;
}
}
}
for(int i = N; i >= 0; i--){
for(int j = i-1; j >= 0; j--){
frac p = D[j][i] / D[i][i];
for(int k = N; k >= 0; k--){
D[j][k] = D[j][k] - D[i][k] * p;
I[j][k] = I[j][k] - I[i][k] * p;
}
}
}
for(int i = 0; i <= N; i++){
frac p = frac(1, 1) / D[i][i];
for(int j = 0; j <= N; j++){
D[i][j] = D[i][j] * p;
I[i][j] = I[i][j] * p;
}
}
printf("%lld", I[N][K].v());
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |