Codeforces Round #696 (Div. 2) B. Different Divisors(素数筛)

题目连接:https://codeforc.es/contest/1474/problem/B

Positive integer x is called divisor of positive integer y, if y is divisible by x without remainder. For example, 1 is a divisor of 7 and 3 is not divisor of 8.

We gave you an integer d and asked you to find the smallest positive integer a, such that

a has at least 4 divisors;
difference between any two divisors of a is at least d.
Input
The first line contains a single integer t (1≤t≤3000) — the number of test cases.

The first line of each test case contains a single integer d (1≤d≤10000).

Output
For each test case print one integer a — the answer for this test case.

Example

input

2
1
2

output

6
15

分析

至少四个因子,很明显,只需要找到比 1 + d 大的最小素数,然后再找到这个素数的下一个素数(两者相距要大于 d),两者的乘积就是答案。
可以用二分来找。

代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

#define maxn 1000007
ll t,d;
ll prime[maxn];
ll visit[maxn];

void Prime(){
    for (ll i = 2;i <= maxn; i++) {
        if (!visit[i]) {
            prime[++prime[0]] = i;
        }
        for (ll j = 1; j <= prime[0] && i*prime[j] <= maxn; j++) {
            visit[i*prime[j]] = 1;
            if (i % prime[j] == 0) {
                break;
            }
        }
    }
}

int main()
{
	Prime();
	//cout<<prime[78499]<<endl;
	scanf("%lld",&t);
	while(t--)
	{
		scanf("%lld",&d);
		ll tmp = 1 + d;
		int pos = lower_bound(prime + 1, prime + 1  + prime[0], tmp) - prime;
		ll tmp1 = prime[pos];
		pos = upper_bound(prime + 1, prime + 1  + prime[0], tmp1) - prime;
		while(prime[pos] - tmp1 < d) pos++;
		printf("%lld\n",prime[pos] * tmp1);
	}
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值