您的位置:首页 > 其它

CodeForces 630K-Indivisibility【容斥定理】

2016-03-10 20:26 330 查看
K. Indivisibility

time limit per test
0.5 seconds

memory limit per test
64 megabytes

input
standard input

output
standard output

IT City company developing computer games decided to upgrade its way to reward its employees. Now it looks the following way. After a new game release users start buying it actively, and the company tracks the number of sales with precision to each transaction.
Every time when the next number of sales is not divisible by any number from 2 to 10 every
developer of this game gets a small bonus.

A game designer Petya knows that the company is just about to release a new game that was partly developed by him. On the basis of his experience he predicts that n people
will buy the game during the first month. Now Petya wants to determine how many times he will get the bonus. Help him to know it.

Input

The only line of the input contains one integer n (1 ≤ n ≤ 1018)
— the prediction on the number of people who will buy the game.

Output

Output one integer showing how many numbers from 1 to n are
not divisible by any number from 2 to 10.

Examples

input
12


output
2

解题思路:
找出1-n中不能被2到10中任意一个数整除的数的个数,因为4 6 8 10都是2的倍数如果不能被2整除则一定不能被这四个数整除同理3和9,则可以推出只要不能被2
3 5 7中任一个数整除即可,这时我们发现答案就是1到n之间素数的个数,如果直接遍历求素数的话一定超时,我们可以用总数减去合数的个数,所有的合数分别为2 3 5 7 的倍数,但是我们又发现这些倍数中有重复例如6既是2的倍数又是3的倍数,直接减的话肯定减多,这时就用上了容斥原理用2 3 5 7 倍数的和减去两两之间重复的,再加上任意三个的交集(因为减去两两交集时多减了三个之间重复的)再减去四个相交的
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define LL __int64
using namespace std;
int main()
{
LL n;
while(scanf("%I64d",&n)!=EOF)//2 3 5 7
{
LL wc;
wc=n-n/2-n/3-n/5-n/7+n/6+n/10+n/14+n/15+n/21+n/35-n/30-n/70-n/42-n/105+n/210;
printf("%I64d\n",wc);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: