【问题描述】

给出m个数a[1],a[2],…,a[m]

求1~n中有多少数不是a[1],a[2],…,a[m]的倍数。

【输入】

输入文件名为count.in。

第一行,包含两个整数:n,m

第二行,包含m个数,表示a[1],a[2],…,a[m]

【输出】

输出文件名为count.out。

输出一行,包含1个整数,表示答案

【输入输出样例】

count.in

count.out

10 2

2 3

3

【数据说明】

对于60%的数据,1<=n<=106

对于另外20%的数据,m=2

对于100%的数据,1<=n<=109,0<=m<=20,1<=a[i]<=109

分析:一道比较简单的容斥原理的题.只需要dfs求出1个数的倍数有多少个,2个数的倍数有多少个......dfs暴力枚举就可以了.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll; ll n, m, a[], ans; ll gcd(ll a, ll b)
{
if (!b)
return a;
return gcd(b, a % b);
} void dfs(int dep, int cnt, ll lcm)
{
if (dep == m + )
{
if (cnt & )
ans += (n / lcm);
else
if (cnt)
ans -= (n / lcm);
return;
}
dfs(dep + , cnt, lcm);
ll temp = lcm / gcd(lcm, a[dep]) * a[dep];
dfs(dep + , cnt + , temp);
} int main()
{
scanf("%lld%lld", &n, &m);
for (int i = ; i <= m; i++)
scanf("%lld", &a[i]);
dfs(, , );
printf("%d\n", n - ans); return ;
}

最新文章

  1. 转:详解Eclipse断点
  2. 【Alpha阶段】第⑨次Scrum例会
  3. 寻找最合适的view
  4. Spring学习总结四——SpringIOC容器四
  5. smarty框架块函数
  6. Codeforces Round #324 (Div. 2) E. Anton and Ira 贪心
  7. 读书笔记-----Java并发编程实战(一)线程安全性
  8. 如何制作css3的3d动画——以骰子旋转为例,详解css3动画属性
  9. HBase协处理器
  10. C# 如何查看源程序的IL代码
  11. Transport (VMDB) error -44: Message
  12. 实例讲解webpack的基本使用第二篇
  13. 我和Session的不解之“缘”(故事型技术长文)
  14. 微信小程序-输入框输入文字后,将光标移到文字中间,接着输入文字后光标又自动跳到最后
  15. Dapper/SqlMapper映射对应问题
  16. js 执行上下文理解
  17. 查看Nginx、PHP、Apache和MySQL的编译参数
  18. hdu5439 二分
  19. 解决chrome运行报错unknown error: cannot get automation extension
  20. Linux 常用环境搭建

热门文章

  1. MSP430 WDT
  2. Visual&#160;Studio&#160;Code配置GitHub(Win7环境)
  3. SpringBoot集成Mybatis配置动态数据源
  4. ACM_统计字符串
  5. cocos2d-x 调用第三方so文件
  6. 7 C#变量-把你想要的东西存在C#程序里边
  7. java DDD 基于maven开发的探讨
  8. tp5.0分页样式调控
  9. Android popwindow 消失监听
  10. AIDL跨进程通信报Intent must be explicit