結果

問題 No.978 Fibonacci Convolution Easy
ユーザー 東前頭十一枚目東前頭十一枚目
提出日時 2020-01-31 22:21:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 2,037 bytes
コンパイル時間 1,613 ms
コンパイル使用メモリ 167,804 KB
実行使用メモリ 19,188 KB
最終ジャッジ日時 2023-10-19 01:03:47
合計ジャッジ時間 2,715 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,348 KB
testcase_01 AC 13 ms
9,640 KB
testcase_02 AC 8 ms
6,632 KB
testcase_03 AC 26 ms
18,544 KB
testcase_04 AC 10 ms
7,372 KB
testcase_05 AC 3 ms
4,360 KB
testcase_06 AC 12 ms
8,812 KB
testcase_07 AC 18 ms
13,264 KB
testcase_08 AC 14 ms
10,252 KB
testcase_09 AC 21 ms
14,704 KB
testcase_10 AC 28 ms
19,156 KB
testcase_11 AC 10 ms
7,980 KB
testcase_12 AC 4 ms
4,348 KB
testcase_13 AC 11 ms
8,720 KB
testcase_14 AC 5 ms
5,008 KB
testcase_15 AC 13 ms
9,460 KB
testcase_16 AC 27 ms
19,188 KB
testcase_17 AC 27 ms
19,136 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using fint64 = int_fast64_t;
template<fint64 MOD>
struct ModInt {
	fint64 x;
	ModInt():x(0){}
	ModInt(fint64 x):
		x(x>=0?x%MOD:(MOD-(-x)%MOD)%MOD)
		{}
	// 負号
	ModInt operator -() const{
		return ModInt(-x);
	}
	// 加算
	ModInt &operator +=(const ModInt &rhs){
		x+=rhs.x;
		if(x>=MOD) x-=MOD;
		return (*this);
	}
	ModInt operator +(const ModInt &rhs) const{
		return ModInt(*this)+=rhs;
	}
	// 減算
	ModInt &operator -=(const ModInt &rhs){
		x+=MOD-rhs.x;
		if(x>=MOD) x-=MOD;
		return (*this);
	}
	ModInt operator -(const ModInt &rhs) const{
		return ModInt(*this)-=rhs;
	}
	// 乗算
	ModInt &operator *=(const ModInt &rhs){
		x*=rhs.x;
		if(x>=MOD) x%=MOD;
		return (*this);
	}
	ModInt operator *(const ModInt &rhs) const{
		return ModInt(*this)*=rhs;
	}
	// 除算
	ModInt &operator /=(const ModInt &rhs){
		(*this)*=rhs.inverse();
		return (*this);
	}
	ModInt operator /(const ModInt &rhs) const{
		return ModInt(*this)/=rhs;
	}
	// 等号
	bool operator ==(const ModInt &rhs){
		return x==rhs.x;
	}
	bool operator !=(const ModInt &rhs){
		return x!=rhs.x;
	}
	// 累乗
	ModInt pow(fint64 n) const{
		fint64 tmp=x;
		fint64 ret=1;
		while(n>0){
			if(n&1) ret=ret*tmp%MOD;
			tmp=tmp*tmp%MOD;
			n>>=1ll;
		}
		return ModInt(ret);
	}
	// 逆元
	ModInt inverse() const{
		fint64 a=x,b=MOD,s=1,t=0;
		while(b>0){
			fint64 u=a/b;
			a-=u*b;
			s-=u*t;
			swap(a,b);
			swap(s,t);
		}
		return ModInt(s);
	}
	// 入出力
	friend istream &operator >>(istream &lhs,ModInt<MOD> &rhs){
		fint64 x; lhs>>x;
		rhs=ModInt<MOD>(x);
		return lhs;
	}
	friend ostream &operator <<(ostream &lhs,const ModInt<MOD> &rhs){
		return lhs<<rhs.x;
	}
};

const int MOD = 1e9 + 7;
using mint = ModInt<MOD>;
int main() {
	int n; cin >> n;
	mint p; cin >> p;
	mint a[n] = {0, 1};
	mint sum = 1;
	for(int i = 2; i < n; ++i) {
		a[i] = p * a[i - 1] + a[i - 2];
		sum += a[i];
	}
	mint ans = 0;
	for(int i = 0; i < n; ++i) {
		ans += a[i] * sum;
		sum -= a[i];
	}
	cout << ans << '\n';
	return 0;
}
0