1. 程式人生 > >HDU 6053 TrickGCD (莫比烏斯函式)

HDU 6053 TrickGCD (莫比烏斯函式)

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 mod109+7

Sample Input

1
4
4 4 4 4

Sample Output

Case #1: 17

題意

給出陣列 A ,問有多個種 B 陣列滿足所給條件。

思路

針對條件,我們可以列舉 gcd ,顯然對於每一個素因子 i 在範圍 [i,j] 下共有 ji 個數可以整除它,假設 A 中共有 cnt 個數字處於 [j,j+i1] 這個範圍內,這也就相當於有 cnt 個位置的數可以在 ji 個因子中隨意變動,共有 (ji)cnt 種方案,而對於每一個因子,其結果為 i|j(ji)cnt

最終通過容斥或者莫比烏斯去掉重複部分即可。

AC 程式碼

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include<iostream>
using namespace std ;

#define inf 0x3f3f3f
typedef __int64 LL;
const int maxn = 1e5+10;
const int mod = 1e9+7;
LL mu[maxn];
LL sum[maxn<<1];
LL cnt[maxn<<1];

void init()
{
    mu[1]=1;
    for(int i=1; i<maxn; i++)
        for(int j=i+i; j<maxn; j+=i)
            mu[j]-=mu[i];
}

LL mult(LL a,LL n)
{
    LL res=1;
    while(n)
    {
        if(n&1)res=(res*a)%mod;
        a=(a*a)%mod;
        n>>=1;
    }
    return res;
}

int main()
{
    ios::sync_with_stdio(false);
    init();
    int T;
    cin>>T;
    for(int ti=1; ti<=T; ti++)
    {
        int n,minn=inf;
        memset(cnt,0,sizeof(cnt));
        cin>>n;
        for(int i=0; i<n; i++)
        {
            int x;
            cin>>x;
            cnt[x]++;
            minn=min(minn,x);
        }
        LL ans=0;
        for(int i=1; i<maxn*2; i++)
            sum[i]=sum[i-1]+cnt[i];
        for(int i=2; i<=minn; i++)
        {
            LL temp=1LL;
            if(mu[i])
            {
                for(int j=i; j<maxn; j+=i)
                    temp=(temp*mult(j/i,sum[j+i-1]-sum[j-1]))%mod;
            }
            ans=(ans-temp*mu[i]+mod)%mod;
        }
        cout<<"Case #"<<ti<<": "<<ans<<endl;
    }
    return 0;
}