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.
A single line contains number n (1 ≤ n ≤ 103).
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.
4
3 2 4 3
6
4 2 4 3 5
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.
题意:A给出一个数x,B每次猜一个y,A回答B,x是否可以被y整除,求出要猜的最小次数和需要猜的数。
素数筛处理出所有素数,枚举每个素数p,可以知道如果p^k<n,则p^k一定需要选,根据这个原则求出所有要猜的数。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
const int MAXN = 1299709;
int isprime[MAXN];
int prime[MAXN];
bool vis[MAXN];
void getPrime()
{
for (int i = 2; i*i < MAXN; i++)
{
if (!vis[i])
{
isprime[i] = 1;
for (int j = i; j < MAXN; j = j + i)
{
vis[j] = true;
}
}
}
for (int i = 2; i < MAXN; i++)
{
if (!vis[i])
{
isprime[i] = 1;
}
}
int ji = 0;
for (int i = 2; i < MAXN; i++)
{
if (isprime[i])
{
prime[ji] = i;
ji++;
}
}
}
int main()
{
getPrime();
int n;
vector<int>ans;
while (scanf("%d", &n) != EOF)
{
ans.clear();
for (int i = 0; i <= prime[i]; i++)
{
int x = prime[i];
while (x <= n)
{
ans.push_back(x);
x = x*prime[i];
}
}
printf("%d\n", ans.size());
for (int i = 0; i < ans.size(); i++)
{
if (i)
printf(" ");
printf("%d", ans[i]);
}
printf("\n");
}
}