CodeForces - 630K Indivisibility (容斥原理)

本文介绍了一种计算从1到n中不可被2至10整除的数的数量的方法,通过对2至10进行化简并使用容斥原理求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CodeForces - 630K
Time Limit: 500MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

 Status

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

Source

//题意:
给你一个数n,问从1---n中有几个数不能整除2,3,4,5,6,7,8,9,10。
//思路:
首先可以将2---10化简为2,3,5,7.因为4,6,8,9,10,可以被2,3,通过相乘得出。所以化简后也就是让求1---n之间有几个数不能整除2,3,5,7。
然后转化,因为不能整除2,3,5,7的数比较难表示出来,比如(11,121,,,13,169),虽然11,13等一些素数是不符合题意的,但他们的平方,立方.....虽然不是素数,但他们也是不复合题意的,所以这些比较难表示,但我们可以先把能被2,3,5,7整除的数的个数sum找出来,再用n-sum,即为所求。
求sum的值用容斥原理就可以求出来了。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define INF 0x3f3f3f3f
#define ll long long
#define N 10010
#define M 1000000007
using namespace std;
int a[]={2,3,5,7};
int b[]={6,10,14,15,21,35};
int c[]={30,42,70,105};
int d[]={210};
int main()
{
	ll n;
	int i;
	while(scanf("%lld",&n)!=EOF)
	{
		ll sum=0;
		for(i=0;i<4;i++)
			sum+=n/a[i];
		for(i=0;i<6;i++)
			sum-=n/b[i];
		for(i=0;i<4;i++)
			sum+=n/c[i];
		sum-=n/210;
		printf("%lld\n",n-sum);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值