結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー snukesnuke
提出日時 2015-04-26 22:25:20
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 3,328 bytes
コンパイル時間 956 ms
コンパイル使用メモリ 84,184 KB
実行使用メモリ 11,464 KB
最終ジャッジ日時 2023-09-18 12:01:09
合計ジャッジ時間 2,740 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
11,328 KB
testcase_01 AC 5 ms
11,172 KB
testcase_02 AC 15 ms
11,352 KB
testcase_03 AC 6 ms
11,352 KB
testcase_04 AC 9 ms
11,356 KB
testcase_05 AC 8 ms
11,280 KB
testcase_06 AC 9 ms
11,292 KB
testcase_07 AC 11 ms
11,324 KB
testcase_08 AC 6 ms
11,464 KB
testcase_09 AC 11 ms
11,264 KB
testcase_10 AC 7 ms
11,232 KB
testcase_11 AC 7 ms
11,332 KB
testcase_12 AC 8 ms
11,404 KB
testcase_13 AC 6 ms
11,188 KB
testcase_14 AC 5 ms
11,220 KB
testcase_15 AC 13 ms
11,348 KB
testcase_16 AC 12 ms
11,236 KB
testcase_17 AC 6 ms
11,184 KB
testcase_18 AC 12 ms
11,316 KB
testcase_19 AC 13 ms
11,364 KB
testcase_20 AC 5 ms
11,360 KB
testcase_21 AC 10 ms
11,172 KB
testcase_22 AC 5 ms
11,176 KB
testcase_23 AC 6 ms
11,412 KB
testcase_24 AC 8 ms
11,264 KB
testcase_25 AC 7 ms
11,192 KB
testcase_26 AC 8 ms
11,244 KB
testcase_27 AC 7 ms
11,204 KB
testcase_28 AC 6 ms
11,184 KB
testcase_29 AC 9 ms
11,224 KB
testcase_30 AC 14 ms
11,256 KB
testcase_31 AC 5 ms
11,264 KB
testcase_32 AC 9 ms
11,188 KB
testcase_33 AC 9 ms
11,280 KB
testcase_34 AC 8 ms
11,404 KB
testcase_35 AC 8 ms
11,448 KB
testcase_36 AC 12 ms
11,272 KB
testcase_37 AC 6 ms
11,424 KB
testcase_38 AC 13 ms
11,348 KB
testcase_39 AC 9 ms
11,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <string.h>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <cctype>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rrep(i,n) for(int i = 1; i <= n; ++i)
#define drep(i,n) for(int i = n-1; i >= 0; --i)
#define gep(i,g,j) for(int i = g.head[j]; i != -1; i = g.e[i].next)
#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define rng(a) a.begin(),a.end()
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcount
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef vector<int> vi;
inline int in() { int x; scanf("%d",&x); return x;}
inline void priv(vi& a) { rep(i,sz(a)) printf("%d%c",a[i],i==sz(a)-1?'\n':' ');}

const int MX = 10005, MY = 1000005, INF = 1000010000;
const ll LINF = 1000000000000000000ll;
const double eps = 1e-10;
const int di[] = {-1,0,1,0}, dj[] = {0,-1,0,1}; //^<v>

// Mod int
const int mod = 1000000007;
struct mint{
	ll x;
	mint():x(0){}
	mint(ll x):x((x%mod+mod)%mod){}
	mint operator+=(const mint& a){ if((x+=a.x)>=mod) x-=mod; return *this;}
	mint operator-=(const mint& a){ if((x+=mod-a.x)>=mod) x-=mod; return *this;}
	mint operator*=(const mint& a){ (x*=a.x)%=mod; return *this;}
	mint operator+(const mint& a)const{ return mint(*this) += a;}
	mint operator-(const mint& a)const{ return mint(*this) -= a;}
	mint operator*(const mint& a)const{ return mint(*this) *= a;}
  bool operator==(const mint& a)const{ return x == a.x;}
};
//
// Matrix
struct mat{
	//typedef double TT;
	typedef mint TT;
	int h, w;
	vector<vector<TT> > d;
	mat(){}
	mat(int h, int w, TT v=0):h(h),w(w),d(h,vector<TT>(w,v)){}
	void fil(TT v=0){ rep(i,h)rep(j,w) d[i][j] = v;}
	void uni(){ rep(i,h)rep(j,w) d[i][j] = (i==j);}
	mat operator+(const mat& a)const{ // same size
		mat res(h,w);
		rep(i,h)rep(j,w) res.d[i][j] = d[i][j]+a.d[i][j];
		return res;
	}
	mat operator-(const mat& a)const{ // same size
		mat res(h,w);
		rep(i,h)rep(j,w) res.d[i][j] = d[i][j]-a.d[i][j];
		return res;
	}
	mat operator*(const mat& a)const{ // w = a.h
		mat res(h,a.w);
		rep(i,h)rep(k,w)rep(j,a.w) res.d[i][j] += d[i][k]*a.d[k][j];
		return res;
	}
	mat power(ll a){ // h = w
		if(a == 0){
			mat res(h,w); res.uni();
			return res;
		}
		mat res = power(a/2);
		res = res*res;
		if(a&1) res = res*(*this);
		return res;
	}
};
//

mint x[MX+MY];

int main(){
  int n; ll k;
  cin >> n >> k;
  if (n > 50) {
    rep(i,n) cin >> x[i].x;
    mint s = 0;
    rep(i,n) s += x[i];
    for (int i = n; i < n+k+3; ++i) {
      x[i] = s;
      s += x[i];
      s -= x[i-n];
    }
    cout << x[k-1].x << " ";
    s = 0;
    rep(i,k) s += x[i];
    cout << s.x << endl;
    return 0;
  }
  mat m(n+1,n+1), m2, a(n+1,1), a2;
  rep(i,n) {
    cin >> a.d[i][0].x;
  }
  rep(i,n-1) m.d[i][i+1] = 1;
  rep(i,n) m.d[n-1][i] = 1;
  m.d[n][n] = m.d[n][0] = 1;
  m2 = m.power(k);
  m = m.power(k-1);
  a2 = m2*a;
  a = m*a;
  cout << a.d[0][0].x << " " << a2.d[n][0].x << endl;
  return 0;
}




0