A. Vasya and Petya's Game
来源:互联网 发布:无线通信算法 招聘 编辑:程序博客网 时间:2024/05/16 08:03
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
32 4 3
6
42 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.
解题说明:此题是给出1-n内的一个数k,然后输出一个询问序列yi, 在询问完后可以判断出这个数为什么。每次询问为k是否为yi的倍数。做法是从因子角度看k,我们只要确定k中质因子的种类以及对应的个数即可。看k中有多少个2,3,5。
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<cmath>using namespace std;int tmp[10000]={0};int main () {int n;scanf("%d",&n);int ans=0;for(int i=2;i<=n;i++) {if(tmp[i]==0) {for(int j=i+i;j<=n;j+=i) {tmp[j]++;}}}for(int i=2;i<=n;i++) {if(tmp[i]<2){ans++;}}printf("%d\n",ans);for(int i=2;i<=n;i++){if(tmp[i]<2){printf("%d ",i);}}return 0;}
- A. Vasya and Petya's Game
- A. Vasya and Petya's Game(cf第一题)
- CodeForces 576A - Vasya and Petya's Game
- coderforce 576A Vasya and Petya's Game
- 51nod-【codeforce】A. Vasya and Petya's Game
- codeforce--Vasya and Petya's Game
- CodeForce #319 div 2 C Vasya and Petya's Game
- codeforces Vasya and Petya's Game(筛选法)
- Codeforces Round #319 (Div. 2) ——C. Vasya and Petya's Game(数学题)
- Codeforces #319(Div.2) C. Vasya and Petya's Game(数学题)
- Codeforces Round #319 (Div. 2) 577C Vasya and Petya's Game(筛选法)
- Codeforces Round #319 (Div. 2) C. Vasya and Petya's Game
- CF#319-C. Vasya and Petya's Game-猜数-数论
- Codeforces Round #319 (Div. 2) C Vasya and Petya's Game
- 51nod 1536不一样的猜数游戏 O(n)素数筛选法。同Codeforces 576A Vasya and Petya's Game。
- 51nod 1536不一样的猜数游戏 思路:O(n)素数筛选法。同Codeforces 576A Vasya and Petya's Game。
- A. Petya and Strings
- A. Petya and Java
- 12299 - RMQ with Shifts(线段树单点更新、区间求最值)
- java 快速排序
- Swift学习笔记基础语法-整数类型
- UML用例图
- JS对象序列化为JSON对象
- A. Vasya and Petya's Game
- 配置Tomcat
- hadoop2集群安装和测试之window开发环境
- tnsping通过,但connect db遇ORA-12537 / ORA-12547 or TNS-12518问题处理
- JS操作JSON总结
- NodeJS
- Javascript与iframe的那些事儿
- 软件测试之常识篇 2
- Jquery取得iframe中元素的几种方法