codeforces 630KIndivisibility(容斥原理)
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中任意一个数整除的数的个数
题解:因为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 倍数的和减去两两之间重复的,再加上任意三个的交集(因为减去两两交集时多减了三个之间重复的)再减去四个相交的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #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\n" ,m); } return 0; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步