2019 Nanchang Onsite

D.Interesting Series

F(n)实际上是一个等比数列的和,将它从递推式转变为通项公式(a^n-1)/(a-1),这里只需要确定n就可以。

题目要求选取k大小的所有子集的答案求和,可以先求a^n部分的和,把它写成母函数的形式(x+a^s1)(x+a^s2)...(x+a^sn),这样不断的分成两半,分别求出,然后再用fft卷积合并这两半,这一部分的和就是x^(n-k)的系数。

分母的a-1实际上还是a-1,分子的-1部分要变成C(n,k)

由于之前没怎么写过,fft的板子代码借鉴群里大佬的。

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <set>
#include <cmath>
#include <queue>
#include <map>
#include <cassert>
const int maxn = 1e5 + 10;
const int mod = 1e5 + 3;
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
namespace fft
{
struct num
{
    double x, y;
    num()
    {
        x = y = 0;
    }
    num(double x, double y) : x(x), y(y) {}
};
inline num operator+(num a, num b)
{
    return num(a.x + b.x, a.y + b.y);
}
inline num operator-(num a, num b)
{
    return num(a.x - b.x, a.y - b.y);
}
inline num operator*(num a, num b)
{
    return num(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
inline num conj(num a)
{
    return num(a.x, -a.y);
}

int base = 1;
vector<num> roots = {{0, 0}, {1, 0}};
vector<int> rev = {0, 1};
const double PI = acosl(-1.0);

void ensure_base(int nbase)
{
    if (nbase <= base)
        return;
    rev.resize(1 << nbase);
    for (int i = 0; i < (1 << nbase); i++)
        rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1));
    roots.resize(1 << nbase);
    while (base < nbase)
    {
        double angle = 2 * PI / (1 << (base + 1));
        for (int i = 1 << (base - 1); i < (1 << base); i++)
        {
            roots[i << 1] = roots[i];
            double angle_i = angle * (2 * i + 1 - (1 << base));
            roots[(i << 1) + 1] = num(cos(angle_i), sin(angle_i));
        }
        base++;
    }
}

void fft(vector<num> &a, int n = -1)
{
    if (n == -1)
        n = a.size();
    assert((n & (n - 1)) == 0);
    int zeros = __builtin_ctz(n);
    ensure_base(zeros);
    int shift = base - zeros;
    for (int i = 0; i < n; i++)
        if (i < (rev[i] >> shift))
            swap(a[i], a[rev[i] >> shift]);
    for (int k = 1; k < n; k <<= 1)
    {
        for (int i = 0; i < n; i += 2 * k)
        {
            for (int j = 0; j < k; j++)
            {
                num z = a[i + j + k] * roots[j + k];
                a[i + j + k] = a[i + j] - z;
                a[i + j] = a[i + j] + z;
            }
        }
    }
}

vector<num> fa, fb;

vector<int> multiply_mod(vector<int> &a, vector<int> &b, int m, int eq = 0)
{
    int need = a.size() + b.size() - 1;
    int nbase = 0;
    while ((1 << nbase) < need)
        nbase++;
    ensure_base(nbase);
    int sz = 1 << nbase;
    if (sz > (int)fa.size())
        fa.resize(sz);
    for (int i = 0; i < (int)a.size(); i++)
    {
        int x = (a[i] % m + m) % m;
        fa[i] = num(x & ((1 << 15) - 1), x >> 15);
    }
    fill(fa.begin() + a.size(), fa.begin() + sz, num{0, 0});
    fft(fa, sz);
    if (sz > (int)fb.size())
        fb.resize(sz);
    if (eq)
        copy(fa.begin(), fa.begin() + sz, fb.begin());
    else
    {
        for (int i = 0; i < (int)b.size(); i++)
        {
            int x = (b[i] % m + m) % m;
            fb[i] = num(x & ((1 << 15) - 1), x >> 15);
        }
        fill(fb.begin() + b.size(), fb.begin() + sz, num{0, 0});
        fft(fb, sz);
    }
    double ratio = 0.25 / sz;
    num r2(0, -1), r3(ratio, 0), r4(0, -ratio), r5(0, 1);
    for (int i = 0; i <= (sz >> 1); i++)
    {
        int j = (sz - i) & (sz - 1);
        num a1 = (fa[i] + conj(fa[j]));
        num a2 = (fa[i] - conj(fa[j])) * r2;
        num b1 = (fb[i] + conj(fb[j])) * r3;
        num b2 = (fb[i] - conj(fb[j])) * r4;
        if (i != j)
        {
            num c1 = (fa[j] + conj(fa[i]));
            num c2 = (fa[j] - conj(fa[i])) * r2;
            num d1 = (fb[j] + conj(fb[i])) * r3;
            num d2 = (fb[j] - conj(fb[i])) * r4;
            fa[i] = c1 * d1 + c2 * d2 * r5;
            fb[i] = c1 * d2 + c2 * d1;
        }
        fa[j] = a1 * b1 + a2 * b2 * r5;
        fb[j] = a1 * b2 + a2 * b1;
    }
    fft(fa, sz);
    fft(fb, sz);
    vector<int> res(need);
    for (int i = 0; i < need; i++)
    {
        ll aa = fa[i].x + 0.5;
        ll bb = fb[i].x + 0.5;
        ll cc = fa[i].y + 0.5;
        res[i] = (aa + ((bb % m) << 15) + ((cc % m) << 30)) % m;
    }
    return res;
}
vector<int> square_mod(vector<int> &a, int m)
{
    return multiply_mod(a, a, m, 1);
}
}; // namespace fft
inline int quick(int a, int b, int m)
{
    int ans = 1;
    while (b)
    {
        if (b & 1)
        {
            ans = (1LL * a * ans) % m;
        }
        a = (1LL * a * a) % m;
        b >>= 1;
    }
    return ans;
}
vector<int> v[maxn];
inline ll inverse(ll a, ll p) { return quick(a, p - 2, p); }
vector<int> solve(int l, int r)
{
    if (l == r)
        return v[l];
    int mid = (l + r) >> 1;
    vector<int> v1 = (solve(l, mid));
    vector<int> v2 = solve(mid + 1, r);
    return fft::multiply_mod(v1, v2, mod);
}
int comb[maxn];
int main()
{
    int n, a, q;
    scanf("%d%d%d", &n, &a, &q);
    comb[0] = 1;
    for (int i = 1; i <= n; i++)
    {
        comb[i] = 1LL * comb[i - 1] * (n + 1 - i) * inverse(i, mod) % mod;
    }
    for (int i = 1, s; i <= n; i++)
    {
        scanf("%d", &s);
        v[i].push_back(quick(a % mod, s, mod));
        v[i].push_back(1);
    }
    vector<int> ans = solve(1, n);
    while (q--)
    {
        int k;
        scanf("%d", &k);
        int cnt = 1LL * (ans[n - k] - comb[k]) * inverse(a - 1, mod) % mod;
        if (cnt < 0)
            cnt += mod;
        printf("%d\n", cnt);
    }
    return 0;
}
View Code

 

posted @ 2019-09-09 16:23  ACforever  阅读(206)  评论(0编辑  收藏  举报