题目链接

\(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. Jenkins部署git+python项目实现持续集成

    目录 1. 创建 item 2. 配置 3. 构建 1. 创建 item 接下来填写创建任务的名字,并选择创建一个 Freestyle project ,点击确认. 2. 配置 接下来进入到项目相关配 ...

  2. MethodInvoker委托,跨线程访问

    Invoke(new MethodInvoker(delegate { textBox1.Enabled = true; })); 上面是简单缩写,也可以写成 private void btnOK_C ...

  3. systemd - CentOS 7进程守护&监控

    需求: 运行环境为CentOS 7系统,我们开发了一个程序,需要在开机时启动它,当程序进程crash或者开机之后,守护进程立即拉起进程. 解决方案: 使用CentOS 7中的init进程systemd ...

  4. 解决WPF下popup不随着window一起移动的问题

    /// <summary> /// Popup帮助类,解决Popup设置StayOpen="True"时,移动窗体或者改变窗体大小时,Popup不随窗体移动的问题 // ...

  5. 【转】Unobtrusive Ajax的使用

    [转]Unobtrusive Ajax的使用 Ajax (Asynchronous JavaScript and XML 的缩写),如我们所见,这个概念的重点已经不再是XML部分,而是 Asynchr ...

  6. 英语gzibeads天珠gzibeads单词

    天珠英语是gZiBeads,藏语叫(si , 斯)汉语译为“斯”或“瑟”,又称“天降石”.在<藏汉大辞典>里天珠的解释为:“亚玛瑙,猫睛石,一种宝石,俗称九眼珠.入药能治脑溢血”.最早的天 ...

  7. 创建简易的SpringBoot项目

    创建简易的SpringBoot项目 这两天在学习springboot,菜鸟刚刚知道这个东西,看着springboot项目下那一大堆目录都不知道从何下手,还是静下心来从最简单的创建一个项目入手,这路和大 ...

  8. 关于logging模块

    from logging.handlers import TimedRotatingFileHandle #日志文件控制(日志删除时间设置) import logging logger=logging ...

  9. python的一些包安装

    Linux下pip 的安装方法: 使用get-pip.py安装 要安装pip,请安全下载get-pip.py.1: curl https://bootstrap.pypa.io/get-pip.py ...

  10. 如果centos7添加新网卡,系统不识别的解决办法

    #ifconfig 2.获取新增网卡的真实mac #ip addr 3.复制eth0到eth1并修改配置文件 #cd /etc/sysconfig/network-scripts #cp ifcfg- ...