cf 959D Mahmoud and Ehab and another array construction task

4 篇文章 0 订阅
2 篇文章 0 订阅

一 原题

D. Mahmoud and Ehab and another array construction task
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Mahmoud has an array a consisting of n integers. He asked Ehab to find another array b of the same lengthsuch that:

  • b is lexicographically greater than or equal to a.
  • bi ≥ 2.
  • b is pairwise coprime: for every 1 ≤ i < j ≤ nbi and bj are coprime, i. e. GCD(bi, bj) = 1, where GCD(w, z) is the greatest common divisor of w and z.

Ehab wants to choose a special array so he wants the lexicographically minimal array between all the variants. Can you find it?

An array x is lexicographically greater than an array y if there exists an index i such than xi > yi and xj = yj for all 1 ≤ j < i. An array x is equal to an array y if xi = yi for all 1 ≤ i ≤ n.

Input

The first line contains an integer n (1 ≤ n ≤ 105), the number of elements in a and b.

The second line contains n integers a1a2...an (2 ≤ ai ≤ 105), the elements of a.

Output

Output n space-separated integers, the i-th of them representing bi.

Examples
input
Copy
5
2 3 5 4 13
output
Copy
2 3 5 7 11 
input
Copy
3
10 3 7
output
Copy
10 3 7 
Note

Note that in the second sample, the array is already pairwise coprime so we printed it.


二 分析

给定一个长度为n的数组a(n<=10^5,ai<=10^5)要求你给出一个新的等长数组b,b的字典序要不小于a,且b中的元素两两互素,输出所有可行的b中字典序最小的一个。

要输出的b字典序最小,我们从前往后贪心地去填数字。为了满足两两互素的条件,维护一个素数表v[maxp],如果素数p已经是先前某个b中元素的素因子,v[p]设置为true。注意,bi的上界题目中并没有规定,但前10^5个素数组成的数组一定是一个合法解,根据素数约占前n个数的1/ln(n),我在实现时把bi的上界定位了1e7.

假设我们已经搞定了b中前k-1个数,如果b的前k-1个数和a都相同,那么bk就要大于等于ak,我们从ak开始枚举可行解,每次验证的代价是一次质因数分解;如果b的前k-1个数中有大于a的,那么bk设置为当前为使用过的最小素数就行了。


三 代码

#include <cstdio>

const int maxn = 1e7 + 5;

int n, a[maxn], d[maxn];
bool v[maxn];

void prepare() {
	for (int i = 2; i < maxn; i++) {
		if (d[i] != 0) continue;
		for (int j = i; j < maxn; j += i) d[j] = i;
	}
}

int main() {
	prepare();
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
		scanf("%d", &a[i]);
	int idx = 0, x;
	while (idx < n) {
		bool ok = true;
		x = a[idx];
		while (x != 1) {
			if (v[d[x]]) { ok = false; break; }
			x /= d[x];
		}
		if (ok) {
			x = a[idx];
			while (x != 1) {
				v[d[x]] = true;
				x /= d[x];
			}
			idx++;
			continue;
		}
		while (!ok) {
			ok = true;
			x = ++a[idx];
			while (x != 1) {
				if (v[d[x]]) { ok = false; break; }
				x /= d[x];
			}
		}
		x = a[idx];
		while (x != 1) {
			v[d[x]] = true;
			x /= d[x];
		}
		idx++;
		break;
	}
	
	int p = 2;
	while (idx < n) {
		for (;; p++) {
			if (d[p] != p || v[p]) continue;
			a[idx++] = p;
			p++; break;
		}
	}
	
	for (int i = 0; i < n; i++) printf("%d ", a[i]);

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值