hdu6053 TrickGCD 莫比乌斯函数 容斥原理

http://acm.hdu.edu.cn/showproblem.php?pid=6053

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 ) ( 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  k th 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
 
题意:给你一个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;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值