您的位置:首页 > 其它

2017 杭电多校联赛第二场 1009 TrickGCD(容斥原理) HDU 6053

2017-07-28 10:34 603 查看


TrickGCD

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

Total Submission(s): 875    Accepted Submission(s): 343


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

2017 Multi-University Training Contest
- Team 2

 

Recommend

liuyiding

题意:给出序列a,a[i]<=1e5,长度为n,问有多少种序列b 其中序列b满足
1<=b[i]<=a[i], b中任意一个[l,r] gcd(b[l],b[l+1]..b[r])>=2

设dp[x]:gcd为x的个数 则b中每个元素都为x的倍数 
b[i]<=a[i] 则第i个位置有a[i]/d种选择 直接累乘TLE.
若a[i]/d=k贡献为k,则和它相同贡献有cnt[kd,(k+1)d-1]个,则按段来枚举,算出该段贡献k^cnt.

最后容斥减掉gcd为jx的部分(j>1).O(nlog^2n)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+20;
const ll mod=1e9+7;
ll dp
,n,a
,cnt
;
ll powmod(ll x,ll n)
{
ll s=1;
while(n)
{
if(n&1)
s=(s*x)%mod;
n>>=1;
x=(x*x)%mod;
}
return s%mod;
}
int main()
{
int T;
cin>>T;
int cas=0;
while(T--)
{
scanf("%d",&n);
memset(cnt,0,sizeof(cnt));
memset(dp,0,sizeof(dp));
ll mx=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]),mx=max(mx,a[i]);
cnt[a[i]]++;
}
for(int i=1;i<=mx;i++)
cnt[i]+=cnt[i-1];
ll ans=0;
for(int i=mx;i>=2;i--)
{
ll res=1;
if(cnt[i-1])
{
dp[i]=0;
continue;
}
for(int j=i;j<=mx;j+=i)
{
ll num=cnt[min(mx,(ll)j+i-1)]-cnt[j-1];//[ki~(k+1)i)
ll x=j/i;
if(num)
res=(res*powmod(x,num))%mod;
}
dp[i]=res;
}
//容斥原理部分
for(int i=mx;i>=2;i--)
{
for(int j=i+i;j<=mx;j+=i)
dp[i]=(dp[i]-dp[j]+mod)%mod;
ans=(ans+dp[i])%mod;
}
printf("Case #%d: %lld\n",++cas,ans);
}
return 0;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息