题目链接

\(Description\)

给定\(x,y\),求有多少个数列满足\(gcd(a_i)=x且\sum a_i=y\)。答案对\(10^9+7\)取模。

\(1≤x,y≤10^9\)


\(Solution\)

\(y\)如果不是\(x\)的倍数,答案为\(0\)

然后呢

令\(y/=x\),问题就变成了求有多少个数列满足\(gcd(a_i)=1且\sum ai=y'\)

如果没有\(gcd\)为\(1\)的限制?

隔板法可得\(ans=\sum_{i=0}^{y-1}C_{y-1}^i=2^{y-1}\)

令\(f(i)\)表示\(gcd(a_i)=1\)且和为\(i\)的方案数,\(g(i)\)表示和为\(i\)的方案数。

可得

\[g(i)=2^i-1,g(i)=\sum_{d|i}f(d)
\]

要求的是\(f(i)\),所以把\(f(i)\)的一项单独拿出来

\[f(i)=g(i)-\sum_{d|i,d\not = i}f(d)
\]

然后就可以从前往后递推了。

复杂度\(O(d(y/x)^2)\),其中\(d(x)\)为\(x\)的约数个数。

当然$$g(i)=\sum_{d|i}f(d)$$

就是一般的莫比乌斯反演的形式。

可以直接得出

\[f(i)=\sum_{d|i}\mu(d)g(\frac{i}{d})
\]

#include<complex>
#include<cstdio>
using namespace std;
const int mod=1e9+7;
const int N=1e5+7;
int x,y,tot;
int d[N];
int qread()
{
int x=0;
char ch=getchar();
while(ch<'0' || ch>'9')ch=getchar();
while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x;
}
int GetMu(int x)
{
if(x==1)return 1;
int t=0,sqr=sqrt(x);
for(int i=2;i<=sqr;i++)
if(x%i==0)
{
t++;x/=i;
if(x%i==0)return 0;
}
if(x>1)t++;
return t&1?-1:1;
}
int Fpow(long long b,int p)
{
long long res=1;
for(;p;p>>=1,b=b*b%mod)
if(p&1)res=res*b%mod;
return res;
}
int main()
{
scanf("%d%d",&x,&y);
if(y%x){printf("0\n");return 0;}
y/=x;
for(int i=1;i*i<=y;i++)
if(y%i==0)
{
d[++tot]=i;
if(i*i!=y)d[++tot]=y/i;
}
long long ans=0;
for(int i=1;i<=tot;i++)
ans+=GetMu(y/d[i])*Fpow(2,d[i]-1);
printf("%d\n",(ans%mod+mod)%mod);
return 0;
}

CF 900D Unusual Sequences的更多相关文章

  1. Codeforces 900D Unusual Sequences 容斥原理

    题目链接:900D  Unusual Sequences 题意: 给出两个数N,M.让你求数列(和为M,gcd为N)的个数. 题解: 首先,比较容易发现的是M%N如果不为零,那么一定不能构成这样的序列 ...

  2. CodeForces 900D Unusual Sequences

    题目链接: https://codeforces.com/contest/900/problem/D 题意 假设有distinct 正整数序列{a1,a2,,,an},满足gcd(a1, a2, .. ...

  3. Codeforces 900D Unusual Sequences:记忆化搜索

    题目链接:http://codeforces.com/problemset/problem/900/D 题意: 给定x,y,问你有多少个数列a满足gcd(a[i]) = x 且 ∑(a[i]) = y ...

  4. 【CF900D】Unusual Sequences 容斥(莫比乌斯反演)

    [CF900D]Unusual Sequences 题意:定义正整数序列$a_1,a_2...a_n$是合法的,当且仅当$gcd(a_1,a_2...a_n)=x$且$a_1+a_2+...+a_n= ...

  5. CodeForces - 900D: Unusual Sequences (容斥&莫比乌斯&组合数学)

    Count the number of distinct sequences a1, a2, ..., an (1 ≤ ai) consisting of positive integers such ...

  6. CF 405C Unusual Product(想法题)

    题目链接: 传送门 Domino Effect time limit per test:1 second     memory limit per test:256 megabytes Descrip ...

  7. CF 990C. Bracket Sequences Concatenation Problem【栈/括号匹配】

    [链接]:CF [题意]: 给出n个字符串,保证只包含'('和')',求从中取2个字符串链接后形成正确的括号序列的方案数(每个串都可以重复使用)(像'()()'和'(())'这样的都是合法的,像')( ...

  8. CF 256D. Good Sequences(DP)

    题目链接 主要是标记前面素数的最大的DP值,要认真一些.没想到居然写了一个很难发现的错误. #include <cstdio> #include <cstring> #incl ...

  9. cf B. Making Sequences is Fun

    http://codeforces.com/contest/373/problem/B 用二分枚举长度就可以. #include <cstdio> #include <cstring ...

随机推荐

  1. .NET / C# EF中的基础操作(CRUD)

    查 public List<users> Querys() { datatestEntities db = new datatestEntities(); var a = db.users ...

  2. 前端1-----HTML了解,内联标签(图片,超链接锚点,超链接邮箱)

    前端1-----HTML了解,内联标签(图片,超链接锚点,超链接邮箱) 一丶自定制B/S # -*-coding:utf-8-*- # Author:Ds import socket IP_PORT= ...

  3. Matlab访问者模式

    访问者(Visitor)模式的定义:将作用于某种数据结构中的各元素的操作分离出来封装成独立的类,使其在不改变数据结构的前提下可以添加作用于这些元素的新的操作,为数据结构中的每个元素提供多种访问方式.它 ...

  4. 为什么MES系统要定制化?看这三家汽车供应商的苦恼

    很多企业对于为什么要对MES系统进行选择和定制化很不理解,今天,小编通过一个故事给大家进行阐述—— 故事背景: 汽车电子行业的三家企业A,B,C. A是整车厂一级供应商,主要产品为汽车电子配电盒. B ...

  5. kvm虚拟化之virt-install

    1. 常用参数 -n --name= 客户端虚拟机名称 -r --ram= 客户端虚拟机分配的内存 -u --uuid= 客户端UUID 默认不写时,系统会自动生成 --vcpus= 客户端的vcpu ...

  6. SAP CDS redirect view支持写操作吗,一个实验来验证

    According to this wiki, write back on CDS view is not supported: And also it is defined in ABAP help ...

  7. 【转载】自定义View,有这一篇就够了

    为了扫除学习中的忙点,尽可能多的覆盖Android知识的边边角角,决定对自定义View做一个稍微全面一点的使用方法总结,在内容上面并没有什么独特的地方,其他大神们博客上面基本上都有讲这方面的内容,如果 ...

  8. 我的windows开发环境设定与日常使用指南

    目录 开发相关的软件包安装.设定 Visual Studio 默认设定 鼠标右键添加"在此处打开cmd"选项 git gvim notepad++ VSCode-Insider C ...

  9. Odoo中的记录集

    转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826218.html 一:record set 1:获取记录集 1)在@api.multi修饰器修饰的函数 ...

  10. css overflow失效的原因

    声明 转载自https://my.oschina.net/xuqianwen/blog/540587 项目中常常有同学遇到这样的问题,现象是给元素设置了overflow:hidden,但超出容器的部分 ...