codeforce--Vasya and Petya's Game
来源:互联网 发布:matlab 数组元素求和 编辑:程序博客网 时间:2024/05/16 07:43
网址:http://codeforces.com/contest/576/problem/A
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 numbersyi, 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 byk 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 to1.
If the unknown number is divisible by 4, it is 4.
If the unknown number is divisible by 3, then the unknown number is3.
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.
题解:
Task A. Div1.
If Petya didn't ask pk, wherep is prime and k ≥ 1, he would not be able to distinguishpk - 1 andpk.
That means, he should ask all the numbers pk. It's easy to prove that this sequence actually guesses all the numbers from1 to n
The complexity is O(N1.5) orO(NloglogN) depending on primality test.
其实没有必要用到vector ,并且开始的时候还因为用了直接WR了,因为开始的时候N取1005,这样的话就遍历越界了,应该增加vec.size()的限制,或者是下面的这样把数组开大点#include <iostream>#include <stdio.h>#include <math.h>#include <string>#include <queue>#include <string.h>#include <map>#include <set>#include <vector>#include <algorithm>#include <stdlib.h>using namespace std;#define rd(x) scanf("%d",&x)#define N 1005bool isprm[N];vector <int> vec;void isprime(){ int i,j,k=0; int s,e=sqrt( double(N) )+1; //sqrt是对于double数开平方 memset(isprm,1,sizeof(isprm)); //prm[k++]=2; isprm[0] = isprm[1] = 0; for(i=4 ;i < N; i=2+i)isprm[i]=0; for(i=3;i<e;i=2+i) if(isprm[i]) for(s=i*2,j=i*i;j<N;j=j+s) isprm[j]=0; for(int i=0;i<1000;i++) if(isprm[i]) vec.push_back(i);}int main(){ int t,b[1005],num=0; rd(t); isprime(); for(int i=1; i <= t ; i++) { if(isprm[i]){ int temp=i,j=1; while(temp<=t) b[num++]=temp,temp=temp*i; } } printf("%d\n",num); for(int i=0; i<num; i++) printf("%d ",b[i]); return 0;}
- codeforce--Vasya and Petya's Game
- CodeForce #319 div 2 C Vasya and Petya's Game
- 51nod-【codeforce】A. Vasya and Petya's Game
- 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
- 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。
- codeforce C - Vasya and Robot
- CodeForce 66A Petya and Java
- lintcode-最长连续子序列
- 格式化控制台输出颜色
- mysql共享表空间和独立表空间
- UIActivityIndicatorView的使用
- 在Servlet中引用json-lib-2.4-jdk15.jar报错的问题的解决办法
- codeforce--Vasya and Petya's Game
- 《程序员面试金典》--像素反转
- C++那些细节--typedef关键字
- java 斗地主的写法
- 维基百科简体中文语料的获取
- 分享一百多套开发视频教程的下载地址
- 沙盒 与数据本地化
- 二元语法(2-gram)分词中的平滑算法
- 使用XUtils上传文件(利用桥接模式将上传模块化)