【51Nod】1284 - 2 3 5 7的倍数(容斥原理 & 二进制优化)

来源:互联网 发布:金蝶软件客服中心 编辑:程序博客网 时间:2024/06/05 17:06

题目链接:点击打开题目

1284 2 3 5 7的倍数
基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题
 收藏
 关注
给出一个数N,求1至N中,有多少个数不是2 3 5 7的倍数。 例如N = 10,只有1不是2 3 5 7的倍数。
Input
输入1个数N(1 <= N <= 10^18)。
Output
输出不是2 3 5 7的倍数的数共有多少。
Input示例
10
Output示例
1




用容斥原理最方便,要不估计要TLE的吧。


代码如下:

#include <cstdio>#include <stack>#include <queue>#include <cmath>#include <vector>#include <cstring>#include <algorithm>using namespace std;#define CLR(a,b) memset(a,b,sizeof(a))#define INF 0x3f3f3f3f#define LL long longint main(){LL n;int num[4] = {2,3,5,7};scanf ("%lld",&n);LL ans = n;int i = 1;while (i < (1 << 4)){int mul = 1;int ant = 0;int t = i;for (int j = 0 ; j < 4 ; j++){if ((1 << j) & i){ant++;mul *= num[j];}}if (ant & 1)//奇数ans -= n / mul;elseans += n / mul;i++;}printf ("%lld\n",ans);return 0;}


0 0
原创粉丝点击