容斥入门

THE FIRST BLOOD

G - 只包含因子2 3 5的数

  51Nod - 1010 

K的因子中只包含2 3 5。满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15。
所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数。
例如:n = 13,S中 >= 13的最小的数是15,所以输出15。
Input第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 10000) 
第2 - T + 1行:每行1个数N(1 <= N <= 10^18)Output共T行,每行1个数,输出>= n的最小的只包含因子2 3 5的数。Sample Input
5
1
8
13
35
77
Sample Output
2
8
15
36
80
        AC:代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const long long maxn = 1e18;
typedef long long ll;
ll a[1000000],sum = 0;
void init()
{
	for(ll i = 1;i<=maxn;i*=2){
		for(ll j = 1;i*j<=maxn;j*=3){
			for(ll k = 1;i*j*k<=maxn;k*=5){
				a[sum++] = i*j*k;
			}
		} 
	} 
}
int main() 
{
	init();
	//printf("ok\n");
	sort(a,a+sum);
	int t;
	cin>>t;
	while(t--)
	{
		ll n;
		scanf("%lld",&n);
		printf("%lld\n",*lower_bound(a+1,a+sum,n));
	}
	return 0;
}

NEXT BLOOD

基准时间限制:1 秒 空间限制:131072 KB 分值: 5  难度:1级算法题
 收藏
 关注
给出一个数N,求1至N中,有多少个数不是2 3 5 7的倍数。 例如N = 10,只有1不是2 3 5 7的倍数。
Input
输入1个数N(1 <= N <= 10^18)。
Output
输出不是2 3 5 7的倍数的数共有多少。
Input示例
10
Output示例
1

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

int main()
{
	long long n;
	while(~scanf("%lld",&n))
	{
		printf("%lld\n",n-n/2-n/3-n/5-n/7+n/6+n/10+n/14+n/15+n/21+n/35-n/70-n/30-n/42-n/105+n/210);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Acer12138/article/details/80049195