結果

問題 No.3457 Fibo-shrink
コンテスト
ユーザー tnakao0123
提出日時 2026-02-28 18:06:48
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
TLE  
実行時間 -
コード長 3,452 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 912 ms
コンパイル使用メモリ 60,724 KB
実行使用メモリ 7,936 KB
最終ジャッジ日時 2026-02-28 18:06:56
合計ジャッジ時間 6,628 ms
ジャッジサーバーID
(参考情報)
judge7 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 4 TLE * 1 -- * 7
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

/* -*- coding: utf-8 -*-
 *
 * 3457.cc:  No.3457 Fibo-shrink - yukicoder
 */

#include<cstdio>
#include<algorithm>

using namespace std;

/* constant */

const int MAX_K = 500 + 1;
const int MOD = 10007;

/* typedef */

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

  explicit operator int() const { return v; }
  
  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 { return MI(MOD - 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); }

  bool operator==(const MI m) const { return v == m.v; }
  bool operator!=(const MI m) const { return v != m.v; }

  MI pow(int n) const {  // a^n % MOD
    MI pm = 1, a = *this;
    while (n > 0) {
      if (n & 1) pm *= a;
      a *= a;
      n >>= 1;
    }
    return pm;
  }

  MI inv() const { return pow(MOD - 2); }
  MI operator/(const MI m) const { return *this * m.inv(); }
  MI &operator/=(const MI m) { return (*this = *this / m); }
};

using mi = MI<MOD>;
using vec = mi[MAX_K];
using mat = vec[MAX_K];

/* global variables */

mi fs[MAX_K], invfs[MAX_K];
mat ma, mb;
vec va, vb;

/* subroutines */

inline void initvec(const int n, vec a) { fill(a, a + n, 0); }

inline void initmat(const int n, mat a) {
  for (int i = 0; i < n; i++) initvec(n, a[i]);
}

inline void unitmat(const int n, mat a) {
  initmat(n, a);
  for (int i = 0; i < n; i++) a[i][i] = 1;
}

inline void copymat(const int n, const mat a, mat b) {
  for (int i = 0; i < n; i++) copy(a[i], a[i] + n, b[i]);
}

inline void addmat(const int n, const mat a, const mat b, mat c) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) c[i][j] = a[i][j] + b[i][j];
}

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

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

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

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

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

void printmat(const int n, const mat a) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (j) putchar(' ');
      printf("%d", a[i][j]);
    }
    putchar('\n');
  }
}

/* main */

int main() {
  int k, s, n;
  scanf("%d%d%d", &k, &s, &n);
  k++;

  fs[0] = fs[1] = 1;
  for (int i = 2; i < k; i++) fs[i] = fs[i - 2] + fs[i - 1];
  for (int i = 0; i < k; i++) invfs[i] = fs[i].inv();

  initmat(k, ma);
  for (int i = 0; i < k - 1; i++) ma[i][i + 1] = 1;
  for (int j = 0; j < k; j++) ma[k - 1][j] = invfs[k - 1 - j];
  powmat(k, ma, n - 1, mb);

  initvec(k, va);
  va[k - 1] = s;
  mulmatvec(k, mb, va, vb);

  printf("%d\n", (int)vb[k - 1]);
  //for (int i = 0; i < k; i++) printf(" %d", (int)vb[i]); putchar('\n');
  
  return 0;
}

0