zoukankan      html  css  js  c++  java
  • CF div2 319 C

    C. Vasya and Petya's Game
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Vasya and Petya are playing a simple game. Vasya thought of number x between 1 and n, and Petya tries to guess the number.

    Petya can ask questions like: "Is the unknown number divisible by number y?".

    The game is played by the following rules: first Petya asks all the questions that interest him (also, he can ask no questions), and then Vasya responds to each question with a 'yes' or a 'no'. After receiving all the answers Petya should determine the number that Vasya thought of.

    Unfortunately, Petya is not familiar with the number theory. Help him find the minimum number of questions he should ask to make a guaranteed guess of Vasya's number, and the numbers yi, he should ask the questions about.

    Input

    A single line contains number n (1 ≤ n ≤ 103).

    Output

    Print the length of the sequence of questions k (0 ≤ k ≤ n), followed by k numbers — the questions yi (1 ≤ yi ≤ n).

    If there are several correct sequences of questions of the minimum length, you are allowed to print any of them.

    Sample test(s)
    Input
    4
    Output
    3
    2 4 3
    Input
    6
    Output
    4
    2 4 3 5
    Note

    The sequence from the answer to the first sample test is actually correct.

    If the unknown number is not divisible by one of the sequence numbers, it is equal to 1.

    If the unknown number is divisible by 4, it is 4.

    If the unknown number is divisible by 3, then the unknown number is 3.

    Otherwise, it is equal to 2. Therefore, the sequence of questions allows you to guess the unknown number. It can be shown that there is no correct sequence of questions of length 2 or shorter.

    自己好菜数论好弱。。。。

    贴个题解

    If Petya didn't ask pk, where p is prime and k ≥ 1, he would not be able to distinguish pk - 1 and pk.

    That means, he should ask all the numbers pk. It's easy to prove that this sequence actually guesses all the numbers from 1 to n

    The complexity is O(N1.5) or O(NloglogN) depending on primality test.

     1 #include<iostream>
     2 using namespace std;
     3 int a[1111],prime[1111],isprime[1111];
     4 int num=0;
     5 void init()
     6 {
     7     for(int i=2;i<1111;i++)isprime[i]=1;
     8     for(int i=2;i<1111;i++)
     9     {
    10         if(isprime[i])
    11         {
    12             prime[num++]=i;
    13             for(int j=i*2;j<1001;j+=i)
    14                 isprime[j]=0;
    15         }
    16     }
    17 }
    18 int main()
    19 {
    20     init();
    21     int n;
    22     cin>>n;
    23     int ans=0;
    24     for(int i=0;i<num;i++)
    25     {
    26         int res=prime[i];
    27         while(res<=n)
    28         {
    29             a[ans++]=res;
    30             res*=prime[i];
    31         }
    32     }
    33     cout<<ans<<endl;
    34     if(ans==0)return 0;
    35     for(int i=0;i<ans-1;i++)
    36         cout<<a[i]<<" ";
    37     cout<<a[ans-1]<<endl;
    38 }
    View Code
  • 相关阅读:
    12C 中,发生脑裂时,节点保留策略
    如何修改集群的公网信息(包括 VIP)
    从 ASH 找到消耗 PGA 和 临时表空间 较多的 Top SQL_ID
    Oracle SCN详解
    10046 trace
    使用trace文件定位ORA-00060问题
    (转)计算机漏洞安全相关的概念POC 、EXP 、VUL 、CVE 、0DAY
    PowerShell 相关常用命令(update...)
    (转)主从同步常遇见问题处理-线上MYSQL同步报错故障处理总结
    pentestbox 安装后的基本设置
  • 原文地址:https://www.cnblogs.com/lmlyzxiao/p/4814811.html
Copyright © 2011-2022 走看看