题目链接:点击打开题目
基准时间限制: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 long
int 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;
else
ans += n / mul;
i++;
}
printf ("%lld\n",ans);
return 0;
}