結果

問題 No.1810 RGB Biscuits
ユーザー tnakao0123tnakao0123
提出日時 2022-01-15 23:25:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,926 bytes
コンパイル時間 345 ms
コンパイル使用メモリ 45,696 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-01 22:23:11
合計ジャッジ時間 1,131 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1810.cc:  No.1810 RGB Biscuits - yukicoder
 */

#include<cstdio>
#include<cstring>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 1000;
const int MOD = 1000000007;

/* typedef */

typedef long long ll;

template<const int MOD>
struct MI {
  int v;
  MI(): v() {}
  MI(int _v): v(_v % MOD) {}
  MI(long long _v): v(_v % MOD) {}

  MI operator+(const MI m) const { return MI(v + m.v); }
  MI operator-(const MI m) const { return MI(v + MOD - m.v); }
  MI operator*(const MI m) const { return MI((long long)v * m.v); }

  MI &operator+=(const MI m) { return (*this = *this + m); }
  MI &operator-=(const MI m) { return (*this = *this - m); }
  MI &operator*=(const MI m) { return (*this = *this * m); }
};

typedef MI<MOD> mi;
typedef mi mat[2][2];

/* global variables */

mat ma, mb;

/* subroutines */

inline void initmat(mat a) { memset(a, 0, sizeof(mat)); }
inline void unitmat(mat a) { a[0][0] = a[1][1] = 1; a[0][1] = a[1][0] = 0; }

inline void copymat(const mat a, mat b) { memcpy(b, a, sizeof(mat)); }

inline void mulmat(const mat a, const mat b, mat c) {
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 2; j++) {
      c[i][j] = 0;
      for (int k = 0; k < 2; k++)
        c[i][j] += a[i][k] * b[k][j];
    }
}

inline void powmat(const mat a, ll b, mat c) {
  mat s, t;
  copymat(a, s);
  unitmat(c);

  while (b > 0) {
    if (b & 1) {
      mulmat(c, s, t);
      copymat(t, c);
    }

    mulmat(s, s, t);
    copymat(t, s);
    b >>= 1;
  }
}

/* main */

int main() {
  int a, b, n;
  scanf("%d%d%d", &a, &b, &n);

  ma[0][0] = a, ma[0][1] = b;
  ma[1][0] = 1, ma[1][1] = 0;

  while (n--) {
    ll ti;
    scanf("%lld", &ti);

    powmat(ma, ti / 2, mb);

    mi rc = mb[0][0] + mb[0][1];
    mi gc = mb[1][0] + mb[1][1];
    mi bc = (ti & 1)  ? rc * a + gc * b : 0;
    mi sum = rc + gc + bc;

    printf("%d\n", sum.v);
  }
  return 0;
}
0