题目路径:
https://www.luogu.com.cn/problem/P4238
思路:
代码:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
using namespace std;
#define LL long long
const int N =4e5;
const int gg = 3, mod = 1e9+7;
LL a[N], b[N],g[N];
int n,tot,bit;
int rel[N];
LL gi = 0;
LL quick(LL a, LL b, LL mod)
{
LL ans = 1;
while (b)
{
if (b & 1) ans = ans * a % mod;
b = b >> 1;
a = a * a % mod;
}
return ans;
}
void ntt(LL a[], int tot,int op)
{
for (int i = 0; i < tot; i++) if (i < rel[i]) swap(a[i], a[rel[i]]);
for (int m = 2; m <= tot; m <<= 1)
{
LL g1 = quick(op == 1 ? gg : gi, (mod-1)/m, mod);
for (int i = 0; i < tot; i += m)
{
LL gk = 1;
for (int j = 0; j < m / 2; j++)
{
LL x = a[i + j], y = a[i + j + m / 2] * gk % mod;
a[i + j] = (x + y) % mod;
a[i + j + m / 2] = ((x - y)%mod + mod) % mod;
gk = gk * g1 % mod;
}
}
}
}
void solve(int len)
{
if (len == 1)
{
a[0] = quick(g[0], mod - 2, mod);
return;
}
solve((len + 1) >> 1);
bit = 0;
while ((1 << bit) <(len<<1)) bit++;
tot = 1 << bit;
for (int i = 0; i <len; i++) b[i] = g[i];
for (int i = len; i <tot; i++)b[i] = 0;
for (int i = 0; i < tot; i++) rel[i] = rel[i / 2] / 2 + ((i & 1) ? tot / 2 : 0);
ntt(a, tot, 1);
ntt(b, tot, 1);
for (int i = 0; i < tot; i++) a[i] = a[i]%mod * ((LL)2 - b[i] * a[i]%mod+mod) % mod;
ntt(a, tot, -1);
LL w = quick(tot, mod - 2, mod);
for (int i =0; i <len; i++)
a[i] = a[i] * w % mod;
for (int i = len; i < tot; i++) a[i] = 0;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> g[i];
for (int i = 0; i < n; i++) g[i] = g[i]%mod;
gi = quick(gg, mod - 2, mod);
while ((1 << bit) <n) bit++;
tot = 1<< bit;
solve(tot);
for (int i = 0; i < n; i++) cout << (a[i]%mod+mod)%mod<< " ";
cout << endl;
return 0;
}