B - Indivisibility
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 thatn 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.
InputThe 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 one integer showing how many numbers from 1 ton are not divisible by any number from2 to10.
12
2
这道题求1-n中有多少数不能被2-10整除,其实也就是求多少数不能被2、3、5、7整除,因为1 ≤ n ≤ 1018,所以如果遍历n去求解,一定会超时,打表的话数组也不能开那么大。此时就要用到容斥原理,我们可以找出1-n中被2、3、5、7整除的数,然后用n减去,由于减去2、3、5、7的倍数时,会重复减去一些数(如6、10、14),所以这时我们得再加上任意两数的乘积的倍数,再减去任意三数的乘积的倍数,最后加上四数的乘积的倍数,可手动模拟一下,会更加清晰。代码如下:
#include<stdio.h> int main(){ long long n, ans; while(scanf("%lld", &n) != EOF){ ans = n - (n/2 + n/3 + n/5 + n/7 - n/6 - n/10 - n/14 - n/15 - n/21 - n/35 + n/30 + n/42 + n/70 + n/105 - n/210); printf("%lld\n", ans); } return 0; }