結果
| 問題 |
No.980 Fibonacci Convolution Hard
|
| コンテスト | |
| ユーザー |
はまやんはまやん
|
| 提出日時 | 2020-02-01 10:14:14 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 4,910 bytes |
| コンパイル時間 | 3,078 ms |
| コンパイル使用メモリ | 207,872 KB |
| 最終ジャッジ日時 | 2025-01-08 21:40:52 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | TLE * 17 |
ソースコード
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
// 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(vector<ll>& a, int sign) {
const int n = a.size();
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, 0, 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(vector<ll>& input) {
_ntt(input, 1);
}
void intt(vector<ll>& input) {
_ntt(input, -1);
const int n_inv = mod_inv((int)input.size(), mod);
for (auto& x : input) x = x * n_inv % mod;
}
vector<ll> convolution(const vector<ll>& a, const vector<ll>& b) {
int ntt_size = 1;
while (ntt_size < a.size() + b.size()) ntt_size *= 2;
vector<ll> _a = a, _b = b;
_a.resize(ntt_size); _b.resize(ntt_size);
ntt(_a);
ntt(_b);
rep(i, 0, ntt_size) {
(_a[i] *= _b[i]) %= mod;
}
intt(_a);
return _a;
}
};
ll garner(vector<pair<int,int>> mr, int mod) {
mr.emplace_back(mod, 0);
int sz = mr.size();
vector<ll> coffs(sz, 1);
vector<ll> constants(sz, 0);
rep(i, 0, sz - 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; j++) {
(constants[j] += coffs[j] * v) %= mr[j].first;
(coffs[j] *= mr[i].first) %= mr[j].first;
}
}
return constants[sz - 1];
}
typedef NTT<167772161, 3> NTT_1;
typedef NTT<469762049, 3> NTT_2;
typedef NTT<1224736769, 3> NTT_3;
vector<ll> int32mod_convolution(vector<ll> a, vector<ll> 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);
int sz = x.size();
vector<ll> ret(sz);
vector<pair<int,int>> mr(3);
rep(i, 0, sz) {
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;
}
/*---------------------------------------------------------------------------------------------------
∧_∧
∧_∧ (´<_` ) Welcome to My Coding Space!
( ´_ゝ`) / ⌒i @hamayanhamayan0
/ \ | |
/ / ̄ ̄ ̄ ̄/ |
__(__ニつ/ _/ .| .|____
\/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/
int p, Q;
int mo = 1000000007;
const int ma = 2010101;
//---------------------------------------------------------------------------------------------------
void _main() {
cin >> p;
vector<ll> v(ma, 0);
v[2] = 1;
rep(i, 3, ma) v[i] = (1LL * v[i - 1] * p + v[i - 2]) % 1000000007;
auto ans = int32mod_convolution(v, v, 1000000007);
cin >> Q;
rep(_, 0, Q) {
int q; cin >> q;
printf("%lld\n", ans[q]);
}
}
はまやんはまやん