Indivisibility(最大公约数+排列组合)

来源:互联网 发布:linux命令手册下载chm 编辑:程序博客网 时间:2024/05/16 18:20

Indivisibility(最大公约数+最小公倍数+排列组合)

Crawling in process...Crawling failedTime Limit:500MS    Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
SubmitStatus

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

Input
12
Output
2
题意:在,1---n(1<=n<=10^18),之间的所有数中,不能被2-----10中的任何一个数整除,求这样的数有多少。

思路:不能被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;}


0 0
原创粉丝点击