結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー snukesnuke
提出日時 2015-04-26 22:25:20
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 14 ms / 5,000 ms
コード長 3,328 bytes
コンパイル時間 736 ms
コンパイル使用メモリ 87,516 KB
実行使用メモリ 11,372 KB
最終ジャッジ日時 2024-07-05 02:58:54
合計ジャッジ時間 2,248 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

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