1. 程式人生 > >莫比烏斯函式 hdu6053

莫比烏斯函式 hdu6053

TrickGCD

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2997    Accepted Submission(s): 1121


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 ) (1
lrn
) , 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 m
od
 109+7
Sample Input 1 4 4 4 4 4
Sample Output Case #1: 17
Source
#include<bits/stdc++.h>
using namespace std;
#define N 200011
const int mod=1e9+7;
typedef long long LL;
int mu[110000];
int MAXN=100000;
int a[110000];
int num[210000];
void Moblus()
{
    mu[1]=1;
    for(int i=1; i<=MAXN; i++)
        for(int j=i+i; j<=MAXN; j+=i)
            mu[j]-=mu[i];
}
LL qpow(LL a,LL b)
{
    LL ans=1;
    while(b)
    {
        if(b&1)
            ans=(ans*a)%mod;
        b>>=1;
        a=(a*a)%mod;
    }
    return ans;
}
int main()
{
    Moblus();
    int t;
    scanf("%d",&t);
    for(int k=1; k<=t; k++)
    {
        int i,j,n;
        scanf("%d",&n);
        memset(a,0,sizeof(a));
        int mn=100005;
        int mx=-1;
        for(i=0; i<n; i++)
        {
            int x;
            scanf("%d",&x);
            mn=min(x,mn);
            mx=max(x,mx);
            a[x]++;
        }
        num[0]=a[0];
        for(i=1; i<=100100; i++)
                num[i]=num[i-1]+a[i];
        LL sum=0;
        int l,r;
        for(i=2; i<=mn; i++)
        {
            LL ans=1;
            for(j=1; j*i<=mx; j++)
            {
                r=(j+1)*i-1;
                l=j*i-1;
                if(r>100000)
                    r=100000;
                ans=(ans*qpow(j,num[r]-num[l]))%mod;

            }
            sum=(sum-ans*mu[i]%mod+mod)%mod;
        }
        printf("Case #%d: %lld\n",k,sum);
    }
    return 0;
}