筛选法(质数)
来源:互联网 发布:111是什么意思网络语言 编辑:程序博客网 时间:2024/06/05 11:16
Vasya and Petya’s Game
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.
Example
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.
题意:猜数游戏,用最少的询问次数猜出数值是什么,我们可以想到,任意数都可以由质数组成。如 x = p1^c1 * p2^c2 * p3^c3……pn^cn. p1到pi都是质数。故我们只需要询问 p^c ,就可以推断出正确的数字。
#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"); }}
- 筛选法(质数)
- Eratosthence筛选法(质数)
- 筛选法求质数(素数)
- 求质数:筛选法(埃拉托色尼筛法)
- 筛选法求质数(acm)
- 筛选法求质数
- 质数筛选法
- 用筛选法求质数
- PHP筛选法求质数。
- 筛选法求质数(注解)
- Eratosthenes筛选法计算质数
- 用筛选法求质数
- (7)筛选求质数
- 质数筛选方法(埃拉托斯特尼筛法)
- 基于埃拉托色尼质数筛选法求最大公因数(java)
- 快速线性筛选法求素数(质数)
- 质数筛选
- 筛选质数
- 常用但易忘记脚本命令
- Linux下 /proc/maps 文件分析
- 多线程基础四(一)、停止不了的线程(interrupted、isInterrupted)
- jquery 根据name属性值查找
- 文件完整性校验——关于散列值和数字签名
- 筛选法(质数)
- javase-static
- 选择排序
- 【LeetCode】20. Valid Parentheses
- Python异常信息的捕获和处理
- 流媒体开发: RTP Header解析及定义
- ORACLE 创建用户步骤及常见错误
- 关于EnumChildWindows使用中的一个问题
- 计算机系统体系