codeforces Unusual Sequences (数论)

D. Unusual Sequences
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Count the number of distinct sequences a1, a2, …, an (1 ≤ ai) consisting of positive integers such that gcd(a1, a2, …, an) = x and . As this number could be large, print the answer modulo 109 + 7.

gcd here means the greatest common divisor.

Input
The only line contains two positive integers x and y (1 ≤ x, y ≤ 109).

Output
Print the number of such sequences modulo 109 + 7.

Examples
input
3 9
output
3
input
5 8
output
0
Note
There are three suitable sequences in the first test: (3, 3, 3), (3, 6), (6, 3).

There are no suitable sequences in the second test.

问 构造 序列 使得 所有的gcd为x 和为y
首先根据我们根据隔板法可以知道 y的数构造序列的总数 为2^(y-1)
考虑 y/x 以后,构造所有的序列gcd为1,这里容斥一下就好,就是所有的情况,减去gcd为1个质数的情况+加上gcd为两个质数的情况…

#include 
using namespace std;

int num[13];
typedef long long ll;
const int mod = 1e9+7;

ll qpow(ll a,ll b)
{
    ll res=1;
    while(b)
    {
        if(b&1) res=res*a%mod;
        a*=a;
        a%=mod;
        b>>=1;
    }
    return res;
}

int main()
{
    int x,y;
    scanf("%d%d",&x,&y);
    if(y%x)
    {
        puts("0");
        return 0;
    }
    y/=x;
    int all=0;
    int yy=y;
    for(int i=2;i*i<=yy;i++)
    {
        if(yy%i==0)
        {
            num[all++]=i;
            while(yy%i==0) yy/=i;
        }
    }
    if(yy>1) num[all++]=yy;
    long long ans=0;
    for(int st=0;st<(1<int tmp=1,k=0;
        for(int i=0;iif(st&(1<if(k&1) ans=(ans-qpow(2ll,y/tmp-1)+mod)%mod;
        else ans=(ans+qpow(2ll,y/tmp-1))%mod;
    }
    printf("%lld\n",ans );
}

你可能感兴趣的:(思维,莫比乌斯函数,容斥原理)