CF900D:Unusual Sequences(数学)

本文探讨了一个数学问题,即计算特定条件下由正整数组成的序列数量。条件包括序列元素的和与最大公约数,以及结果的模运算。文章提供了一种解决策略,包括将问题转化为求解特定数的组合方案,并通过编程实现。
部署运行你感兴趣的模型镜像

D. Unusual Sequences
time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard 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.


题意:将Y分解成若干个数相加,且这些数的GCD为X的方案数。

思路:显然Y必须能被X整除,且这些数都能被X整除,那么我们把Y拆成T=Y/X个X,这T个块可以任意组合,但GCD必须为X。先考虑将T拆成若干个数相加的方案数,用隔板法得pow(2, T-1),这就是GCD为X的倍数的方案数,那么我们减去2*X,3*X......的方案数即可,可以容斥原理实现,不过倒着计算往往会简单一点。
 

# include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod = 1e9+7;
LL a[2000],dp[2000];
LL qmod(LL y)
{
    LL res = 1, two = (LL)2;
    for(;y;y>>=1)
    {
        if(y&1) res = res*two%mod;
        two = two*two%mod;
    }
    return res;
}
int main()
{
    LL x, y;
    int cnt=0;
    scanf("%lld%lld",&x,&y);
    if(y%x) return 0*puts("0");
    for(LL i=1; i<=sqrt(y); ++i)
    {
        if(y%i==0)
        {
            if(i%x==0) a[++cnt] = i;
            if(i*i!=y&&y/i%x==0) a[++cnt] = y/i;
        }
    }
    sort(a+1, a+1+cnt);
    for(int i=cnt; i>0; --i)
    {
        dp[i] = qmod(y/a[i]-1);
        for(int j=i+1; j<=cnt; ++j)
            if(a[j]%a[i]==0)
                dp[i] = ((dp[i]-dp[j])%mod+mod)%mod;
    }
    printf("%lld\n",dp[1]);
    return 0;
}

 

您可能感兴趣的与本文相关的镜像

AutoGPT

AutoGPT

AI应用

AutoGPT于2023年3月30日由游戏公司Significant Gravitas Ltd.的创始人Toran Bruce Richards发布,AutoGPT是一个AI agent(智能体),也是开源的应用程序,结合了GPT-4和GPT-3.5技术,给定自然语言的目标,它将尝试通过将其分解成子任务,并在自动循环中使用互联网和其他工具来实现这一目标

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值