codeforce--Vasya and Petya's Game
网址:http://codeforces.com/contest/576/problem/A
1 second
256 megabytes
standard input
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.
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.
题解:
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的更多相关文章
- 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/ ...
- 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 ...
- 数学 - 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整除,系统会回答 ...
- Codeforces Round #319 (Div. 2) C. Vasya and Petya's Game 数学题
C. Vasya and Petya's Game ...
- CF576A Vasya and Petya's Game
题目大意: 给定一个数字 n,现在 Vasya 要从 1∼n 中想一个数字 x. Petya 向 Vasya 询问 "x 是否能整除 y?" ,通过 Vasya 的回答来判断 x ...
- 【CodeForces 577C】Vasya and Petya’s Game
链接 某个数x属于[1,n],至少询问哪些数“x是否是它的倍数”才能判断x.找出所有质因数和质因数的幂即可. #include<cstdio> #include<algorithm& ...
- CF 577C Vasya and Petya's Game
题意:一个游戏,A童鞋在1~n的范围里猜一个数,B童鞋询问一个集合,A童鞋要对集合里每个数做出回答,他猜的数能否给整除,B要通过这些答案得到A猜的数,最少需要猜哪些数? 解法:一个数可以由若干个质数的 ...
- CodeForces 577C Vasya and Petya's Game 数学
题意就是给你一个1到n的范围 你每次可以问这个数是否可以被某一个数整除 问你要猜多少数才能确定这个数…… 一开始一点思路也没有 后来查了一下才知道 每个数都可以分为几个质数的整数次幂相乘得到…… #i ...
- 51nod 1536不一样的猜数游戏 思路:O(n)素数筛选法。同Codeforces 576A Vasya and Petya's Game。
废话不多说,先上题目. 51nod Codeforces 两个其实是一个意思,看51nod题目就讲的很清楚了,题意不再赘述. 直接讲我的分析过程:刚开始拿到手有点蒙蔽,看起来很难,然后......然后 ...
- codeforces 576a//Vasya and Petya's Game// Codeforces Round #319 (Div. 1)
题意:猜数游戏变种.先选好猜的数,对方会告诉你他想的那个数(1-n)能不能整除你猜的数,问最少猜几个数能保证知道对方想的数是多少? 对一个质数p,如果p^x不猜,那么就无法区分p^(x-1)和p^x, ...
随机推荐
- xml学习笔记一(概述)
XML 被设计用来传输和存储数据. HTML 被设计用来显示数据. 什么是 XML? XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类似 ...
- 透明度兼容性(ie8以上)
转载:http://www.cnblogs.com/PeunZhang/p/4089894.html demo代码:文件中,背景透明,文字不透明的研究和使用.zip
- SQL集合运算参考及案例(一):列值分组累计求和
概述 目前企业应用系统使用的大多数据库都是关系型数据库,关系数据库依赖的理论就是针对集合运算的关系代数.关系代数是一种抽象的查询语言,是关系数据操纵语言的一种传统表达方式.不过我们在工作中发现,很多人 ...
- 本地同时启动两个tomcat
本地同时启动两个tomcat 这几天开发用到了Ext JS4,所以着手学习Ext JS4,由于官方很多demo都是需要与服务器端进行数据交互,因此需要在tomcat里部署上官方的demo.而本地mye ...
- Jquery中的offset()和position()
今天遇到这个偏移量的问题,特做此记录.以便日后查看. 先看看这两个方法的定义. offset(): 获取匹配元素在当前视口的相对偏移. 返回的对象包含两个整形属性:top 和 left.此方法只对可见 ...
- HAML学习
来源:http://ningandjiao.iteye.com/blog/1772845 一个技术能够风靡,一定是有它的原因的,在熟悉之前,我们没有资格去对它做任何的判断. Haml 是一种简洁优美的 ...
- Android SDK 4.0.3 开发环境配置及运行
最近又装了一次最新版本的ADK环境 目前最新版是Android SDK 4.0.3 本文的插图和文本虽然是Android2.2的 步骤都是一样的,如果安装的过程中遇到什么问题,可以留言,我会尽快回复! ...
- SVN代码回滚命令之---merge的使用
一.改动还没被提交的情况(未commit) 这种情况下,见有的人的做法是删除work copy中文件,然后重新update,恩,这种做法达到了目的,但不优雅,因为这种事没必要麻烦服务端. 其实一个命令 ...
- 黄聪:优化清理WordPress数据库wp_options表(缩小autoload体积)
使得wp_options表变得庞大的重要原因:无用的RSS Feed Cache.如果你在wp_options表中发现了大量option_name包含“_transient”的数据,那就是它没跑了.先 ...
- 黄聪:Dsicuz x2.5、X3、X3.2如何去掉域名后面的/forum.php
Dsicuz x2.5去掉域名后面的/forum.php 1, 后台--全局--域名设置--应用域名--设置默认域名为访问域名就可以,如:www.xxxxx.com 上面2种方法都可以去掉域名后面的/ ...