XVII Open Cup named after E.V. Pankratiev. Eastern Grand Prix.
Problem F. Buddy Numbers
Input le: standard input
Output le: standard output
Time limit: 1 second
Memory limit: 256 mebibytes
Polycarp says that two positive integers are buddies, if one is divisible by another. For example, 2 and 4
are buddies, and 10 and 3 are not. Note that 1 is buddy with every positive integer.
Given integer n nd out if it is possible to arrange all consecutive integers from 1 to n in a row such that
any pair of neighbor numbers are buddies. If it is possible, print any of those arrangements.
Input
The only line of the input contains one integer n (1 n 1000).
Output
If it is impossible to arrange numbers from 1 to n in such a way, printany of them.
Examples
standard input standard output
2 2 1
3 3 1 2
Source
XVII Open Cup named after E.V. Pankratiev. Eastern Grand Prix.
My Solution
题意:给出一个n,问能不能把1~n这n个数排出一个序列,使得任意相邻的两个数,一个数是另一个数的因数。
贪心、数论、构造
1 1
2 2 1
3 3 1 2
4 3 1 2 4
然后5的时候,也就是当1~n里素数的个数大于2的时候就不好办了,
此外这里n == 6的时候还是可以构造出来了, 3 6 2 4 1 5.
所以对于n == 1、2、3、4、6的时候是有答案的,直接输出即可,其它的时候都构造不出来,为-1。
#include <bits/stdc++.h>
using namespace std;
#define LL long long
LL n;
LL a[100][100];
int main () {
int n;
cin>>n;
if(n==1)printf("1");
else
if(n==2)printf("1 2");
else
if(n==3)printf("3 1 2");
else
if(n==4)printf("3 1 2 4");
else
if(n==6)printf("5 1 3 6 2 4");
else printf("-1");
}
Thank you!
------from ProLights