您的位置:首页 > 其它

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

2017-08-08 13:34 357 查看
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?

* 1≤Bi≤Ai

* For each pair( l , r ) (1≤l≤r≤n)
, gcd(bl,bl+1...br)≥2

 

Input

The first li
101ed
ne is an integer T(1≤T≤10)
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 1≤n,Ai≤105

 

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

1
4
4 4 4 4

 

Sample Output

Case #1: 17

 

Source

2017 Multi-University Training Contest - Team 2

 
题意:给你一个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
,prime
,a
,sum
;
int minn,maxn,cnt,t,n,cas=0;
LL ans,temp;
bool vis
;

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: