网址:http://codeforces.com/contest/576/problem/A

A. Vasya and Petya's Game
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 test(s)
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.

题解:

Task A. Div1.

If Petya didn't ask pk, where
p is prime and k ≥ 1, he would not be able to distinguish
pk - 1 and
pk.

That means, he should ask all the numbers pk. It's easy to prove that this sequence actually guesses all the numbers from
1 to n

The complexity is O(N1.5) or
O(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 1005
bool 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的更多相关文章

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

    C. Vasya and Petya's Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...

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

    C. Vasya and Petya's Game time limit per test 1 second memory limit per test 256 megabytes input sta ...

  3. 数学 - Codeforces Round #319 (Div. 1)A. Vasya and Petya's Game

    Vasya and Petya's Game Problem's Link Mean: 给定一个n,系统随机选定了一个数x,(1<=x<=n). 你可以询问系统x是否能被y整除,系统会回答 ...

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

                                                     C. Vasya and Petya's Game                           ...

  5. CF576A Vasya and Petya's Game

    题目大意: 给定一个数字 n,现在 Vasya 要从 1∼n 中想一个数字 x. Petya 向 Vasya 询问 "x 是否能整除 y?" ,通过 Vasya 的回答来判断 x ...

  6. 【CodeForces 577C】Vasya and Petya’s Game

    链接 某个数x属于[1,n],至少询问哪些数“x是否是它的倍数”才能判断x.找出所有质因数和质因数的幂即可. #include<cstdio> #include<algorithm& ...

  7. CF 577C Vasya and Petya's Game

    题意:一个游戏,A童鞋在1~n的范围里猜一个数,B童鞋询问一个集合,A童鞋要对集合里每个数做出回答,他猜的数能否给整除,B要通过这些答案得到A猜的数,最少需要猜哪些数? 解法:一个数可以由若干个质数的 ...

  8. CodeForces 577C Vasya and Petya's Game 数学

    题意就是给你一个1到n的范围 你每次可以问这个数是否可以被某一个数整除 问你要猜多少数才能确定这个数…… 一开始一点思路也没有 后来查了一下才知道 每个数都可以分为几个质数的整数次幂相乘得到…… #i ...

  9. 51nod 1536不一样的猜数游戏 思路:O(n)素数筛选法。同Codeforces 576A Vasya and Petya's Game。

    废话不多说,先上题目. 51nod Codeforces 两个其实是一个意思,看51nod题目就讲的很清楚了,题意不再赘述. 直接讲我的分析过程:刚开始拿到手有点蒙蔽,看起来很难,然后......然后 ...

  10. codeforces 576a//Vasya and Petya's Game// Codeforces Round #319 (Div. 1)

    题意:猜数游戏变种.先选好猜的数,对方会告诉你他想的那个数(1-n)能不能整除你猜的数,问最少猜几个数能保证知道对方想的数是多少? 对一个质数p,如果p^x不猜,那么就无法区分p^(x-1)和p^x, ...

随机推荐

  1. html之ol标签

    有序列表,请使用 CSS 来定义列表的类型. 通常和li配对使用 HTML5新属性: compact reversed:降序 start:有序列表的起始值 type:在列表中使用标记类型(1,A,a, ...

  2. Android: 在 TextView 里使用删除线

    Android: 在 TextView 里使用删除线 分类: Android2014-09-25 13:17 3431人阅读 评论(0) 收藏 举报 以编程的方式添给 TextView 添加删除线: ...

  3. .NET分布式事务未提交造成6107错误或系统被挂起的问题分析定位

    问题描述: 系统中多个功能不定期出现“Unable to get error message (6107) (0).”错误,即分布式事务超时,但报出错误的部分功能根本没有使用分布式事务. 原因分析: ...

  4. C#遍历hashtable

    foreach (DictionaryEntry de in hashTable) { System.Windows.Forms.MessageBox.Show(de.Key.ToString()); ...

  5. Servlet程序访问的流程

    方式一: jsp: <font color="red">${ msg }</font> <form action="/personal/re ...

  6. R提高篇(四): 数据管理二

    目录: 数学函数 统计函数 应用示例 控制流 数学函数 ceiling(x):  大于等于 x  的最小整数, 如:  ceiling(3.213)  --> 4 floor(x):     小 ...

  7. Struts2 Interceptors

    Alias Interceptor : 别名拦截器 <action name="someAction" class="com.examples.SomeAction ...

  8. 【转】sql server2005中raiserror的用法

    raiserror  是由单词 raise error 组成     raise  增加; 提高; 提升 raiserror 的作用: raiserror 是用于抛出一个错误.[ 以下资料来源于sql ...

  9. TMS320C54x系列DSP的CPU与外设——第5章 数据寻址

    第5章 数据寻址 C54x DSP提供7种基本寻址方式. ■ Immediate addressing uses the instruction to encode a fixed value.    ...

  10. onNewIntent调用时机

    在IntentActivity中重写下列方法:onCreate onStart onRestart  onResume  onPause onStop onDestroy  onNewIntent 一 ...