【CF900D】 Unusual Sequences

题目

题目描述
Count the number of distinct sequences a_{1},a_{2},…,a_{n}a
1

,a
2

,…,a
n

( 1<=a_{i}1<=a
i

) consisting of positive integers such that gcd(a_{1},a_{2},…,a_{n})=xgcd(a
1

,a
2

,…,a
n

)=x and . As this number could be large, print the answer modulo 10^{9}+710
9
+7 .

gcdgcd here means the greatest common divisor.

输入格式
The only line contains two positive integers xx and yy ( 1<=x,y<=10^{9}1<=x,y<=10
9
).

输出格式
Print the number of such sequences modulo 10^{9}+710
9
+7 .

题意翻译
输入x,y,求有多少个数列满足其gcd为x,和为y。

输入输出样例
输入 #1复制
3 9
输出 #1复制
3
输入 #2复制
5 8
输出 #2复制
0
说明/提示
There are three suitable sequences in the first test: (3,3,3)(3,3,3) , (3,6)(3,6) , (6,3)(6,3) .

There are no suitable sequences in the second test.

思路

首先如果y%x!=0那么一定无解
问题转换成求t=y/x满足gcd(A[])=1且sum(A[])=t的A序列个数
先不考虑gcd=1那么我们就可以用个办法解决
加上gcd就是经典的反演
当然还有荣吃的做法

代码

#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
map<int,int> mp;

int power(int x,int p) {
    int ret=1;
    for(;p;p>>=1,x=1LL*x*x%mod) if(p&1) ret=1LL*ret*x%mod;
    return ret;
}
void upd(int &x,int y) {
    (x+=y)>=mod&&(x-=mod);
}
int solve(int n) {
    if(n==1) return 1;
    if(mp.count(n)) return mp[n];
    int ans=0;
    for(int i=2;i*i<=n;++i) {
        if(n%i==0) {
            upd(ans,solve(i));
            if(i*i!=n) upd(ans,solve(n/i));
        }
    }
    upd(ans,solve(1));
    ans=(power(2,n-1)-ans+mod)%mod;
    return mp[n]=ans;
}
int main() 
{
    int x,y;
    scanf("%d%d",&x,&y);
    if(y%x) return puts("0"),0;
    printf("%d\n",solve(y/x));
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值