Introduction to Inclusion and Exclusion

THE FIRST BLOOD

G - numbers containing only factors 2 3 5

  51Node - 1010 

Only 2 3 5 is included in the factor of K. The first 10 numbers that satisfy the condition are: 2, 3, 4, 5, 6, 8, 9, 10, 12, 15.
All such K form a sequence S, now given a number n, find the smallest number in S >= the given number.
For example: n = 13, the smallest number >= 13 in S is 15, so output 15.
Input line 1: a number T, representing the number of numbers used as input tests later. (1 <= T <= 10000) 
Line 2 - T + 1: 1 number per line N(1 <= N <= 10^18) Output a total of T lines, 1 number per line, output >= n The smallest number contains only factors 2 3 5. Sample Input
5
1
8
13
35
77
Sample Output
2
8
15
36
80
        AC: code
#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;
			}
		}
	}
}
intmain()
{
	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

Base Time Limit: 1 second Space Limit: 131072 KB Score: 5Difficulty  : Level 1 Algorithm Question
 collect
 focus on
Given a number N, find out how many numbers from 1 to N are not multiples of 2 3 5 7. For example N = 10, only 1 is not a multiple of 2 3 5 7.
Input
Enter 1 number N (1 <= N <= 10^18).
Output
Output how many numbers are not multiples of 2 3 5 7.
Input example
10
Output example
1

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

intmain()
{
	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;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324676969&siteId=291194637