CodeForces 66D - Petya and His Friends(构造)
2016-12-05 16:03
387 查看
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代码:
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; }
相关文章推荐
- codeforces 66D D. Petya and His Friends( 数论+构造)
- CodeForces 66 D.Petya and His Friends(构造+数论+高精度)
- codeforces 66 D. Petya and His Friends(数论)
- Codeforces 66 D Petya and His Friends(找规律)
- Codeforces Beta Round #61 (Div. 2) D. Petya and His Friends 想法
- codeforces 515B B. Drazil and His Happy Friends(模拟)
- [数论] Codeforces 819D R #421 D.Mister B and Astronomers & 516E R #292 E. Drazil and His Happy Friends
- 【codeforces 515B】Drazil and His Happy Friends
- CF 61 div2 D. Petya and His Friends
- 【22.48%】【codeforces 689D】Friends and Subsequences
- Codeforces 112A-Petya and Strings(实现)
- 2015山东省第六届ACM-BIGZHUGOD and His Friends II(赛瓦定理+二分法求三次方程)
- 字符串k在第li到第ri个字符串中一共出现了几次 后缀数组+线段树 Codeforces Div. 1E. Mike and Friends
- Codeforces 793b B. Igor and his way to work 觉得大神写的3维bfs太复杂,突然发现这题是连连看算法。
- CodeForces 673D - Bear and Two Paths(构造)
- codeforces 746 D. Green and Black Tea (构造)
- CodeForces 459C-G - Pashmak and Buses-构造-K进制
- Codeforces 112A-Petya and Strings(实现)
- Codeforces 916 C. Jamie and Interesting Graph (构造)
- codeforces-544B-Sea and Islands【构造】