結果

問題 No.579 3 x N グリッド上のサイクルのサイズ(hard)
ユーザー Ryuhei Mori
提出日時 2017-06-07 12:26:28
言語 C
(gcc 8.2.0)
結果
AC  
実行時間 2 ms
コード長 1,752 Byte
コンパイル時間 98 ms
使用メモリ 968 KB
最終ジャッジ日時 2019-08-16 19:23:29

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
1_1.txt AC 1 ms
968 KB
1_2.txt AC 1 ms
968 KB
1_3.txt AC 1 ms
964 KB
1_4.txt AC 1 ms
968 KB
1_5.txt AC 1 ms
968 KB
1_6.txt AC 1 ms
964 KB
1_7.txt AC 1 ms
964 KB
1_8.txt AC 1 ms
964 KB
1_9.txt AC 1 ms
968 KB
1_10.txt AC 1 ms
968 KB
1_11.txt AC 1 ms
964 KB
1_12.txt AC 1 ms
964 KB
1_13.txt AC 1 ms
964 KB
1_14.txt AC 1 ms
964 KB
1_15.txt AC 1 ms
968 KB
1_16.txt AC 1 ms
964 KB
1_17.txt AC 1 ms
968 KB
1_18.txt AC 1 ms
968 KB
1_19.txt AC 1 ms
968 KB
1_20.txt AC 1 ms
964 KB
2_1.txt AC 1 ms
964 KB
2_2.txt AC 1 ms
968 KB
2_3.txt AC 1 ms
968 KB
2_4.txt AC 1 ms
968 KB
2_5.txt AC 1 ms
964 KB
2_6.txt AC 1 ms
968 KB
2_7.txt AC 1 ms
964 KB
2_8.txt AC 1 ms
964 KB
2_9.txt AC 1 ms
968 KB
2_10.txt AC 1 ms
968 KB
2_11.txt AC 1 ms
968 KB
2_12.txt AC 1 ms
968 KB
2_13.txt AC 1 ms
968 KB
2_14.txt AC 1 ms
964 KB
2_15.txt AC 2 ms
968 KB
2_16.txt AC 2 ms
968 KB
2_17.txt AC 1 ms
968 KB
2_18.txt AC 1 ms
968 KB
2_19.txt AC 1 ms
964 KB
2_20.txt AC 1 ms
968 KB
3_1.txt AC 2 ms
968 KB
3_2.txt AC 1 ms
968 KB
3_3.txt AC 1 ms
968 KB
3_4.txt AC 1 ms
964 KB
3_5.txt AC 1 ms
968 KB
3_6.txt AC 1 ms
964 KB
3_7.txt AC 2 ms
964 KB
3_8.txt AC 1 ms
968 KB
3_9.txt AC 1 ms
964 KB
3_10.txt AC 1 ms
964 KB
3_11.txt AC 1 ms
968 KB
3_12.txt AC 2 ms
968 KB
3_13.txt AC 1 ms
964 KB
3_14.txt AC 1 ms
968 KB
3_15.txt AC 1 ms
964 KB
3_16.txt AC 1 ms
964 KB
3_17.txt AC 1 ms
968 KB
3_18.txt AC 2 ms
964 KB
3_19.txt AC 2 ms
964 KB
3_20.txt AC 1 ms
968 KB
4_1.txt AC 2 ms
968 KB
4_2.txt AC 2 ms
968 KB
4_3.txt AC 1 ms
964 KB
4_4.txt AC 2 ms
968 KB
4_5.txt AC 2 ms
964 KB
4_6.txt AC 2 ms
964 KB
4_7.txt AC 2 ms
964 KB
4_8.txt AC 2 ms
964 KB
4_9.txt AC 2 ms
964 KB
4_10.txt AC 2 ms
968 KB
4_11.txt AC 2 ms
968 KB
4_12.txt AC 2 ms
964 KB
4_13.txt AC 1 ms
964 KB
4_14.txt AC 2 ms
968 KB
4_15.txt AC 2 ms
968 KB
4_16.txt AC 1 ms
964 KB
4_17.txt AC 2 ms
964 KB
4_18.txt AC 2 ms
964 KB
4_19.txt AC 1 ms
968 KB
4_20.txt AC 2 ms
968 KB
テストケース一括ダウンロード

ソースコード

diff #
#include <stdio.h>

#define M 12

const int mod = 1000000007;

int dp[2][M][M];

const int T[M][M] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   -4},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   -4},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,   39},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,   -56},
{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,  -62},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,  386},
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,  -753},
{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,  884},
{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, -685},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,  342},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,  -102},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,   16},
};


const int s[M] = {
  32, 316, 2292, 14422, 84744, 479004, 2638328, 14258574, 75940592, 399782668, 84795558, 786749020
};


void mult(const int A[M][M], const int B[M][M], int C[M][M]){
  int i, j, k;
  for(i=0;i<M;i++){
    for(j=0;j<M;j++){
      long long int tmp = 0;
      for(k=0;k<M;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][M][M]){
  int i, j;
  int tmp[2][M][M];
  int b = 0;
  int c = 0;
  for(i=0;i<M;i++){
    for(j=0;j<M;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<M;i++){
    for(j=0;j<M;j++){
      A[0][i][j] = tmp[b][i][j];
    }
  }
}

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

  for(i=0;i<M;i++){
    for(j=0;j<M;j++){
      dp[0][i][j] = T[i][j];
    }
  }

  if(n <= M){
    printf("%d\n", s[n-1]);
    return 0;
  }
  modpow(n-M, dp);
  ans = 0;
  for(i=0;i<M;i++){
    ans = (ans + (long long) s[i] * dp[0][i][M-1]) % mod;
  }

  printf("%d\n", ans);
  return 0;
}
0