hdu 6053 TrickGCD(容斥,分段,莫比乌斯函数)

You are given an array AA , and Zhu wants to know there are how many different array BB satisfy the following conditions?

  • 1≤Bi≤Ai1≤Bi≤Ai
  • For each pair( l , r ) (1≤l≤r≤n1≤l≤r≤n) , gcd(bl,bl+1…br)≥2gcd(bl,bl+1…br)≥2
    Input
    The first line is an integer T(1≤T≤101≤T≤10) describe the number of test cases.

Each test case begins with an integer number n describe the size of array AA.

Then a line contains nn numbers describe each element of AA

You can assume that 1≤n,Ai≤1051≤n,Ai≤105
Output
For the kkth 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 modmod 109+7109+7
Sample Input
1
4
4 4 4 4
Sample Output
Case #1: 17

题意:给你一个A序列,让你求有多少个b序列满足的条件是 1<=B[i]<=A[i];

一个比较朴素的想法是,所有的数都含有那么一个相同的因子>=2 而所有的素数的组合可以组合成任意的数,所以所有的数就是/2的数+/3的数-6 之类的重复的数,也就是组成的合数
所以就是很明显的容斥了,那么现在的问题是,数能到1e5,求所有的素数的组合再看每个数有多少个这样的数这个过程。。肯定会爆炸的

如何快速求出 两个办法 一个是桶装分段,看每一段有多少个数然后用一个快速幂就好。这样的时间复杂度是onlognlogn,或者用莫比乌斯函数也是一个nlognlogn
分段的思想就是 找出mi之前所有的素数
找到在1e5内 在素数t到t+1段的a[i]个数有多少个。然后就可以用快速幂了。然后对于每次on的查询可以优化到变成一个logn的复杂度 调和级数

#include <bits/stdc++.h>
using namespace std;

const int N = 1e6+10;
typedef long long ll;
int a[N+100];
int T[N+100];


int pri[N+100];
int pp[N+100];

int mi=1e9;
const int mod = 1e9+7;
int tk=0,mx;
const int MAXN=10000;  
void init()
{
    for(int i=2;i<=MAXN;i++)
    {
        if(pri[i]) continue;
        if(!pri[i]) pp[++tk]=i;
        for(int j=i+i;j<=MAXN;j+=i)
            pri[j]=1;
    }
}
ll ans;

ll qpow(ll a,ll b){  
    ll ret=1;  
    while(b){  
        if(b&1) ret=(ret*a)%mod;  
        a=(a*a)%mod;  
        b>>=1;  
    }  
    return ret;  
}  
void dfs(ll x,int p,int id){  
    if(x>mi) return;  
    if(id!=0){  
        ll ret=1;  
        ll ppp=x,cnt=1;  
        while(ppp<=mx) ret=(ret*qpow(cnt++,T[ppp]-T[ppp+x]))%mod,ppp=ppp+x;  
        if(p)ans=(ans+ret)%mod;  
        else ans=(ans-ret+mod)%mod;  
    }  
    for(int i=id+1;i<=tk;i++)  
        dfs(x*pp[i],p^1,i);  
}  

int main(){  
    int t,ca=1;  
    scanf("%d",&t);  
   init();  
   int n;
    while(t--){  

        scanf("%d",&n);  
        memset(T,0,sizeof(T));mi=1e9;ans=0;mx=-1e9;  
        for(int i=0;i<n;i++) scanf("%d",&a[i]),mi=min(mi,a[i]),mx=max(mx,a[i]),T[a[i]]+=1;  
        for(int i=N-5;i>0;i--) T[i]+=T[i+1];  
        dfs(1,0,0);  
        printf("Case #%d: %lld\n",ca++,(ans+mod)%mod);  
    }  
    return 0;  
}  

用莫比乌斯函数的话,前面也是一样的然后在容斥部分就不是单纯的利用个数判断+-,而是预处理了= = 反正就是预处理了你要选择的符号了,其实也就是特殊容斥,具体的还要继续学

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5+10;
typedef long long ll;
ll mu[N+100];
ll T[N+100];
int a[N+100];
const int mod = 1e9+7;

void mobi(int mn)
{
    mu[1]=1;
    for(int i=1;i<=mn;i++)
    {
        for(int j=i+i;j<=mn;j+=i)
        {
            mu[j]-=mu[i];
        }
    }
}


ll qpow(ll a,ll b)  
{  
    ll ans=1;  
    while(b)  
    {  
        if(b%2==1)ans=ans*a%mod;  
        a=(a*a)%mod;  
        b/=2;  
    }  
    return ans;  
}  


int main()
{
    int t;
    cin>>t;
    mobi(100000);
    int cas=1;
    while(t--)
    {
        memset(T,0,sizeof(T));
        int mi=1e9+7;
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            mi=min(a[i],mi);
            T[a[i]]++;
        }

        for(int i=1;i<=N;i++)
        {
            T[i]+=T[i-1];
        }

        ll ans=0;
        for(ll i=2;i<=mi;i++)
        {
            ll gg=1;
            for(ll j=1;j*i<=100000;j++)
            {

                gg=(gg*qpow(j,(T[(j+1)*i-1]-T[j*i-1])))%mod;

            }
            ans=(ans-mu[i]*gg%mod+mod)%mod;
        }
        printf("Case #%d: %lld\n",cas++,ans );
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值