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.
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 one integer showing how many numbers from 1 to n are not divisible by any number from 2 to 10.
12
2
题意:求1到 n之间有多少数不能被从2到10之间的任意一个整除!
和630 j 相反,肯定不能直接暴力!
想了好久,最后搜的题解,才知道容斥原理!
对于容斥原理:这和高中学的求相对元素不重复:下面看个例子:
(1)电视台向100人调查前一天收看电视的情况,有62人看过2频道,34人看过8频道,其中11人两个频道都看过。两个频道都没看过的有多少人?100-(62+34-11)=15!(2)|A∪B∪C| = |A|+|B|+|C| - |A∩B| - |B∩C| - |C∩A| + |A∩B∩C|![]()
能被4,6,8整除的都能被2整除,能被6,9整除的都能被3整除,能被10整除的都能被5整除,所以只要统计不能被2,3,5,7整除的数字即为答案! 我们可以用容斥求n-(可以除以任意其中一个或多个的书屋的个数);
AC code
#include<stdio.h> int main() { __int64 n; while(scanf("%I64d",&n)!=EOF) { __int64 m=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/105+n/70-n/210; m=n-m; printf("%I64d\n",m); } return 0; }