NYOJ--993 How many integers can you find
来源:互联网 发布:js 中 json点不出来 编辑:程序博客网 时间:2024/04/30 22:17
How many integers can you find
时间限制:1000 ms | 内存限制:65535 KB
难度:1
- 描述
- 给你三个数,n,m1,m2,找出所有小于n的能被m1或m2整除的数的个数。
- 输入
- 输入包含多组测试数据,每组数据占一行。
0<n<2^31,0<m1,m2<=10。 - 输出
- 每组数据输出占一行。
- 样例输入
12 2 3
- 样例输出
7
- 来源
- 爱生活
#include <iostream>using std::endl;using std::cout;using std::cin;//欧几里得求最大公约数int gcd(int a, int b){ if (a % b == 0) { return b; } else { return gcd(b, a % b); }}int main(){ int n,m1, m2; while (cin >> n >> m1 >> m2) { if (m1 != m2) cout << (n-1)/m1+(n-1)/m2-(n-1)/((m1*m2)/gcd(m1,m2))<< endl; else cout << (n-1)/m1 << endl; } return 0;}
暴力循环的话就一定会超时,
/* 能被m1整除的个数 + 能被m2整除的个数 - 能同时被m1和m2整除的个数 */
s = n / m1 + n / m2 - n / lcm(m1, m2);
if (n % m1 == 0) s--;
if (n % m2 == 0) s--;
if (n % lcm(m1, m2) == 0) s++;
0 0
- NYOJ--How many integers can you find
- NYOJ-How many integers can you find
- nyoj-993-How many integers can you find
- NYOJ 993 How many integers can you find
- NYOJ--993 How many integers can you find
- nyoj 993 How many integers can you find
- NYOJ 993 How many integers can you find (数学)
- nyoj 993 How many integers can you find <水>
- 993 How many integers can you find
- NYOJ 题目993 How many integers can you find(容斥原理,数学)
- hdu1796 How many integers can you find
- NYOJ993 How many integers can you find
- HDU1796-How many integers can you find
- How many integers can you find
- hdu1796 How many integers can you find
- NYOJ993--How many integers can you find
- How many integers can you find
- How many integers can you find
- Fedora 20下,JAVA JDK的安装
- this 关键字
- i++和++i以及*p++和*++p
- Redis数据库的安装配置方法详解
- debugging python with IDLE
- NYOJ--993 How many integers can you find
- Js获取短信验证码前段效果
- Linux hostname命令修改主机名称
- VxWorks6.6 pcPentium BSP 使用说明(三):设备驱动
- VxWorks6.6 pcPentium BSP 使用说明(二):创建启动盘
- VxWorks6.6 pcPentium BSP 使用说明(一):基本概念
- Eeid—ligerUI学习(1)[首页布局]
- STL中heap算法(堆算法)
- cubieboard2 编译安装opencv