結果

問題 No.1448 和差算
ユーザー boutarou
提出日時 2021-03-31 14:32:49
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,991 bytes
コンパイル時間 1,923 ms
コンパイル使用メモリ 194,888 KB
最終ジャッジ日時 2025-01-20 01:31:46
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < int(n); i++)
using ll = long long;
using P = pair<int, int>;

// MODINT
const int MOD = 1000000007;
struct mint {
    long long x;
    mint(long long x = 0):x((x % MOD + MOD) % MOD) {}
    mint operator-() const { return mint(-x); }
    mint& operator+=(const mint a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += MOD - a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= MOD;
        return *this;
    }
    mint operator+(const mint a) const { return mint(*this) += a; }
    mint operator-(const mint a) const { return mint(*this) -= a; }
    mint operator*(const mint a) const { return mint(*this) *= a; }

    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t >> 1);
        a *= a;
        if (t & 1) a *= *this;
        return a;
    }
    mint inv() const { return pow(MOD - 2); }
    mint& operator/=(const mint a) { return *this *= a.inv(); }
    mint operator/(const mint a) const { return mint(*this) /= a; }
    bool operator==(const mint& a) const { return x == a.x; }
};
mint modpow(mint x, ll t) { return x.pow(t); }
mint modinv(mint x) {return x.inv(); }
istream& operator>>(istream& is, mint& a) { return is >> a.x; }
ostream& operator<<(ostream& os, const mint& a) { return os << a.x; }


int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);

    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    ll n;
    cin >> n;
    mint so = modpow(16, n / 8);
    n %= 8;
    ll w = 1, x = 0, y = 0, z = 1;
    rep(i, n) {
        ll nw = w - y, nx = x - z, ny = w + y, nz = x + z;
        w = nw, x = nx, y = ny, z = nz;
    }
    // XはAの係数、YはBの係数
    mint X = w + y, Y = x + z;
    X *= (w + y > 0 ? b : a);
    Y *= (x + z > 0 ? d : c);
    mint ans = (X + Y) * so;
    cout << ans << endl;
    return 0;
}
0