zoukankan      html  css  js  c++  java
  • codeforces 414A A. Mashmokh and Numbers(素数筛)

    题目链接:

    A. 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 10^9.

    Input
     

    The first line of input contains two space-separated integers n, k (1 ≤ n ≤ 10^5; 0 ≤ k ≤ 10^8).

    Output
     

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

    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.

    题意

    一个数列的数各不相同,每次取走前两个,得到gcd(x,y)的分数,问是否存在这样的长度为n,最后得分为k的数列;

    思路

    不存在的就不说了;

    存在的时候可以第一次把k-n/2+1的分数得到,剩下的全都是gcd(x,y)==1的情况,需要用素数筛先处理出素数;

    AC代码

    #include <bits/stdc++.h>
    using namespace std;
    #define Riep(n) for(int i=1;i<=n;i++)
    #define Riop(n) for(int i=0;i<n;i++)
    #define Rjep(n) for(int j=1;j<=n;j++)
    #define Rjop(n) for(int j=0;j<n;j++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    typedef long long LL;
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const int inf=0x3f3f3f3f;
    const int N=1e6+5e5;
    int a[N],flag[N];
     int cnt=0;
    int getprime()
    {
    
        mst(flag,0);
        for(int i=2;i<N;i++)
        {
            if(!flag[i])
            {
                a[cnt++]=i;
            for(int j=2;j*i<N;j++)
            {
                flag[i*j]=1;
            }
            }
        }
        return cnt;
    }
    
    int main()
    {
            int n,k;
            scanf("%d%d",&n,&k);
            getprime();
            if(n==1)
            {
                if(k==0)cout<<"1"<<endl;
                else cout<<"-1"<<endl;
            }
            else
            {
                if(n/2>k)cout<<"-1"<<endl;
                else
            {
                int m=k-n/2+1;
                    printf("%d %d ",m,2*m);
                    int num=0;
                    for(int i=1;i<cnt&&num<n-2;i++)
                    {
                        if(a[i]==m||a[i]==2*m)continue;
                        else
                        {
                            printf("%d ",a[i]);
                            num++;
                        }
                    }
            }
            }
    
    
        return 0;
    }
  • 相关阅读:
    Sql server 备份还原后出现“受限制用户”问题
    jQuery的deferred对象详解
    SQLServer 自增主键创建, 指定自增主键列值插入数据,插入主键
    无法添加来自此网站的应用 扩展程序
    C#中dynamic的正确用法
    C#编程总结(十四)dynamic
    基于params,ref,out的参数问题详解
    SQLServer 数据库查看死锁、堵塞的SQL语句
    如何对web.config进行加密和解密
    自定义HttpModule的一些经验--配置篇
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5462545.html
Copyright © 2011-2022 走看看