結果

問題 No.891 隣接3項間の漸化式
ユーザー tnakao0123tnakao0123
提出日時 2019-09-21 22:08:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,545 bytes
コンパイル時間 652 ms
コンパイル使用メモリ 85,084 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 06:49:50
合計ジャッジ時間 1,976 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 1 ms
4,348 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 1 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 1 ms
4,348 KB
testcase_39 AC 2 ms
4,348 KB
testcase_40 AC 2 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:72:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   72 |   scanf("%d%d%d", &a, &b, &n);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 891.cc:  No.891 隣接3項間の漸化式 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MOD = 1000000007;

/* typedef */

typedef long long ll;
typedef int mat[2][2];

/* global variables */

mat ma, mc;

/* subroutines */

void copymat(const mat a, mat b) { memcpy(b, a, sizeof(mat)); }
void initmat(mat a) { a[0][0] = a[1][1] = 1, a[0][1] = a[1][0] = 0; }

void mulmat(const mat a, const mat b, mat c) { // a * b => c
  c[0][0] = ((ll)a[0][0] * b[0][0] + (ll)a[0][1] * b[1][0]) % MOD;
  c[0][1] = ((ll)a[0][0] * b[0][1] + (ll)a[0][1] * b[1][1]) % MOD;
  c[1][0] = ((ll)a[1][0] * b[0][0] + (ll)a[1][1] * b[1][0]) % MOD;
  c[1][1] = ((ll)a[1][0] * b[0][1] + (ll)a[1][1] * b[1][1]) % MOD;
}

void powmat(const mat a, int b, mat c) { // a^b => c
  mat s, t;
  initmat(c);
  copymat(a, s);

  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);

  if (n <= 1) printf("%d\n", n);
  else {
    ma[0][0] = 0; ma[0][1] = 1;
    ma[1][0] = b; ma[1][1] = a;
    powmat(ma, n, mc);
    printf("%d\n", mc[0][1]);
  }
  
  return 0;
}
0