1. 程式人生 > >hdu6053 TrickGCD 莫比烏斯函式 容斥原理

hdu6053 TrickGCD 莫比烏斯函式 容斥原理

TrickGCD

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


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 題意:給你一個a陣列,要求找出b陣列。b陣列滿足:1<=bi<=ai,gcd(b1,...,bn)>=2。求這樣的b陣列有多少個。

題解:gcd(b1,...,bn)=k,那麼在k下b陣列有a1/k*a2/k*...*an/k。根據容斥原理:ans=+(k=一個素數的積)-(k=兩個素數的積)+(k=三個素數的積)-...,其實可以看出前面的係數就是莫比烏斯函式的相反數。但是列舉每個k,再遍歷a陣列計算答案,複雜度就是o(n^2)。可以在遍歷a陣列上想辦法,設sum[m]表示1<=a[i]<=m的a[i]的個數。那麼對於每一個k,對答案的貢獻就是ans=1^(sum[2*k-1]-sum[k-1])*2^(sum[3*k-1]-sum[2*k-1])*3^(sum[4*k-1]-sum[3*k-1])*...,這樣就可以了。

程式碼:

#include<set>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<string>
#include<bitset>

#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>

#include<iomanip>
#include<iostream>

#define debug cout<<"aaa"<<endl
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define MIN_INT (-2147483647-1)
#define MAX_INT 2147483647
#define MAX_LL 9223372036854775807i64
#define MIN_LL (-9223372036854775807i64-1)
using namespace std;

const int N = 100000 + 5;
const int mod = 1000000000 + 7;
int mu[N],prime[N],a[N],sum[N];
int minn,maxn,cnt,t,n,cas=0;
LL ans,temp;
bool vis[N];

void Init(){
	mem(mu,0),mem(prime,0),mem(vis,0);
	mu[1]=1,cnt=0;
	for(int i=2;i<N;i++){
		if(!vis[i]){
			prime[cnt++]=i;
			mu[i]=-1;
		}
		for(int j=0;j<cnt&&i*prime[j]<N;j++){
			vis[i*prime[j]]=1;
			if(i%prime[j]) mu[i*prime[j]]=-mu[i];
			else{
				mu[i*prime[j]]=0;
				break;
			}
		}
	}
}

LL quick(LL a,LL b){
	LL re=1;
	while(b){
		if(b&1){
			re=(re*a)%mod;
		}
		b>>=1;
		a=(a*a)%mod; 
	}
	return re;
}

int main(){
	Init();
	scanf("%d",&t);
	while(t--){
		mem(sum,0),minn=MAX_INT,maxn=0,ans=0;
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			minn=min(minn,a[i]);
			maxn=max(maxn,a[i]);
			sum[a[i]]++;
		}
		for(int i=1;i<=maxn;i++){//儲存a[k]∈[1,i]的個數 
			sum[i]+=sum[i-1];
		}
		for(int i=2;i<=minn;i++){//列舉每個因子 
			temp=1;
			//篩法求每個因子對答案的貢獻 
			//ans=1^(sum[2*k-1]-sum[k-1])*2^(sum[3*k-1]-sum[2*k-1])*3^(sum[4*k-1]-sum[3*k-1])*...
			for(int j=i;j<=maxn;j+=i){
				temp=(temp*quick(j/i,sum[min((j+i-1),maxn)]-sum[j-1]))%mod;
			}
			ans=(ans-mu[i]*temp)%mod;
		}
		ans=(ans+mod)%mod;
		printf("Case #%d: %lld\n",++cas,ans);
	}
	return 0;
}