CodeForces 66D - Petya and His Friends(构造)

来源:互联网 发布:2017党章党规网络答题 编辑:程序博客网 时间:2024/05/21 10:44

D. Petya and His Friends
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Little Petya has a birthday soon. Due this wonderful event, Petya’s friends decided to give him sweets. The total number of Petya’s friends equals to n.

Let us remind you the definition of the greatest common divisor: GCD(a1, …, ak) = d, where d represents such a maximal positive number that each ai (1 ≤ i ≤ k) is evenly divisible by d. At that, we assume that all ai’s are greater than zero.

Knowing that Petya is keen on programming, his friends has agreed beforehand that the 1-st friend gives a1 sweets, the 2-nd one gives a2 sweets, …, the n-th one gives an sweets. At the same time, for any i and j (1 ≤ i, j ≤ n) they want the GCD(ai, aj) not to be equal to 1. However, they also want the following condition to be satisfied: GCD(a1, a2, …, an) = 1. One more: all the ai should be distinct.

Help the friends to choose the suitable numbers a1, …, an.

Input
The first line contains an integer n (2 ≤ n ≤ 50).

Output
If there is no answer, print “-1” without quotes. Otherwise print a set of n distinct positive numbers a1, a2, …, an. Each line must contain one number. Each number must consist of not more than 100 digits, and must not contain any leading zeros. If there are several solutions to that problem, print any of them.

Do not forget, please, that all of the following conditions must be true:

For every i and j (1 ≤ i, j ≤ n): GCD(ai, aj) ≠ 1
GCD(a1, a2, …, an) = 1
For every i and j (1 ≤ i, j ≤ n, i ≠ j): ai ≠ aj
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).

Examples
input
3
output
99
55
11115
input
4
output
385
360
792
8360

题意:
给出n个数,让你构造他们,使得两两之间的gcd不等于1,同时整体的gcd等于1,如果不能构造,输出-1.

解题思路:
如果是2就必然无法构造。
对于n大于2的情况,先让n个数都为1,然后除了第一个都乘上2,除了第二个都乘上3,再让第一个和第二个都乘上5,最后让n个数都乘上没有出现过的素数即可。

这样首先让除了第一个的所有数gcd都为2,再让除了2的所有数gcd都为3,再使得1和2非互素。最后处理,使得每个数都不相同即可。

AC代码:

#include<bits/stdc++.h>using namespace std;int a[1005];int main(){    int n;    scanf("%d",&n);    for(int i = 0;i < n;i++)    scanf("%d",&a[i]);    int res = 0;    for(int i = 0;i < n;i++)    {        int cnt = 1;        int l = i-1,r = i+1;        while(a[l] <= a[l+1]&&l >= 0) l--,cnt++;        while(a[r] <= a[r-1]&&r < n)  r++,cnt++;        res = max(res,cnt);    }    printf("%d\n",res);    return 0;}
0 0