結果
| 問題 |
No.980 Fibonacci Convolution Hard
|
| ユーザー |
risujiroh
|
| 提出日時 | 2020-01-31 21:52:55 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 5,282 bytes |
| コンパイル時間 | 1,994 ms |
| コンパイル使用メモリ | 183,808 KB |
| 実行使用メモリ | 519,312 KB |
| 最終ジャッジ日時 | 2024-09-17 07:59:41 |
| 合計ジャッジ時間 | 21,680 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | MLE * 17 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
namespace fft {
struct C {
double x, y;
C(double _x = 0, double _y = 0) : x(_x), y(_y) {}
};
C operator+(C l, C r) { return {l.x + r.x, l.y + r.y}; }
C operator-(C l, C r) { return {l.x - r.x, l.y - r.y}; }
C operator*(C l, C r) { return {l.x * r.x - l.y * r.y, l.x * r.y + l.y * r.x}; }
C operator~(C a) { return {a.x, -a.y}; }
vector<C> w{1};
void ensure(int n) {
for (int m = w.size(); m < n; m *= 2) {
C dw{cos(acos(0) / m), sin(acos(0) / m)};
w.resize(2 * m);
for (int i = 0; i < m; ++i) w[m + i] = w[i] * dw;
}
}
void fft(vector<C>& a, int n, bool inverse) {
assert((n & (n - 1)) == 0);
ensure(n);
if (not inverse) {
for (int m = n; m >>= 1; ) {
for (int s = 0, k = 0; s < n; s += 2 * m, ++k) {
for (int i = s, j = s + m; i < s + m; ++i, ++j) {
C x = a[i], y = a[j] * w[k];
a[i] = x + y, a[j] = x - y;
}
}
}
} else {
for (int m = 1; m < n; m *= 2) {
for (int s = 0, k = 0; s < n; s += 2 * m, ++k) {
for (int i = s, j = s + m; i < s + m; ++i, ++j) {
C x = a[i], y = a[j];
a[i] = x + y, a[j] = (x - y) * ~w[k];
}
}
}
double inv = 1.0 / n;
for (auto&& e : a) e.x *= inv, e.y *= inv;
}
}
void real_fft(vector<C>& a) {
if (a.size() < 2) return;
assert(a.size() % 2 == 0);
int n = a.size() / 2;
for (int i = 0; i < n; ++i) a[i] = {a[2 * i].x, a[2 * i + 1].x};
fft(a, n, false);
for (int s = n; s >>= 1; ) for (int i = s, j = 2 * s; j-- > s; ++i) {
C wa((1 + w[i].y) / 2, -w[i].x / 2), wb((1 - w[i].y) / 2, w[i].x / 2);
a[2 * i] = a[i] * wa + ~a[j] * wb, a[2 * j + 1] = ~a[2 * i];
}
a[1] = a[0].x - a[0].y, a[0] = a[0].x + a[0].y;
}
void real_ifft(vector<C>& a) {
if (a.size() < 2) return;
assert(a.size() % 2 == 0);
int n = a.size() / 2;
for (int i = 0; i < n; ++i) {
C wa((1 + w[i].y) / 2, w[i].x / 2), wb((1 - w[i].y) / 2, -w[i].x / 2);
a[i] = a[2 * i] * wa + a[2 * i + 1] * wb;
}
fft(a, n, true);
for (int i = n; i--; ) a[2 * i].x = a[i].x, a[2 * i + 1].x = a[i].y;
}
} // namespace fft
template <class T, class F = multiplies<T>>
T power(T a, long long n, F op = multiplies<T>(), T e = {1}) {
assert(n >= 0);
T res = e;
while (n) {
if (n & 1) res = op(res, a);
if (n >>= 1) a = op(a, a);
}
return res;
}
template <unsigned Mod> struct Modular {
using M = Modular;
unsigned v;
Modular(long long a = 0) : v((a %= Mod) < 0 ? a + Mod : a) {}
M operator-() const { return M() -= *this; }
M& operator+=(M r) { if ((v += r.v) >= Mod) v -= Mod; return *this; }
M& operator-=(M r) { if ((v += Mod - r.v) >= Mod) v -= Mod; return *this; }
M& operator*=(M r) { v = (uint64_t)v * r.v % Mod; return *this; }
M& operator/=(M r) { return *this *= power(r, Mod - 2); }
friend M operator+(M l, M r) { return l += r; }
friend M operator-(M l, M r) { return l -= r; }
friend M operator*(M l, M r) { return l *= r; }
friend M operator/(M l, M r) { return l /= r; }
friend bool operator==(M l, M r) { return l.v == r.v; }
};
template <unsigned Mod, size_t K = 2, int B = __lg(Mod) / K + 1>
array<vector<fft::C>, K> mint_fft(const vector<Modular<Mod>>& a, int sz) {
array<vector<fft::C>, K> res;
for (size_t p = 0; p < K; ++p) {
res[p].resize(sz);
for (int i = 0; i < (int)a.size(); ++i)
res[p][i] = (a[i].v >> (p * B)) & ((1 << B) - 1);
fft::real_fft(res[p]);
}
return res;
}
template <unsigned Mod, size_t N, int B = __lg(Mod) / ((N + 1) / 2) + 1>
vector<Modular<Mod>> mint_ifft(array<vector<fft::C>, N> a) {
int n = a[0].size();
vector<Modular<Mod>> res(n);
for (size_t p = 0; p < N; ++p) {
fft::real_ifft(a[p]);
auto base = power(Modular<Mod>(2), p * B);
for (int i = 0; i < n; ++i) res[i] += round(a[p][i].x) * base;
}
return res;
}
template <class T, size_t K> array<vector<T>, 2 * K - 1> operator*(
const array<vector<T>, K>& l, const array<vector<T>, K>& r) {
int n = l[0].size();
array<vector<T>, 2 * K - 1> res;
for (size_t p = 0; p < K; ++p) for (size_t q = 0; q < K; ++q) {
res[p + q].resize(n);
for (int i = 0; i < n; ++i)
res[p + q][i] = res[p + q][i] + l[p][i] * r[q][i];
}
return res;
}
template <unsigned Mod> vector<Modular<Mod>> operator*(
const vector<Modular<Mod>>& l, const vector<Modular<Mod>>& r) {
if (l.empty() or r.empty()) return {};
int n = l.size(), m = r.size(), sz = 1 << __lg(2 * (n + m - 1) - 1);
if (min(n, m) < 30) {
vector<long long> res(n + m - 1);
for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j)
res[i + j] += (l[i] * r[j]).v;
return {begin(res), end(res)};
}
bool eq = l == r;
auto a = mint_fft(l, sz), b = eq ? a : mint_fft(r, sz);
auto res = mint_ifft<Mod>(a * b);
return {begin(res), begin(res) + (n + m - 1)};
}
constexpr long long mod = 1e9 + 7;
using Mint = Modular<mod>;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int p;
cin >> p;
int n = 2e6;
vector<Mint> a(n);
for (int i = 0; i < n; ++i) {
if (i < 2) {
a[i] = i;
} else {
a[i] = p * a[i - 1] + a[i - 2];
}
}
a = a * a;
int q;
cin >> q;
while (q--) {
int i;
cin >> i;
i -= 2;
cout << a[i].v << '\n';
}
}
risujiroh