• 最大公约数——414AMashmokh and Numbers


    C. Mashmokh and Numbers
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    It's holiday. Mashmokh and his boss, Bimokh, are playing a game invented by Mashmokh.

    In this game Mashmokh writes sequence of n distinct integers on the board. Then Bimokh makes several (possibly zero) moves. On the first move he removes the first and the second integer from from the board, on the second move he removes the first and the second integer of the remaining sequence from the board, and so on. Bimokh stops when the board contains less than two numbers. When Bimokh removes numbers x and y from the board, he gets gcd(x, y) points. At the beginning of the game Bimokh has zero points.

    Mashmokh wants to win in the game. For this reason he wants his boss to get exactly k points in total. But the guy doesn't know how choose the initial sequence in the right way.

    Please, help him. Find n distinct integers a1, a2, ..., an such that his boss will score exactly k points. Also Mashmokh can't memorize too huge numbers. Therefore each of these integers must be at most 109.

    Input

    The first line of input contains two space-separated integers n, k (1 ≤ n ≤ 105; 0 ≤ k ≤ 108).

    Output

    If such sequence doesn't exist output -1 otherwise output n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).

    Examples
    input
    5 2
    output
    1 2 3 4 5
    input
    5 3
    output
    2 4 3 7 1
    input
    7 2
    output
    -1
    Note

    gcd(x, y) is greatest common divisor of x and y.

    题意:输出一个长度为n序列,然后对这个序列进行操作,每次取前两个数,并从序列中去掉,求它们的最大公约数,当余下的不足两个时停止,将这些最大公约数累加起来,要等于k。如果这个序列不存在就输出-1。

    题解: 长度为n的序列,那么我们可以取n/2次,当K<n/2时肯定是输出-1 的,因为每对数公约数最下为1。当k==n/2时,只需要输出n个连续的数字就可以了,因为连续的两个数字的公约数为1,当k>n/2时,我们先输出最大公约数为k-n/2+的两个数,余下的同样为连续的数字。

     1 /*C*/
     2 #include<cstdio>
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     int n,k;
     8     while(scanf("%d%d",&n,&k)!=EOF)
     9     {
    10         int t=n/2;
    11         if(t>k||(k!=0&&t==0))
    12         {
    13             printf("-1
    ");
    14             continue;
    15         }
    16         else if(t==k)
    17         {
    18             for(int i=1;i<=n;i++)
    19                 printf("%d ",i);
    20             printf("
    ");
    21         }
    22         else
    23         {
    24             int temp=k-t+1;
    25             printf("%d %d ",temp,temp*2);
    26             int d=2;
    27             for(int i=temp*2+1;;i++)
    28             {
    29                 if(d==n)    break;
    30                 printf("%d ",i);
    31                 d++;
    32                 
    33             }
    34             printf("
    ");
    35         }
    36     }
    37     return 0;
    38  } 
  • 相关阅读:
    曾经写的一些文章,与技术无关,整理出来怀旧,:)
    在VS.NET2003中使用XHTML的插件HTML TIDY 及 MindManger
    把机器退出了域,造成无法启动 MSSQLSERVER ,晕
    hello php!
    今天又看了一下存储过程
    [转]PAGEII携手极速网爱情电影经典对白
    关键词:2005年,世乒赛.上海,乒乓的胜地
    一本SharePoint方面的书,Special Edition Using Microsoft® SharePoint Portal Server
    keo计划
    关于xp_cmdshell 。。注意安全!
  • 原文地址:https://www.cnblogs.com/yepiaoling/p/5316979.html
Copyright © 2020-2023  润新知