結果
| 問題 |
No.194 フィボナッチ数列の理解(1)
|
| コンテスト | |
| ユーザー |
mint6421
|
| 提出日時 | 2019-07-04 15:18:53 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,133 bytes |
| コンパイル時間 | 1,628 ms |
| コンパイル使用メモリ | 166,428 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-19 03:56:40 |
| 合計ジャッジ時間 | 3,116 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 WA * 8 |
コンパイルメッセージ
main.cpp:128:1: warning: ISO C++ forbids declaration of ‘main’ with no type [-Wreturn-type]
128 | main(){
| ^~~~
ソースコード
#include<bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define INF 1000000000000000
#define ll long long
#define ull unsigned long long
#define M (int)(1e9+7)
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=(int)m;i<(int)n;i++)
#define RFOR(i,m,n) for(int i=(int)m;i>=(int)n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
#define IN(a,n) rep(i,n){ cin>>a[i]; }
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define int ll
void init(){
cin.tie(0);
ios::sync_with_stdio(false);
}
int n,k;
int a[1100];
#define mat vector<vector<int>>
mat mul_mat(mat &a, mat &b){
mat res(a.size(), vector<int>(b[0].size(),0));
rep(i, a.size())rep(k, b.size())rep(j, b[0].size()){
res[i][j] = (res[i][j] + (a[i][k] * b[k][j])%M)%M;
}
return res;
}
mat power_mat(mat &a,int k){
mat res(a.size(), vector<int>(a.size()));
rep(i, a.size()) res[i][i] = 1;
while(k > 0){
if(k&1) res = mul_mat(res,a);
a = mul_mat(a,a);
k >>= 1;
}
return res;
}
int f[1100000];
void solve1(){
int sum=0,ans=0;
rep(i,n){
f[i]=a[i];
sum+=f[i];
sum%=M;
ans+=f[i];
ans%=M;
}
FOR(i,n,k){
int res=sum;
sum-=f[i%n];
sum+=res;
sum%=M;
f[i%n]=res;
ans+=res;
ans%=M;
}
cout<<f[(k-1+n)%n]<<' '<<ans<<endl;
}
void solve2(){
mat s(n,vector<int>(n,0));
rep(i,n){
s[i][0]=1;
}
rep(i,n-1){
s[i][i+1]=1;
}
mat t(2*n,vector<int>(2*n,0));
rep(i,n){
rep(j,n){
t[i][j]=s[i][j];
if(i==j){
t[i+n][i]=1;
t[i+n][i+n]=1;
}
}
}
s = power_mat(s,k-n);
int res=0;
rep(i,n){
res+=(a[n-1-i]*s[i][0])%M;
res%=M;
}
t = power_mat(t,k-n+1);
t[n][0]--;
int ans=0;
rep(i,n){
ans+=(a[n-1-i]*t[i+n][0])%M;
ans%=M;
ans+=a[i];
ans%=M;
}
cout<<res<<' '<<ans<<endl;
}
main(){
cin>>n>>k;
rep(i,n){
cin>>a[i];
}
if(k<=1000000) solve1();
else solve2();
}
mint6421