Codeforces Codeforces Round #319 (Div. 2) C. Vasya and Petya's Game 数学

本文详细解析了C.VasyaandPetya'sGame的解题思路,并提供了实现代码。通过理解最小询问次数与素数、素数幂的关系,我们能够高效解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Vasya and Petya's Game

Time Limit: 1 Sec  

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/577/problem/C

Description

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 Input

4

Sample Output

3

2 4 3

HINT

 

题意

在1-n中随便选一个数,然后你可以提问,问这个数是否%y==0

问你最少问多少次,可以确定这个数

题解:

假设,你没有问p^k(p是素数,k>1),那么你是不能够分辨p^k-1 和p^k的

所以你就必须问咯

所以最后答案只需要把小于等于n的素数以及素数的幂都输出出来就好了

代码:

//qscqesze
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 500001
#define mod 1001
#define eps 1e-9
#define pi 3.1415926
int Num;
//const int inf=0x7fffffff;
const ll inf=999999999;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//*************************************************************************************
vector<int> ans;
vector<int> Q;
int main()
{
    int n=read();
    if(n==1)
    {
        printf("0");
        return 0;
    }
    for(int i=2;i<=n;i++)
    {
        int flag = 0;
        for(int j=2;j*j<=i;j++)
        {
            if(i%j)continue;
            flag=1;break;
        }
        if(!flag)ans.push_back(i);
    }
    for(int i=0;i<ans.size();i++)
    {
        int j = ans[i];
        for(;j<=n;j*=ans[i])
            Q.push_back(j);
    }
    printf("%d\n",Q.size());
    for(int i=0;i<Q.size();i++)
        printf("%d ",Q[i]);
    printf("\n");
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值