結果

問題 No.995 タピオカオイシクナーレ
ユーザー k_kmhrk_kmhr
提出日時 2020-02-21 21:58:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,872 bytes
コンパイル時間 1,939 ms
コンパイル使用メモリ 176,360 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-07-30 06:24:07
合計ジャッジ時間 3,712 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 40 ms
4,376 KB
testcase_17 AC 39 ms
4,376 KB
testcase_18 AC 40 ms
4,380 KB
testcase_19 AC 40 ms
4,380 KB
testcase_20 AC 40 ms
4,380 KB
testcase_21 AC 40 ms
4,376 KB
testcase_22 AC 40 ms
4,376 KB
testcase_23 AC 39 ms
4,376 KB
testcase_24 AC 39 ms
4,380 KB
testcase_25 AC 40 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0; i<(n); i++)
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

ll myPow(ll x, ll n, ll m){
  if(n == 0)
    return 1;
  if(n % 2 == 0)
    return myPow(x * x % m, n / 2, m);
  else
    return x * myPow(x, n - 1, m) % m;
}

typedef vector<ll> vec;
typedef vector<vec> mat;
const ll m=1e9+7; //mod
//A*B
mat mul(mat &a, mat &b){
    mat c(a.size(), vec(b[0].size()));
    for(ll i=0; i<a.size(); i++){
        for(ll k=0; k<b.size(); k++){
            for(ll j=0; j<b[0].size(); j++){
                c[i][j]=(c[i][j]+a[i][k]*b[k][j])%m;
            }
        }
    }
    return c;
}
//A^n
mat pow(mat a, ll n){
    mat b(a.size(),vec(a.size()));
    for(ll i=0; i<a.size(); i++){
        b[i][i]=1;
    }
    while(n>0){
        if(n&1)b=mul(b,a);
        a=mul(a,a);
        n>>=1;
    }
    return b;
}
//matは mat a(3, vec(3)); のように呼び出す

int main(){
    ll n,m,k,p,q; cin>>n>>m>>k>>p>>q;
    vector<ll> b(n);
    rep(i,n) cin>>b[i];
    ll mod=1e9+7;
    mat prob(2,vec(1));
    prob={{1},{0}};
    mat gyoretu(2, vec(2));
    gyoretu={{q-p,p},{p,q-p}};
    gyoretu=pow(gyoretu,k);
    prob=mul(gyoretu,prob);
    ll res=0;
    rep(i,m) {res+=b[i]*prob[0][0]; res%=mod;}
    for(ll i=m; i<n; i++) {res+=b[i]*prob[1][0]; res%=mod;}
    //x=res; y=q^k; R=x y^-1
    ll y=myPow(q,k,mod);
    ll resy=modinv(y,mod);
    cout<<(res*resy)%mod<<endl;
    return 0;
}
0