Indivisibility(最大公约数+排列组合)
来源:互联网 发布:linux命令手册下载chm 编辑:程序博客网 时间:2024/05/16 18:20
Description
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.
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 ton are not divisible by any number from 2 to 10.
Sample Input
12
2
思路:不能被2-----10中的任何一个数整除,即:不能被2、3、5、7中的任何一个数整除。
设这样的数有ans个;
(1)
t1=n/2 (n个数中,共有n/2个数可以被2整除)
t2=n/3
t3=n/5
t4=n/7 (n个数中,共有n/2个数可以被2整除)
ans=ans-t1-t2-t3-t4;
(2)
如:数字6(2、3的公倍数被减了两次)
m1=n/(2*3) (m1为2、3的公倍数)
m2=n/(2*5)
m3=n/(2*7)
m4=n/(3*5)
m5=n/(3*7)
m6=n/(5*7) (m6为5、7的公倍数)
ans=ans+m1+m2+m3+m4+m5+m6
(3)如:数字30,分别是2、3、5的倍数被减了3次,又是2、3的公倍数,是2、5的公倍数是3、5的公倍数,又加了3次。所以需要再减一次。
h1=n/(2*3*5)
h2=n/(2*3*7)
h3=n/(2*5*7)
h4=n/(3*5*7)
ans=ans-h1-h2-h3-h4
(4)
如:数字210,分别是2、3、5、7的倍数被减了4次,又是2、3的公倍数,是2、5的公倍数是2、7的公倍数,又是3、5的公倍数,是3、7的公倍数是5、7的公倍数,被加了
6次,又是又是2、3、5的公倍数,是2、5、7的公倍数是3、5、7的公倍数,是2、3、7的公倍数,又减了4次,总计被减了2次,因此需要再加一次。
ans=ans+n/(2*3*5*7)
My solution:
/*2016.3.11*/
#include<stdio.h>long long t[5]={0,2,3,5,7},m[9],m2[6],sum;int main(){int i,j,k,h;long long n,ans;while(scanf("%lld",&n)==1){h=j=k=sum=1;ans=n; for(i=1;i<=4;i++) { ans=ans-(n/t[i]); sum*=t[i];//计算两个数的公倍数 } for(i=1;i<=4;i++) { for(j=i+1;j<=4;j++) m[k++]=t[i]*t[j];//计算两个数的公倍数 } for(i=1;i<=4;i++) for(j=i+1;j<=4;j++) for(k=j+1;k<=4;k++) m2[h++]=t[i]*t[j]*t[k];//计算三个数的公倍数 for(i=1;i<=6;i++) ans+=(n/m[i]);//加上两个数的公倍数 for(i=1;i<=4;i++) ans-=(n/m2[i]);//减去3个数的公倍数 ans+=n/sum;//加上4个数的公倍数 printf("%lld\n",ans);}return 0;}
- Indivisibility(最大公约数+排列组合)
- CF630K:Indivisibility(容斥)
- CF630K:Indivisibility(容斥)
- CodeForces 630 K. Indivisibility(容斥)
- Codeforces--630K--Indivisibility(容斥)
- codeforces 630K Indivisibility (容斥原理)
- CodeForces - 630K Indivisibility (容斥原理)
- 打靶 (排列组合)
- 排列组合(二)
- hdu 1261(排列组合)
- 排列组合问题(1)
- hdu 5366(排列组合)
- Benches(排列组合)
- bzoj3505(排列组合)
- bzoj1600(排列组合)
- Number Sequence (排列组合)
- 开会(排列组合)
- 取三角形(排列组合 )
- 倒排索引原理
- ListView的优化问题以及策略
- dashboard中资源释放
- 华为基础训练---字符串分割
- Android环境搭建
- Indivisibility(最大公约数+排列组合)
- macchanger克隆MAC地址
- Linux之:最常用的20条命令
- CodeForces-630 B. Moore's Law【数学】
- Linux 字符设备驱动开发基础(三)—— read()、write() 相关函数解析
- ubuntu 14.04 使用root 登录图形界面
- 程序员笔试面试基础知识资料整理
- PAT 1090
- 328. Odd Even Linked List