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

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

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

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 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.

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值