codeforces 630KIndivisibility(容斥原理)

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<string>
#include<math.h>
#include<algorithm>
#define LL long long
#define PI atan(1.0)*4
#define DD double
#define MAX 100100
#define mod 100
#define dian 1.000000011
#define INF 0x3f3f3f
using namespace std;
int main()
{
	LL n,m;
	while(scanf("%lld",&n)!=EOF)
	{
		
		m=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;
		m=n-m;
		printf("%lld
",m);
	}
	return 0;
} 

  

原文地址:https://www.cnblogs.com/tonghao/p/5241427.html