TrickGCD————(hdu6053)2017多校(莫比乌斯容斥)

来源:互联网 发布:拒绝退款后淘宝介入 编辑:程序博客网 时间:2024/06/04 18:50
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 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
144 4 4 4
 

Sample Output
Case #1: 17
题意:给出一个数列,然后让你求出另一个数列有多少种可能;
另一个数列满足数列gcd(b1,b2,……,bn)>=2而且对应的bi<=ai;
解题思路:从2开始枚举到min(a1,a2……,an),然后计算含有这个因子的数出现的个数就是
然后利用快速幂计算,然后会有重复计算,利用莫比乌斯容斥;
#include <iostream>#include <cstring>#include <cstdio>using namespace std;typedef long long ll;ll mu[100005],cas=1;ll num[200005];int a[100005];ll mod=1e9+7;void mobius(ll mn){    mu[1]=1;    for(ll i=1;i<=mn;i++){        for(ll j=i+i;j<=mn;j+=i){            mu[j]-=mu[i];        }    }}ll  Pow(ll x,ll n){    ll res=1;    while(n>0)    {        if(n&1)        res=res*x%mod;        x=x*x%mod;        n>>=1;    }    return res;}int main(){    int T,cas;    scanf("%d",&T);    cas=0;    mobius(100000);    while(T--)    {        cas++;        int n;        scanf("%d",&n);        memset(num,0,sizeof(num));        int minn=100005;        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);            num[a[i]]++;            minn=min(a[i],minn);        }        for(int i=1;i<=200000;i++)        num[i]+=num[i-1];        ll sum=0;        for(int i=2;i<=minn;i++)        {            ll su=1;            for(int j=1;i*j<=100000;j++)            {                su=(su*Pow(j,num[i*(1+j)-1]-num[i*j-1]))%mod;//好好想一想在这里            }            sum=(sum-(mu[i]*su)%mod+mod)%mod;        }        printf("Case #%d: %lld\n",cas,sum);    }    return 0;}
原创粉丝点击