首页 > 代码库 > bzoj2194

bzoj2194

http://www.lydsy.com/JudgeOnline/problem.php?id=2194

卷积。。。

卷积并不高深,其实卷积就是两个多项式相乘的系数,但是得满足一点条件,就是f[n]=a[i]*b[n-i],就是下标和固定。。。然后这道题下标和不固定,但是我们把b反过来,就是一个卷积了。每次和是固定的

但是输出的时候得输出从n-2n,因为c[n+k]=a[i]*b[n+k-i],n<=n+k<=2*n

技术分享
#include<bits/stdc++.h>
using namespace std;
#define pi acos(-1)
const int N = 600010;
complex<double> a[N], b[N];
int n, m, l;
int r[N];
void fft(complex<double> *a, int f)
{
    for(int i = 0; i <= n; ++i) if(i < r[i]) swap(a[i], a[r[i]]);
    for(int i = 1; i < n; i <<= 1)
    {
        complex<double> w(cos(pi / i), f * sin(pi / i));
        for(int p = i << 1, j = 0; j <= n; j += p)
        {
            complex<double> t(1, 0);
            for(int k = 0; k < i; ++k, t = t * w)
            {
                complex<double> x = a[j + k], y = t * a[j + k + i];
                a[j + k] = x + y; a[j + k + i] = x - y;
            }
        }
    }
}
int main()
{
    scanf("%d", &n); --n;
    for(int i = 0; i <= n; ++i) 
    {
        int x, y; scanf("%d%d", &x, &y); a[i] = x; b[n - i] = y;
    }
    m = 2 * n; for(n = 1; n <= m; n <<= 1) ++l;
    for(int i = 0; i <= n; ++i) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1));
    fft(a, 1); fft(b, 1);
    for(int i = 0; i <= n; ++i) a[i] = a[i] * b[i];
    fft(a, -1);
    for(int i = m / 2; i <= m; ++i) printf("%d\n", (int)(a[i].real() / n + 0.5)); 
    return 0;
}
View Code

 

bzoj2194