codeforces 645F (莫比乌斯反演)

题目链接:点击这里

题意:给出初始n个数,问每次增加一个数字后的k元组的gcd之和.

只需要求出gcd分别等于1-1e6的k元组数. 设 F[x] 表示gcd等于x的倍数的k元组数量, f[x] 表示gcd等于x的k元组数量,那么就有:

f[1]=μ(1)F[1]+μ(2)F[2]+μ(3)F[3]++μ[n]F[n]f[2]=μ(1)F[2]+μ(2)F[4]+μ(3)F[6]+μ(k)F[2k]f[n]=μ(1)F[n]

所求的结果就是 1e6i=1if[i] ,每次插入一个数x,F[x]就会有所改变,直接把所有的F[x]的系数相加乘以变化量即可(可以nlgn预处理每一个x的mu函数*i的系数和).

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#pragma comment(linker, "/STACK:102400000,102400000")
#define Clear(x,y) memset (x,y,sizeof(x))
#define Close() ios::sync_with_stdio(0)
#define Open() freopen ("more.in", "r", stdin)
#define get_min(a,b) a = min (a, b)
#define get_max(a,b) a = max (a, b);
#define y0 yzz
#define y1 yzzz
#define fi first
#define se second
#define pii pair
#define pli pair
#define pll pair
#define pb push_back
#define pl c<<1
#define pr (c<<1)|1
#define lson tree[c].l,tree[c].mid,pl
#define rson tree[c].mid+1,tree[c].r,pr
#define mod 1000000007
typedef unsigned long long ull;
template <class T> inline T lowbit (T x) {return x&(-x);}
template <class T> inline T sqr (T x) {return x*x;}
template <class T>
inline bool scan (T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return 0; //EOF
    while (c != '-' && (c < '0' || c > '9') ) c = getchar();
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return 1;
}
const double pi = 3.14159265358979323846264338327950288L;
using namespace std;
#define INF 1e17
#define maxn 1000005
#define maxm 1000005
//-----------------morejarphone--------------------//

long long prime[maxn], tol;
bool vis[maxn];
int mu[maxn];
vector <long long> fac[maxn];//每个数的因子
long long sum[maxn];//每个数的因子和
long long g[maxn];//每个数的因子的mu函数之和
int n, k, q;
long long cnt[maxn];//每个数的倍数计数

void init () {
    tol = 0;
    memset (vis, 0, sizeof vis);
    mu[1] = 1;
    for (long long i = 2; i <= 1000000; i++) {
        if (!vis[i]) {
            prime[tol++] = i;
            mu[i] = -1;
        }
        for (long long j = 0; j < tol; j++) {
            if (i*prime[j] > 1000000)
                break;
            vis[i*prime[j]] = 1;
            if (i%prime[j] == 0) {
                mu[i*prime[j]] = 0;
                break;
            }
            else {
                mu[i*prime[j]] = -mu[i];
            }
        }
    }
    Clear (cnt, 0);
    Clear (sum, 0);
    Clear (g, 0);
    for (int i = 1; i <= 1000000; i++) fac[i].clear ();
    for (int i = 1; i <= 1000000; i++) { 
        for (int j = i; j <= 1000000; j += i) {
            fac[j].pb (i);
            sum[j] += i;
            g[j] += mu[i]*(j/i)%mod;
            g[j] = (g[j]+mod) % mod;
        }
    }
}

long long qpow (long long a, long long b) {
    if (b == 0) return 1;
    long long ans = qpow (a, b>>1);
    ans = ans*ans%mod;
    if (b&1) ans = ans*a%mod;
    return ans;
}

long long F[maxn], f[maxn];
long long A[maxn], rev[maxn];

long long C (long long m, long long n) {
    if (m < n) return 0;
    return A[m]*rev[n] %mod *rev[m-n]%mod;
}

void solve () {
    A[0] = 1;
    for (int i = 1; i < maxn; i++) A[i] = A[i-1]*i%mod;
    rev[maxn-1] = qpow (A[maxn-1], mod-2);
    for (int i = maxn-2; i >= 0; i--) rev[i] = rev[i+1]*(i+1)%mod;
    for (int i = 1; i <= 1000000; i++) {
        F[i] = C (cnt[i], k);
    }
    long long ans = 0;
    for (int i = 1; i <= 1000000; i++) {
        f[i] = 0;
        for (int j = i; j <= 1000000; j += i) {
            f[i] += F[j]*mu[j/i];
            f[i] = (f[i]%mod + mod) % mod;
        }
        ans = (ans + f[i]*i%mod) % mod;
    }
    while (q--) {
        long long num; scan (num);
        for (int i = 0; i < fac[num].size (); i++) {
            int j = fac[num][i];
            long long more = C (cnt[j]+1, k) - C (cnt[j], k) + mod; more %= mod;
            if (more) {
                ans += g[j]*more%mod;
                ans = (ans + mod) % mod;
            }
            cnt[j]++;
        }
        printf ("%lld\n", ans);
    }
}

long long a[100005];

int main () {
    //Open ();
    init ();
    scanf ("%d%d%d", &n, &k, &q);
    for (int i = 1; i <= n; i++) {
        scanf ("%d", &a[i]); 
        for (int j = 0; j < fac[a[i]].size (); j++) {
            cnt[fac[a[i]][j]]++;
        }
    }
    solve ();
    return 0;
}

你可能感兴趣的:(组合数学)