結果

問題 No.1681 +-*
ユーザー nawawannawawan
提出日時 2021-09-17 22:04:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 3,070 bytes
コンパイル時間 1,929 ms
コンパイル使用メモリ 202,572 KB
実行使用メモリ 14,472 KB
最終ジャッジ日時 2023-09-12 07:40:53
合計ジャッジ時間 4,412 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
12,908 KB
testcase_01 AC 6 ms
12,908 KB
testcase_02 AC 6 ms
13,004 KB
testcase_03 AC 6 ms
12,968 KB
testcase_04 AC 6 ms
13,024 KB
testcase_05 AC 65 ms
14,236 KB
testcase_06 AC 65 ms
14,428 KB
testcase_07 AC 66 ms
14,104 KB
testcase_08 AC 101 ms
14,104 KB
testcase_09 AC 102 ms
14,204 KB
testcase_10 AC 102 ms
14,280 KB
testcase_11 AC 102 ms
14,216 KB
testcase_12 AC 101 ms
14,300 KB
testcase_13 AC 53 ms
13,512 KB
testcase_14 AC 101 ms
14,108 KB
testcase_15 AC 5 ms
12,996 KB
testcase_16 AC 5 ms
13,048 KB
testcase_17 AC 102 ms
14,160 KB
testcase_18 AC 101 ms
14,472 KB
testcase_19 AC 101 ms
14,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<const int MOD> struct modint{
    long long val;
    modint(): val(0) {}
    modint(long long x){
        if(x < 0) val = x % MOD + MOD;
        else val = x % MOD;
    }
    modint(const modint &t){
        val = t.val;
    }
    modint& operator =(const modint m){
        val = m.val;
        return *this;
    }
    modint operator -(){
        return modint(-val);
    }
    modint& operator-=(const modint &m){
        val -= m.val;
        if(val < 0) val += MOD;
        return *this;
    }
    modint& operator+=(const modint &m){
        val += m.val;
        if(val >= MOD) val -= MOD;
        return *this;
    }
    modint& operator*=(const modint &m){
        val *= m.val;
        val %= MOD;
        return *this;
    }
    modint& operator/=(modint m){
        *this *= m.inv();
        return *this;
    }
    modint inv(){
        long long x = 1, y = 0;
        long long a = val, b = MOD;
        while(b != 0){
            long long t = a / b;
            a -= t * b;
            x -= t * y;
            swap(a, b);
            swap(x, y);
        }
        x %= MOD;
        if(x < 0) x += MOD;
        return modint(x);
    }
    modint pow(long long k){
        long long res = 1;
        long long v = val;
        while(k > 0){
            if(k & 1) res = res * v % MOD;
            v = v * v % MOD;
            k >>= 1;
    }
        return modint(res);
    }
    bool operator==(const modint &m){
        return val == m.val;
    }
    modint operator+(const modint &m){
        return modint(*this) += m;
    }
    modint operator-(const modint &m){
        return modint(*this) -= m;
    }
    modint operator*(const modint &m){
        return modint(*this) *= m;
    }
    modint operator/(const modint &m){
        return modint(*this) /= m;
    }
    bool operator!=(const modint &m){
        return modint(*this).val != m.val;
    }
    bool operator!=(const int &m){
        return modint(*this).val != m;
    }
};
const int MOD = 1000000007;
using mint = modint<MOD>;
const int MAX = 410000;
mint fac[MAX], finv[MAX], inv[MAX];
void COMinit(){
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i;
        inv[i] = mint(MOD) - inv[MOD % i] * (MOD / i);
        finv[i] = finv[i - 1] * inv[i];
    }
}
mint COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * finv[k] * finv[n - k];
}
mint repow(long long x, long long y){
    if(y == 0) return 1;
    mint res = 1;
    mint x2 = x;
    while(y > 0){
        if(y & 1) res = res * x2;
        x2 = x2 * x2;
        y >>= 1;
    }
    return res;
}
int main(){
    int N;
    cin >> N;
    vector<long long> A(N);
    for(int i = 0; i < N; i++) cin >> A[i];
    mint sum = A[0];
    mint ans = 0;
    for(int i = 0; i < N; i++){
        if(i != N - 1) {
            ans += repow(3, N - 2 - i) * sum * 2;
            sum *= A[i + 1];
        }
        else ans += sum;
    }
    cout << ans.val << endl;
}
0