題目連結:
HDU 6053
題意:
給出你一個長度為n 的A 數組,讓你構造出一個長度也為n 的B 數組,且B 數組要滿足對于所有的1<=i<=n,A[i]<=B[i] ,且對于B 數組,任意一個區間的gcd>=2 ,求滿足的方案數。
題解:
容斥原理+莫比烏斯函數。
答案就是∑ min(A) i=2 −mu(i)∗(∏ n i=1 ⌊A i k ⌋) 。
對于f(k) 那麼任意B 都應該含有k 這個因子。
那麼B i 可選擇的方案數就是⌊A i k ⌋ .
根據乘法原理:
f(k)=∏ n i=1 ⌊A i k ⌋ 。
因為gcd(1,n)>=2 ,那麼我們可以考慮分别計算gcd 是2 的倍數,gcd 是3 的倍數…gcd 是k (k 為質數)的倍數。然後相加,但這樣會有重複,是以我們隻要在系數上乘上一個莫比烏斯函數就可以了。然後,如果直接枚舉每一個質數為作gcd ,肯定會T ,O(n 2 ) 。我們可以考慮預處理A 數組中每個數的個數并做字首和,然後再O(nlogn) 枚舉A i k 。
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
typedef long long ll;
int mu[100005];
void mobius(int n)
{
mu[1]=1;
for(int i=1;i<=n;i++){
for(int j=i+i;j<=n;j+=i){
mu[j] -= mu[i];
}
}
}
int cnt[200005];
ll ans;
ll q_mod(ll x,int y)
{
ll res=1;
while(y)
{
if(y&1){
res = res * x % mod;
}
y >>= 1;
x = x * x % mod;
}
return res;
}
int main()
{
int cas = 0;
int n,t,Min;
mobius(100000);
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
Min=100000;
for(int i=0;i<=200000;i++) cnt[i]=0;
while(n--)
{
int x;
scanf("%d",&x);
cnt[x]++;
Min=min(Min,x);
}
ll ans=0;
for(int i=1;i<=200000;i++)
{
cnt[i]+=cnt[i-1];
}
for(int i=2;i<=Min;i++)
{
ll temp=1;
for(int j=1;i*j<=100000;j++)
{
temp=(temp * q_mod(1LL*j,cnt[i*j+i-1]-cnt[i*j-1]))%mod;
}
ans=(ans-temp*mu[i]+mod)%mod;
}
printf("Case #%d: ",++cas);
printf("%lld\n",ans);
}
return 0;
}