結果
問題 | No.980 Fibonacci Convolution Hard |
ユーザー | snuke |
提出日時 | 2020-01-31 21:44:40 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 8,097 bytes |
コンパイル時間 | 2,836 ms |
コンパイル使用メモリ | 218,312 KB |
実行使用メモリ | 34,560 KB |
最終ジャッジ日時 | 2024-09-17 07:33:42 |
合計ジャッジ時間 | 11,738 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
ソースコード
#include <bits/stdc++.h> #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 srep(i,s,t) for (int i = s; i < t; ++i) #define rng(a) a.begin(),a.end() #define rrng(a) a.rbegin(),a.rend() #define maxs(x,y) (x = max(x,y)) #define mins(x,y) (x = min(x,y)) #define limit(x,l,r) max(l,min(x,r)) #define lims(x,l,r) (x = max(l,min(x,r))) #define isin(x,l,r) ((l) <= (x) && (x) < (r)) #define pb push_back #define eb emplace_back #define sz(x) (int)(x).size() #define pcnt __builtin_popcountll #define uni(x) x.erase(unique(rng(x)),x.end()) #define snuke srand((unsigned)clock()+(unsigned)time(NULL)); #define show(x) cout<<#x<<" = "<<x<<endl; #define PQ(T) priority_queue<T,v(T),greater<T> > #define bn(x) ((1<<x)-1) #define dup(x,y) (((x)+(y)-1)/(y)) #define newline puts("") #define v(T) vector<T> #define vv(T) v(v(T)) using namespace std; typedef long long int ll; typedef unsigned uint; typedef unsigned long long ull; typedef pair<int,int> P; typedef tuple<int,int,int> T; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<P> vp; typedef vector<T> vt; inline int in() { int x; scanf("%d",&x); return x;} template<typename T>inline istream& operator>>(istream&i,v(T)&v) {rep(j,sz(v))i>>v[j];return i;} template<typename T>string join(const v(T)&v) {stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);} template<typename T>inline ostream& operator<<(ostream&o,const v(T)&v) {if(sz(v))o<<join(v);return o;} template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v) {return i>>v.fi>>v.se;} template<typename T1,typename T2>inline ostream& operator<<(ostream&o,const pair<T1,T2>&v) {return o<<v.fi<<","<<v.se;} template<typename T>inline ll suma(const v(T)& a) { ll res(0); for (auto&& x : a) res += x; return res;} const double eps = 1e-10; const ll LINF = 1001002003004005006ll; const int INF = 1001001001; #define dame { puts("-1"); return 0;} #define yn {puts("Yes");}else{puts("No");} const int MX = 200005; // Mod int const int mod = 1000000007; // const int mod = 998244353; struct mint { ll x; mint():x(0){} mint(ll x):x((x%mod+mod)%mod){} // mint(ll x):x(x){} mint& fix() { x = (x%mod+mod)%mod; return *this;} mint operator-() const { return mint(0) - *this;} mint operator~() const { return mint(1) / *this;} 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){ (x*=a.pow(mod-2).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;} mint operator/(const mint& a)const{ return mint(*this) /= a;} mint pow(ll t) const { if(!t) return 1; mint res = pow(t/2); res *= res; return (t&1)?res*x:res; } bool operator<(const mint& a)const{ return x < a.x;} bool operator==(const mint& a)const{ return x == a.x;} }; mint ex(mint x, ll t) { return x.pow(t);} istream& operator>>(istream&i,mint&a){i>>a.x;return i;} ostream& operator<<(ostream&o,const mint&a){o<<a.x;return o;} typedef vector<mint> vm; struct comb { vm f, g; comb(){} comb(int mx):f(mx+1),g(mx+1) { f[0] = 1; rrep(i,mx) f[i] = f[i-1]*i; g[mx] = f[mx].pow(mod-2); for(int i=mx;i>0;i--) g[i-1] = g[i]*i; } mint c(int a, int b) { if (a < b) return 0; return f[a]*g[b]*g[a-b]; } }; // // NTT template<class T> T extgcd(T a, T b, T& x, T& y) { for (T u = y = 1, v = x = 0; a;) { T q = b / a; swap(x -= q * u, u); swap(y -= q * v, v); swap(b -= q * a, a); } return b; } template<class T> T mod_inv(T a, T m) { T x, y; extgcd(a, m, x, y); return (m + x % m) % m; } ll mod_pow(ll a, ll n, ll mod) { ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; } template<int mod, int primitive_root> class NTT { public: int get_mod() const { return mod; } void _ntt(vl& a, int sign) { const int n = sz(a); const int g = primitive_root; //g is primitive root of mod int h = (int)mod_pow(g, (mod - 1) / n, mod); // h^n = 1 if (sign == -1) h = (int)mod_inv(h, mod); //h = h^-1 % mod int i = 0; for (int j = 1; j < n - 1; ++j) { for (int k = n >> 1; k >(i ^= k); k >>= 1); if (j < i) swap(a[i], a[j]); } for (int m = 1; m < n; m *= 2) { const int m2 = 2 * m; const ll base = mod_pow(h, n / m2, mod); ll w = 1; rep(x, m) { for (int s = x; s < n; s += m2) { ll u = a[s]; ll d = a[s + m] * w % mod; a[s] = u + d; if (a[s] >= mod) a[s] -= mod; a[s + m] = u - d; if (a[s + m] < 0) a[s + m] += mod; } w = w * base % mod; } } for (auto& x : a) if (x < 0) x += mod; } void ntt(vl& input) { _ntt(input, 1); } void intt(vl& input) { _ntt(input, -1); const int n_inv = mod_inv(sz(input), mod); for (auto& x : input) x = x * n_inv % mod; } vl convolution(const vl& a, const vl& b){ int ntt_size = 1; while (ntt_size < sz(a) + sz(b)) ntt_size *= 2; vl _a = a, _b = b; _a.resize(ntt_size); _b.resize(ntt_size); ntt(_a); ntt(_b); rep(i, ntt_size){ (_a[i] *= _b[i]) %= mod; } intt(_a); return _a; } }; ll garner(vp mr, int mod){ mr.emplace_back(mod, 0); vl coffs(sz(mr), 1); vl constants(sz(mr), 0); rep(i, sz(mr) - 1){ // coffs[i] * v + constants[i] == mr[i].second (mod mr[i].first) ll v = (mr[i].second - constants[i]) * mod_inv<ll>(coffs[i], mr[i].first) % mr[i].first; if (v < 0) v += mr[i].first; for (int j = i + 1; j < sz(mr); j++) { (constants[j] += coffs[j] * v) %= mr[j].first; (coffs[j] *= mr[i].first) %= mr[j].first; } } return constants[sz(mr) - 1]; } typedef NTT<167772161, 3> NTT_1; typedef NTT<469762049, 3> NTT_2; typedef NTT<1224736769, 3> NTT_3; vl int32mod_convolution(vl a, vl b,int mod){ for (auto& x : a) x %= mod; for (auto& x : b) x %= mod; NTT_1 ntt1; NTT_2 ntt2; NTT_3 ntt3; auto x = ntt1.convolution(a, b); auto y = ntt2.convolution(a, b); auto z = ntt3.convolution(a, b); vl ret(sz(x)); vp mr(3); rep(i, sz(x)){ mr[0].first = ntt1.get_mod(), mr[0].second = (int)x[i]; mr[1].first = ntt2.get_mod(), mr[1].second = (int)y[i]; mr[2].first = ntt3.get_mod(), mr[2].second = (int)z[i]; ret[i] = garner(mr, mod); } return ret; } vl fast_int32mod_convolution(vl a, vl b,int mod){ for (auto& x : a) x %= mod; for (auto& x : b) x %= mod; NTT_1 ntt1; NTT_2 ntt2; NTT_3 ntt3; auto x = ntt1.convolution(a, b); auto y = ntt2.convolution(a, b); auto z = ntt3.convolution(a, b); const ll m1 = ntt1.get_mod(), m2 = ntt2.get_mod(), m3 = ntt3.get_mod(); const ll m1_inv_m2 = mod_inv<ll>(m1, m2); const ll m12_inv_m3 = mod_inv<ll>(m1 * m2, m3); const ll m12_mod = m1 * m2 % mod; vl ret(sz(x)); rep(i, sz(x)){ ll v1 = (y[i] - x[i]) * m1_inv_m2 % m2; if (v1 < 0) v1 += m2; ll v2 = (z[i] - (x[i] + m1 * v1) % m3) * m12_inv_m3 % m3; if (v2 < 0) v2 += m3; ll constants3 = (x[i] + m1 * v1 + m12_mod * v2) % mod; if (constants3 < 0) constants3 += mod; ret[i] = constants3; } return ret; } // int main() { int n, p; n = 2000000; cin>>p; int q; cin>>q; vm a(n); a[0] = 0; a[1] = 1; rep(i,n) { if (i < 2) continue; a[i] = a[i-1]*p+a[i-2]; } // vl x(n); // rep(i,n) x[i] = a[i].x; // vl d = fast_int32mod_convolution(x,x,mod); vm d(n); rep(i,5) { rep(j,i+1) d[i] += a[j]*a[i-j]; } srep(i,5,n) { d[i] = d[i-2]*p*p; d[i] += d[i-3]*p*2; d[i] += d[i-4]; d[i] += a[i]*a[0]*2; } rep(qi,q) { int s; cin>>s; s -= 2; cout<<d[s]<<endl; } return 0; }