hdu 6053 TrickGCD(莫比乌斯反演)

Problem Description
You are given an array  A  , and Zhu wants to know there are how many different array  B  satisfy the following conditions?

1BiAi
* For each pair( l , r ) ( 1lrn ) ,  gcd(bl,bl+1...br)2
 

Input
The first line is an integer T( 1T10 ) describe the number of test cases.

Each test case begins with an integer number n describe the size of array  A .

Then a line contains  n  numbers describe each element of  A

You can assume that  1n,Ai105
 

Output
For the  k th test case , first output "Case #k: " , then output an integer as answer in a single line . because the answer may be large , so you are only need to output answer  mod   109+7
 

Sample Input
  
  
1 4 4 4 4 4
 

Sample Output
  
  
Case #1: 17
 

Source

题意:给你一个数组a,让你构造一个数组 b 满足  1 <= bi <= ai,且对于每一组( l , r ) (1≤l≤r≤n) , gcd(bl,bl+1...br)≥2。问有多少组不同的b数组

转化一下:等同于构造一个b数组,使得gcd(b1,b2,....,bn) >= 2。


设F(n) 为gcd是n的倍数的个数,f(n)为gcd是n的个数

所以答案就是 F(1) - f(1)。

可以发现 F(n) = sigma(f(d)) ,(n|d)

我们可以利用莫比乌斯反演求出f(1)。

莫比乌斯反演有两种形式,这里我们使用第二种:

一、

二、


那么剩下的问题就是怎么求F(d)了,F(d) = (a1 / d)*(a2 / n)*...*(an / d) = sigma(i ^ (sum[d*(i+1)-1] - sum[d*i-1]) ,其中 1 <= i <= mx(最大的ai),sum[i]表示a数组中小于等于i的数的数量。


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;

const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
int p[maxn/10];
int flag[maxn];
int mu[maxn];
int cnt = 0;
void init()
{
    int i,j;
    mu[1] = 1;
    for(i=2;i<maxn;i++)
    {
        if(!flag[i])
        {
            p[cnt++] = i;
            mu[i] = -1;
        }
        for(j=0;j<cnt&&p[j]*i<maxn;j++)
        {
            flag[p[j]*i] = 1;
            if(i % p[j] == 0)
            {
                mu[p[j]*i] = 0;
                break;
            }
            mu[p[j]*i] = -mu[i];
        }
    }
}

int a[100010];
int sum[100010];

LL quickpow(LL x,int k)
{
    LL ans = 1;
    while(k)
    {
        if(k&1)
            ans = ans*x%mod;
        x = x*x%mod;
        k /= 2;
    }

    return ans;
}

int main(void)
{
    int T,n,i,j;
    init();
    scanf("%d",&T);
    int cas = 1;
    while(T--)
    {
        scanf("%d",&n);
        int mi = 1e9;
        int mx = 0;
        memset(sum,0,sizeof(sum));
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            mi = min(mi,a[i]);
            mx = max(mx,a[i]);
            sum[a[i]]++;
        }
        sum[0] = 0;
        for(i=1;i<=mx;i++)
            sum[i] += sum[i-1];
        LL F1;
        LL f1 = 0;
        for(i=1;i<=mi;i++)
        {
            LL t = 1;
            for(j=1;j*i<=mx;j++)
            {
                t = t*quickpow(j,sum[min((j+1)*i-1,mx)]-sum[j*i-1])%mod;
            }
            if(i == 1)
                F1 = t;
            f1 = (f1 + mu[i]*t + mod)%mod;
        }
        LL ans = (F1 -f1 + mod)%mod;
        printf("Case #%d: %I64d\n",cas++,ans);
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值