TrickGCD----筛法/分块/莫比乌斯函数

来源:互联网 发布:中传在线 网络教育 编辑:程序博客网 时间:2024/06/02 02:54

TrickGCD

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1872    Accepted Submission(s): 719


Problem Description
You are given an array A , and Zhu wants to know there are how many different array B satisfy the following conditions?

* 1BiAi
* For each pair( l , r ) (1lrn) , gcd(bl,bl+1...br)2
 

Input
The first line is an integer T(1T10) 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 1n,Ai105
 

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 answermod109+7
 

Sample Input
144 4 4 4
 

Sample Output
Case #1: 17
 

Source
2017 Multi-University Training Contest - Team 2 

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6053


终于还是做了这个题,前两天宇航和兴志问我这个题的反演做法怎么做,我推了一下,没有推出来,当时手中还有别的题我就没有细想这个题,昨天学了比格思茅,但是没有太懂,今早从ACfun中有了许些感悟,zyyyyy给我推荐了这个题,我见了这个题三次了,终于静下心来好好的推一下这个题了。

做完这个题才发现,本以为宇航和兴志是卡在了反演上,但是这个题的重点并不在反演,而是在与筛法的思想和分块的方法,而且反演只用了莫比乌斯函数。


这个题的意思是说给出n个数a[i],每个数可以变成不大于它的数b[i],现问所有数的gcd大于1的方案数。其中(n,a[i]<=1e5)。

由于a[i]不大,可以想到枚举gcd的值。考虑一个gcd(a_1,a_2,a_3…a_n)=d,我们考虑d的贡献,显然每个a[i]的倍数都满足前面的式子,但是所有的d计入会有重复情况,所以我们考虑容斥的方法,首先对d进行素数分解,发现重复情况就是d的互异素数个数为偶数的,或是素数的指数大于1的。

然后涉及到莫比乌斯的知识点就出来了,你会惊讶的发现上述情况的系数和莫比乌斯函数很像,mu(d)=(-1)^k。然后我们考虑如何快速的求出d的贡献。

我从纸上推出来了一个公式

i=2min(a)μ(i)j=1naji

(符号不会打,从网上找了好长时间的这个公式,样子和我推的不太一样,但是一个意思)

里面的u[i]为莫比乌斯函数,这个我们已经解决掉了。(我就推到这里,下面来自大牛的思路)



感觉类似于筛法的思想,分块的方法有多种,在题中分块也算挺常见的吧。

代码:

#include <cstdio>#include <iostream>#include <cstring>#define LL long longusing namespace std;const int mod=1e9+7;const int maxn=2e5+8;int sum[maxn],prime[maxn],mu[maxn],cnt;void mobius(){    cnt=0;    mu[1]=1;    memset(prime,0,sizeof(prime));    for(int i=2;i<maxn;++i){        if(!prime[i]){            prime[cnt++]=i;            mu[i]=-1;        }        for(int j=0;j<cnt&&i*prime[j]<maxn;++j){            prime[i*prime[j]]=1;            if(i%prime[j])mu[prime[j]*i]=-mu[i];            else{                mu[i*prime[j]]=0;                break;            }        }    }}LL POW(LL a,LL n){    a%=mod;    LL res=1;    while(n>0){        if(n&1)            res=res*a%mod;        a=a*a%mod;        n>>=1;    }    return res;}int main(){    mobius();    int t,test=0;    scanf("%d",&t);    while(t--){        test++;        int n;        scanf("%d", &n);        int mn=1e5+5,mx=-1;        memset(sum,0,sizeof sum);        for(int i=0;i<n;++i){            int x;            scanf("%d",&x);            ++sum[x];            if(mn>x)                mn=x;            if(mx<x)                mx=x;        }        for(int i=1;i<maxn;++i) {            sum[i]+=sum[i-1];        }        LL res=0;        for(int i=2;i<=mn;++i){            if(!mu[i])                continue;            LL tmp=1;            for(int j=1;i*j<=mx;++j)                tmp=tmp*POW(j,sum[i*j+(i-1)]-sum[i*j-1])%mod;            res=(res-mu[i]*tmp+mod)%mod;        }        printf("Case #%d: %lld\n",test,res);    }    return 0;}


原创粉丝点击