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
output
解题思路:
找出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 倍数的和减去两两之间重复的,再加上任意三个的交集(因为减去两两交集时多减了三个之间重复的)再减去四个相交的
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; }
相关文章推荐
- HDOJ 1104 Remainder(BFS + 数论)
- Android返回数据给上一个活动-startActivityForResult
- 蓝桥杯:回形取数
- 成为一名JAVA高级工程师你需要学什么【转】
- 精益创业之父Steve Blank: 怎样让企业内部创新获得50倍增速
- GDKOI 2016 Day2【T1】【染色大战】
- x264代码剖析(九):x264_encoder_encode()函数之x264_slice's'_write()函数
- x264代码剖析(九):x264_encoder_encode()函数之x264_slice's'_write()函数
- 第2周项目4 求平方和
- 【上传AppStore】iOS项目上传到AppStore步骤流程(第三章) - 基本信息总汇
- java提高篇-----详解java的四舍五入与保留位
- Pipe-python的stream
- 培训学习心得3.10
- Java集合框架(四)
- 欢迎页面的三个动画
- 欢迎页面的三个动画
- Unity5.0 摄像机 Clear Flags和Culling Mask属性用途详解
- 【转】unity基础问题汇总
- 判断一个数字是否为回文,不使用额外的存储空间
- CreateThread函数