結果
問題 | No.621 3 x N グリッド上のドミノの置き方の数 |
ユーザー |
|
提出日時 | 2017-12-21 01:38:35 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 3 ms / 3,000 ms |
コード長 | 2,333 bytes |
コンパイル時間 | 1,710 ms |
コンパイル使用メモリ | 168,820 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-12-16 07:32:23 |
合計ジャッジ時間 | 3,379 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 66 |
ソースコード
#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-thVI 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;}