HDU 6053 TrickGCD(莫比乌斯反演)

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?

* 1≤Bi≤Ai
* For each pair( l , r ) (1≤l≤r≤n) , gcd(bl,bl+1...br)≥2

Input

The first line is an integer T(1≤T≤10) 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 1≤n,Ai≤105

Output

For the kth 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

2017 Multi-University Training Contest - Team 2


题意:给出长度为n的a数列,求满足条件的B数组的个数,条件:①1<=b[i]<=a[i] ②区间整体gcd>=2
题解:

令g(d)为gcd为d的倍数的答案


根据容斥原理,去掉重复的情况(如g(2)+g(3)-g(6))



f(i,d)表示序列a中a/d=i的元素个数,可以通过预处理出前缀和+快速幂,之后利用预处理出莫比乌斯函数后进行简单的反演即可算出答案,总复杂度nlogn^2


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<math.h>
#include<queue>
#define INF 1e9
#define ll long long
#define maxn 300005
#include<set>
#include<stack>
using namespace std;
int n,m,mm;
int a[100005];
int mo[100005];
bool isprime[100005];
int num[100005];
ll mod=1e9+7;
void mobi()
{
    memset(isprime,true,sizeof(isprime));
    isprime[1]=false;
    for(int i=2;i<100005;i++)
        if(isprime[i])
        {
            mo[i]=-1;
            for(int j=2;i*j<100005;j++)
            {
                if(j%i==0) mo[i*j]=0;
                else mo[i*j]=-mo[j];
                isprime[i*j]=false;
            }
        }
}
ll quickpow(ll n,ll m)
{
    ll ans=1;
    n%=mod;
    while(m)
    {
        if(m%2==1)
        {
            ans=ans*n;
            ans=ans%mod;
            m=m-1;
        }
        else
        {
            n=(n*n)%mod;
            n%=mod;
            m=m/2;
        }
    }
    return ans%mod;
}
int main()
{
    int t;
    scanf("%d",&t);
    mobi();
    for(int w=1;w<=t;w++)
    {
        scanf("%d",&n);
        m=0;
        mm=100005;
        memset(num,0,sizeof(num));
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            m=max(a[i],m);
            mm=min(mm,a[i]);
            num[a[i]]++;
        }
        cout<<"Case #"<<w<<": ";
        for(int i=1;i<=m;i++)
            num[i]+=num[i-1];
        ll ans=0;
        for(int i=2;i<=mm;i++)
        {
            if(mo[i]==0) continue;
            ll sum=1;
            for(int j=1;j<=m/i;j++)
            {
                ll mi=1ll*(num[min(m,j*i+i-1)]-num[j*i-1]);
                sum*=quickpow(1ll*j,mi);
                sum%=mod;
            }
            ans+=sum*(-mo[i]);
            ans=(ans+mod)%mod;
        }
        cout<<ans%mod<<endl;

    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值