XVII Open Cup named after E.V. Pankratiev. Eastern Grand Prix. Problem F. Buddy Numbers 贪心、数论、构造

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值