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