結果

問題 No.1049 Zero (Exhaust)
ユーザー le_panda_noirle_panda_noir
提出日時 2020-05-30 20:11:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,147 bytes
コンパイル時間 1,120 ms
コンパイル使用メモリ 85,752 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-25 14:41:22
合計ジャッジ時間 2,082 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0,_i=(n);i<_i;++i)

template<int MOD> struct MInt {
  long long val;
  constexpr MInt(long long val = 0) : val(val % MOD) { if (val < 0) val += MOD; }
  MInt operator+(const MInt& n) const { return MInt(val) += n; }
  MInt operator-(const MInt& n) const { return MInt(val) -= n; }
  MInt operator*(const MInt& n) const { return MInt(val) *= n; }
  MInt& operator+=(const MInt& n) { val = (val + n.val) % MOD; return *this; }
  MInt& operator-=(const MInt& n) { val = (MOD + val - n.val) % MOD; return *this; }
  MInt& operator*=(const MInt& n) { val = (val * n.val) % MOD; return *this; }
  friend ostream& operator<<(ostream& os, const MInt& n) { os<<n.val; return os; }
};
constexpr int MOD = 1e9+7;
using mint = MInt<MOD>;

template<class T> struct Matrix {
  int width, height;
  vector<vector<T>> v;
  Matrix(int w) : width(w), height(w), v(w, vector<T>(w, 0)) {};
  Matrix(int w, int h) : width(w), height(h), v(w, vector<T>(h, 0)) {};
  Matrix operator+(const Matrix& n) const { Matrix ans = v; return ans += n; }
  Matrix operator*(const Matrix& n) const { Matrix ans = v; return ans *= n; }
  constexpr Matrix& operator+=(const Matrix& rhs) {
    for (int i = 0; i < height; ++i)
      for (int j = 0; j < width; ++j)
        v[i][j] += rhs.v[i][j];
    return *this;
  }
  Matrix& operator*=(const Matrix& rhs) {
    Matrix<T> ans(height, rhs.width);
    for (int i = 0; i < height; ++i)
      for (int j = 0; j < rhs.width; ++j)
        for (int k = 0; k < width; ++k)
          ans[i][j] += v[i][k] * rhs.v[k][j];
    width = rhs.width;
    v = ans.v;
    return *this;
  }
  Matrix pow(ll N) {
    Matrix pow = *this, ans(width);
    for (int i = 0; i < width; ++i) ans[i][i] = 1;
    for (ll p = N; p > 0; p >>= 1) {
      if (p & 1) ans *= pow;
      pow *= pow;
    }
    return ans;
  }
  vector<T>& operator[](int i) {
    return v[i];
  }
};

int main() {
  int P, K; cin >> P >> K;
  Matrix<mint> m(2);
  m[0][0] = P+1; m[0][1] = 2;
  m[1][0] = P-1; m[1][1] = 2 * (P-1);
  cout << m.pow(K)[0][0] << endl;

  return 0;
}
0