TrickGCD(莫比乌斯函数,容斥)

题目:
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<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>

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;j<num;j++)
        {
            int 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<w.size() && w[i]<=Min;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;i<n;i++)
            scanf("%d",&a[i]);
        sort(a,a+n);
        Min = a[0];//除数最多枚举到Min
        Max = a[n-1];
        printf("Case #%d: ",Case);
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值