結果

問題 No.621 3 x N グリッド上のドミノの置き方の数
ユーザー Sebastian KingSebastian King
提出日時 2017-12-21 01:38:35
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 3,000 ms
コード長 2,333 bytes
コンパイル時間 1,487 ms
コンパイル使用メモリ 154,856 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-22 11:12:08
合計ジャッジ時間 3,675 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 2 ms
4,376 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,376 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 2 ms
4,380 KB
testcase_52 AC 2 ms
4,380 KB
testcase_53 AC 2 ms
4,376 KB
testcase_54 AC 2 ms
4,380 KB
testcase_55 AC 2 ms
4,380 KB
testcase_56 AC 2 ms
4,380 KB
testcase_57 AC 2 ms
4,376 KB
testcase_58 AC 2 ms
4,376 KB
testcase_59 AC 2 ms
4,376 KB
testcase_60 AC 2 ms
4,380 KB
testcase_61 AC 2 ms
4,376 KB
testcase_62 AC 2 ms
4,380 KB
testcase_63 AC 2 ms
4,380 KB
testcase_64 AC 2 ms
4,380 KB
testcase_65 AC 2 ms
4,376 KB
testcase_66 AC 2 ms
4,384 KB
testcase_67 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define pb push_back
#define SZ(x) ((int)(x).size())
#define rep(i,a,n) for (int i=a;i<n;i++)

using namespace std;

typedef __int128 ll;
typedef long long LL;
typedef vector<LL> VI;

const ll mod=1E9+7;

ll powmod(ll a,ll b){ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

namespace nico{
	const int N=10010;
	ll res[N],base[N],_c[N],_md[N];

	vector<LL> Md;	
	void mul(ll *a,ll *b,LL k){
		rep(i,0,k+k) _c[i]=0;
		rep(i,0,k) if (a[i]) rep(j,0,k) _c[i+j]=(_c[i+j]+a[i]*b[j])%mod;
		for (LL i=k+k-1;i>=k;i--) if (_c[i])
			rep(j,0,SZ(Md)) _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod;
		rep(i,0,k) a[i]=_c[i];
	}
	LL solve(ll n,VI a,VI b){
		ll ans=0,pnt=0;
		LL k=SZ(a);
		assert(SZ(a)==SZ(b));
		rep(i,0,k) _md[k-1-i]=-a[i];_md[k]=1;
		Md.clear();
		rep(i,0,k) if (_md[i]!=0) Md.push_back(i);
		rep(i,0,k) res[i]=base[i]=0;
		res[0]=1;
		while ((1ll<<pnt)<=n) pnt++;
		for (LL p=pnt;p>=0;p--){
			mul(res,res,k);
			if ((n>>p)&1){
				for (LL i=k-1;i>=0;i--) res[i+1]=res[i];res[0]=0;
				rep(j,0,SZ(Md)) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod;
			}
		}
		rep(i,0,k) ans=(ans+res[i]*b[i])%mod;
		if (ans<0) ans+=mod;
		return ans;
	}

	VI BM(VI s){
		VI C(1,1),B(1,1);
		LL L=0,m=1,b=1;
		rep(n,0,SZ(s)){
			ll d=0;
			rep(i,0,L+1) d=(d+(ll)C[i]*s[n-i])%mod;
			if (d==0) ++m;
			else if (2*L<=n){
				VI T=C;
				ll c=mod-d*powmod(b,mod-2)%mod;
				while (SZ(C)<SZ(B)+m) C.pb(0);
				rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
				L=n+1-L; B=T; b=d; m=1;
			}
			else{
				ll c=mod-d*powmod(b,mod-2)%mod;
				while (SZ(C)<SZ(B)+m) C.pb(0);
				rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
				++m;
			}
		}
		return C;
	}
	LL gao(VI a,ll n){ // 0-based value, n-th
		VI c=BM(a);
		c.erase(c.begin());
		rep(i,0,SZ(c)) c[i]=(mod-c[i])%mod;
		return solve(n,c,VI(a.begin(),a.begin()+SZ(c)));
	}
};

int main(){
	vector<LL> a({2, 5, 22, 75, 264, 941, 3286, 11623, 40960, 144267, 508812, 1792981, 6319994, 22277291, 78518760, 276763545ll, 975517878ll, 3438444583ll, 12119670866ll, 42718700667ll, 150572583140ll, 530730064095ll, 1870688029160ll, 6593699432859ll, 23241110692298ll, 81918995835971ll, 288743600016544ll, 1017747647730855ll, 3587301266941940ll, 12644323361314601ll, 44568019595356576ll});
	LL n;
	cin >> n;
	cout << nico::gao(a, n - 1) << endl;
	return 0;
}
0