結果

問題 No.541 3 x N グリッド上のサイクルの個数
コンテスト
ユーザー Ryuhei Mori
提出日時 2017-06-01 17:59:38
言語 C(gnu17)
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -std=gnu17 -Wno-error=implicit-function-declaration -Wno-error=implicit-int -Wno-error=incompatible-pointer-types -Wno-error=int-conversion -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,311 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 192 ms
コンパイル使用メモリ 39,884 KB
最終ジャッジ日時 2026-02-21 23:47:54
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 62
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <stdio.h>

const int mod = 1000000007;

int dp[2][10][10];

const int T[10][10] = {
{1, 1, 1, 1, 1, 1, 1, 1, 0, 0},
{0, 1, 1, 1, 0, 0, 0, 1, 0, 1},
{0, 1, 1, 1, 1, 1, 0, 0, 0, 1},
{0, 1, 1, 1, 1, 1, 1, 0, 1, 1},
{0, 0, 1, 1, 1, 1, 0, 0, 0, 1},
{0, 0, 1, 1, 1, 1, 1, 0, 0, 1},
{0, 0, 0, 1, 0, 1, 1, 1, 0, 1},
{0, 0, 0, 1, 0, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 0, 1, 0, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
};

void mult(const int A[10][10], const int B[10][10], int C[10][10]){
  int i, j, k;
  for(i=0;i<10;i++){
    for(j=0;j<10;j++){
      long long int tmp = 0;
      for(k=0;k<10;k++){
        tmp = (tmp + (long long int) A[i][k] * B[k][j]) % mod;
      }
      C[i][j] = tmp;
    }
  }
}

void modpow(long long int n, int A[2][10][10]){
  int i, j;
  int tmp[2][10][10];
  int b = 0;
  int c = 0;
  for(i=0;i<10;i++){
    for(j=0;j<10;j++){
      tmp[0][i][j] = i == j;
    }
  }

  while(n){
    if(n&1) mult(tmp[b], A[c], tmp[b^1]), b^=1;
    mult(A[c], A[c], A[c^1]), c^=1;
    n /= 2;
  }

  for(i=0;i<10;i++){
    for(j=0;j<10;j++){
      A[0][i][j] = tmp[b][i][j];
    }
  }
}

int main(){
  int i, j;
  long long int n;
  scanf("%lld", &n);

  for(i=0;i<10;i++){
    for(j=0;j<10;j++){
      dp[0][i][j] = T[i][j];
    }
  }
  modpow(n+1, dp);

  printf("%d\n", dp[0][0][9]);
  return 0;
}
0