题目:
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 nn numbers describe each element of A
You can assume that 1≤n,Ai≤10^5
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 10^9+7’
题解:
枚举除数k,容斥+莫比乌斯函数,B序列的可能性有∑ ( ∏(k/Ai) ) 种
对Ai进行分组,k/Ai值相同的为一组,快速求解∏(k/Ai)
代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int mod = 1e9+7;
const int maxn = 1e5+10;
vector <int> w;
int T,n,a[maxn],pr[maxn],mu[maxn],Max,Min;
long long ans;
long long qpow(long long x,int b)
{
long long c = 1;
while (b)
{
if (b & 1) c = (c*x) % mod;
x = (x*x) % mod;
b >>= 1;
}
return (c % mod);
}
void getmu()//求莫比乌斯函数
{
int N = 1e5,num = 0;
bool fp[maxn];
memset(fp,0,sizeof(fp));
for (int i=2;i<=N;i++)
{
if (!fp[i])
{
pr[num++] = i;
mu[i] = -1;
}
for (int j=0;jint k = i * pr[j];
if (k > N) break;
fp[k] = 1;
if (i % pr[j] == 0)
{
mu[k] = 0;
break;
}
else
mu[k] = -mu[i];
}
}
for (int i=2;i<=N;i++)//利用莫比乌斯函数进行容斥
if (mu[i] != 0)
w.push_back(i);
return;
}
void solve()
{
for (int i=0;i//枚举除数
{
long long tot = 1;
for (int j=1;j*w[i]<=Max;j++)
{
int k = j * w[i];
int l = lower_bound(a,a+n,k)-a;
int r = lower_bound(a,a+n,k+w[i])-a;//分组
if (r > l)
tot = (tot * qpow(j,r-l)) % mod;
}
if (mu[w[i]] == 1)
ans = (ans - tot + mod) % mod;
else
ans = (ans + tot) % mod;
}
printf("%I64d\n",ans);
return;
}
int main()
{
getmu();
scanf("%d",&T);
for (int Case=1;Case<=T;Case++)
{
ans = 0;
scanf("%d",&n);
for (int i=0;iscanf("%d",&a[i]);
sort(a,a+n);
Min = a[0];//除数最多枚举到Min
Max = a[n-1];
printf("Case #%d: ",Case);
solve();
}
return 0;
}