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;
    }
  • 相关阅读:
    av 1. Computer Abstractions and Technolog
    计算机组成与设计硬件/软件接口 (MIPS版)
    7. 我们的十个数字
    6.电报机与继电器qk
    六. Vue CLI详解
    五. Webpack详解
    四. 前端模块化
    一. Vue简介
    三. Vue组件化
    7. Git原理
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5462545.html
Copyright © 2011-2022 走看看