結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー 小指が強い人小指が強い人
提出日時 2016-08-17 22:50:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 171 ms / 5,000 ms
コード長 5,005 bytes
コンパイル時間 1,904 ms
コンパイル使用メモリ 178,192 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2024-04-25 08:36:23
合計ジャッジ時間 4,902 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
11,008 KB
testcase_01 AC 7 ms
11,136 KB
testcase_02 AC 171 ms
11,008 KB
testcase_03 AC 13 ms
11,136 KB
testcase_04 AC 55 ms
11,008 KB
testcase_05 AC 44 ms
11,136 KB
testcase_06 AC 55 ms
10,880 KB
testcase_07 AC 99 ms
11,008 KB
testcase_08 AC 10 ms
11,136 KB
testcase_09 AC 70 ms
10,880 KB
testcase_10 AC 21 ms
10,880 KB
testcase_11 AC 23 ms
11,008 KB
testcase_12 AC 45 ms
11,136 KB
testcase_13 AC 16 ms
10,880 KB
testcase_14 AC 8 ms
11,136 KB
testcase_15 AC 134 ms
11,008 KB
testcase_16 AC 110 ms
10,880 KB
testcase_17 AC 21 ms
10,880 KB
testcase_18 AC 114 ms
11,008 KB
testcase_19 AC 170 ms
11,136 KB
testcase_20 AC 16 ms
11,136 KB
testcase_21 AC 16 ms
11,136 KB
testcase_22 AC 15 ms
11,008 KB
testcase_23 AC 8 ms
10,880 KB
testcase_24 AC 12 ms
11,136 KB
testcase_25 AC 11 ms
10,880 KB
testcase_26 AC 11 ms
11,008 KB
testcase_27 AC 10 ms
11,008 KB
testcase_28 AC 10 ms
11,008 KB
testcase_29 AC 14 ms
11,008 KB
testcase_30 AC 170 ms
11,136 KB
testcase_31 AC 7 ms
11,136 KB
testcase_32 AC 34 ms
11,008 KB
testcase_33 AC 59 ms
11,136 KB
testcase_34 AC 51 ms
10,880 KB
testcase_35 AC 36 ms
10,880 KB
testcase_36 AC 116 ms
10,880 KB
testcase_37 AC 11 ms
11,008 KB
testcase_38 AC 147 ms
11,264 KB
testcase_39 AC 48 ms
10,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> veci;
typedef vector<ll> vecll;
typedef vector<string> vecs;
template<class T,class U> using Hash=unordered_map<T,U>;

#define REP(i, a, n) for(ll i = (a); (i) < (ll)(n); (i)++)
#define RREP(i, a, n) for(ll i = (n)-1; (i) >= (a); (i)--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, 0, n)
#define MOD 1000000007ULL
#define _SPLIT " "

template<class T> T read(){T a;cin >> a;return a;}
template<class T> void read(T& a){cin >> a;}
template<class T,class ...Args> void read(T& a, Args&... args){cin >> a; read(args...);} 
template<class T> void rarr(T& a, int n){for(int i = 0; i < n; i++) {cin >> a[i];}}
template<class T> void write(T a){cout << setprecision(22) << a << endl;}
template<class T,class ...Args> void write(T a, Args... args){cout << setprecision(22) << a << _SPLIT; write(args...);}
template<class T> void warr(vector<T> a, const char* c = " "){cout << a[0];for(int i = 1; i < (int)a.size(); i++)cout << c << a[i];cout << endl;;}
template<class T> void warr(T a, int n, const char* c = " "){cout << a[0];for(int i = 1; i < n; i++)cout << c << a[i];cout << endl;}
void split(string s, string delim, veci& result){result.clear();string::size_type pos = 0;while(pos != string::npos){string::size_type p = s.find(delim, pos);if(p == string::npos){result.push_back(atoi(s.substr(pos).data()));break;}else {result.push_back(atoi(s.substr(pos, p - pos).data()));}pos = p + delim.size();}}
void split(string s, string delim, vecs& result){result.clear();string::size_type pos = 0;while(pos != string::npos){string::size_type p = s.find(delim, pos);if(p == string::npos){result.push_back(s.substr(pos));break;}else {result.push_back(s.substr(pos, p - pos));}pos = p + delim.size();}}
ll gcd(ll a, ll b){while(true){ll k = a % b;if(k == 0)return b;a = b;b = k;}}
ll comb(ll n, ll m){ll p=1;m=min(m,n-m);for(ll i=1;i<=m;i++){p*=n-i+1;p/=i;}return p;}

typedef unsigned long long Num;

struct Matrix {
	vector<vector<Num> > v, w;
	Matrix() {}
	Matrix(int n, int m): v(n, vector<Num>(m)) { assert(n > 0 && m > 0); }
	inline int height() const { return (int)v.size(); }
	inline int width() const { return (int)v[0].size(); }
	inline Num& at(int i, int j) { assert(0 <= i && i < height() && 0 <= j && j < width()); return v[i][j]; }
	inline const Num& at(int i, int j) const { assert(0 <= i && i < height() && 0 <= j && j < width()); return v[i][j]; }
	static Matrix identity(int n) {
		Matrix A(n, n);
		rep(i, n) A.at(i, i) = 1;
		return A;
	}
	inline static Matrix identity(const Matrix& A) {
		assert(A.height() == A.width()); return identity(A.height()); }
	Matrix& operator*=(const Matrix& B) {
		int n = height(), m = B.width(), p = B.height();
		assert(p == width());
		w.resize(n, vector<Num>(m, 0));
		rep(i, n) rep(j, m) {
			//MOD = 1000000007の場合、1000000012000000036となり、
			//2^64にオーバーフローするのは2^64-1-1000000006^2くらいで、それは最上位4bitが立っているから
			//!MODが違う場合、書き換えること!
			Num x = 0;
			rep(k, p) {
				x += at(i, k) * B.at(k, j);
				if((x >> 60) == 0xf) x %= MOD;
			}
			x %= MOD;
			w[i][j] = x;
		}
		v.swap(w);
		return *this;
	}
	Matrix& operator+=(const Matrix& B) {
		int n = height(), m = width();
		assert(n == B.height() && m == B.width());
		rep(i, n) rep(j, m) {
			at(i, j) += B.at(i, j);
			if(at(i, j) >= MOD) at(i, j) -= MOD;
		}
		return *this;
	}
	void undomult() { v.swap(w); }
};
ostream& operator<<(ostream& o, const Matrix& A) {
	int n = A.height(), m = A.width();
	rep(i, n) {
		o << "["; rep(j, m) o << A.at(i, j) << (j+1 == m ? "]\n" : ",");
	}
	return o;
}

/* ジェネリックなべき乗算 */
template<typename Mat>
Mat operator^(const Mat& t, ll k) {
	Mat A = t, B = Mat::identity(t);
	while(k) {
		if(k & 1) {
			B *= A;
		}
		A *= A;
		k >>= 1;
	}
	return B;
}

ll n;
vector<ll> v(1001000);

ll f(ll k){
    Matrix m(n,n);
    rep(i,n)m.at(0,i)=1;
    rep(i,n-1)m.at(i+1,i)=1;
    m=m^(k-n);
    ll res1=0;
    rep(i,n)res1=(res1+m.at(0,i)*v[n-i-1])%MOD;
    return res1;
}

ull powmod(unsigned long long a, unsigned long long k, const unsigned MD) {
	a %= MD;
	unsigned long long r = MD == 1 ? 0 : 1;
	while(k) {
		if(k & 1)
			(r *= a) %= MD;
		(a *= a) %= MD;
		k >>= 1;
	}
	return r;
}

int main(void)
{
    ll k;
    read(n,k);
    rarr(v,n);
    ll res1=0,res2=0;
    if(k<=1e6){
        ll s=0;
        rep(i,n)s+=v[i];
        v[n]=s;
        ll vs=s;
        REP(i,n,k){
            vs=(vs+s)%MOD;
            s=(s-v[i-n]+MOD)%MOD;
            s=(s+v[i])%MOD;
            v[i+1]=s;
        }
        res1=v[k-1],res2=vs;
    }
    else {
        res2=f(k+n);
        REP(i,1,n-1){
            res2=(res2-f(k+n-i-1)*i+MOD*100)%MOD;
            res2+=v[i-1]*(n-i-1);
        }
        res2=(res2+MOD-v[n-1]);
        res2=(res2*powmod(n-1,MOD-2,MOD))%MOD;
        res1=f(k);
    }
    write(res1,res2);
    return 0;
}
0