hdu 6053 TrickGCD 筛法

时间:2022-12-20 15:27:42

TrickGCD

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

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 line 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
 
Recommend
liuyiding
题意:给你n个数字,每个位置的数字可以小于等于a[i],求所有gcd(l,r)都满足大于等于2的情况数;
思路:显然枚举gcd的情况,对于每个位置都有a[i]/gcd的个数可以满足条件,gcd的情况的所有a[i]/gcd的乘积;
   这个也需要优化,枚举除数,a[i]/gcd相同的为一块,nlong(n)的复杂度*快速幂的log,后面的用容斥筛一下就好了;
   莫比乌斯好像也可以。
 
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<bitset>
#include<time.h>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-4
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=1e9+,MOD=1e9+;
const LL INF=1e18+,mod=1e9+; int a[N],sum[N];
LL dp[N],num[N];
LL qpow(LL a,LL b,LL c)
{
LL ans=;
while(b)
{
if(b%)ans=(ans*a)%c;
b>>=;
a=(a*a)%mod;
}
return ans;
}
int main()
{
int n;
int T,cas=;
scanf("%d",&T);
while(T--)
{
memset(a,,sizeof(a));
memset(sum,,sizeof(sum));
scanf("%d",&n);
for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
a[x]++;
}
for(int i=;i<=;i++)
sum[i]=sum[i-]+a[i];
for(int i=;i<=;i++)//枚举除数
{
num[i]=1LL;
for(int j=;j<=;j+=i)
{
int b;
if(j+i->)b=sum[]-sum[j-];
else if(j==) b=sum[j+i-];
else b=sum[j+i-]-sum[j-];
int a=j/i;
if(a==&&b)num[i]=;
else if(b)num[i]=(num[i]*qpow(a,b,mod))%mod;
}
}
for(int i=;i>=;i--)
{
dp[i]=num[i];
for(int j=i+i;j<=;j+=i)
dp[i]-=dp[j],dp[i]=(dp[i]%mod+mod)%mod;
}
LL ans=;
for(int i=;i<=;i++)
ans+=dp[i],ans%=mod;
printf("Case #%d: %lld\n",cas++,ans); }
return ;
}