結果

問題 No.720 行列のできるフィボナッチ数列道場 (2)
ユーザー akakimidoriakakimidori
提出日時 2018-07-28 01:18:24
言語 C90
(gcc 11.4.0)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 1,515 bytes
コンパイル時間 972 ms
コンパイル使用メモリ 26,908 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-19 05:50:27
合計ジャッジ時間 1,726 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

typedef long long int int64;

#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define ABS(a) ((a)>(0)?(a):-(a))

const int l=2;
const int mod=1000000007;

#define POS(i,j) ((i)*l+(j))

void matmul(int *c,const int *a,const int *b){
  int x[4];
  int i,j,k;
  for(i=0;i<l*l;i++) x[i]=0;
  for(i=0;i<l;i++){
    for(j=0;j<l;j++){
      for(k=0;k<l;k++){
	x[POS(i,j)]=(x[POS(i,j)]+(int64)a[POS(i,k)]*b[POS(k,j)])%mod;
      }
    }
  }
  for(i=0;i<l*l;i++) c[i]=x[i];
  return;
}

void matadd(int *c,const int *a,const int *b){
  int i,j;
  for(i=0;i<l;i++)
    for(j=0;j<l;j++)
      c[POS(i,j)]=(a[POS(i,j)]+b[POS(i,j)])%mod;
  return;
}

void matmulPow(const int *a,int64 n,int *res){
  int t[4]={1,0,0,1};
  int s[4];
  int i;
  for(i=0;i<l*l;i++){
    s[i]=a[i];
  }
  while(n>0){
    if(n&0x01) matmul(t,t,s);
    matmul(s,s,s);
    n>>=1;
  }
  for(i=0;i<l*l;i++) res[i]=t[i];
  return;
}

void matSum(const int *a,int64 n,int *res){
  int i;
  if(n==1){
    for(i=0;i<l*l;i++) res[i]=a[i];
    return;
  }
  int x[4];
  int e[4]={1,0,0,1};
  for(i=0;i<l*l;i++) x[i]=a[i];
  matSum(a,n/2,res);
  matmulPow(a,n/2,x);
  matadd(x,x,e);
  matmul(res,res,x);
  if(n%2!=0){
    matmulPow(a,n,x);
    matadd(res,res,x);
  }
  return;
}

void run(void){
  int64 n;
  int m;
  scanf("%lld%d",&n,&m);
  int a[4]={1,1,1,0};
  int b[4];
  matmulPow(a,m,a);
  matSum(a,n,b);
  printf("%d\n",b[POS(1,0)]);
}

int main(void){
  run();
  return 0;
}
0