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, ...
随机推荐
- SpringMVC的各种参数绑定方式
1. 基本数据类型(以int为例,其他类似):2. 包装类型(以Integer为例,其他类似):3. 自定义对象类型:4. 自定义复合对象类型:5. List绑定:6. Set绑定:7. Map绑定: ...
- EventBus学习
Git位置https://github.com/greenrobot/EventBus 使用起来很方便:1. Implement any number of event handling method ...
- Settings.System.getInt获取Setting里的设置信息
数据库的路径:/data/data/com.android.providers.settings. 获取飞行模式: Settings.System.getInt(mContext.getContent ...
- [mybatis] mybatis错误:Invalid bound statement (not found)
点击菜单抛出异常: org.springframework.web.util.NestedServletException: Request processing failed; nested exc ...
- 04文件与IO
文件系统调用: open.close.create.read.write open: int open(const char* path, int flags) path:要打开的文件的路径 flag ...
- 【solr】solr5.0整合中文分词器
1.solr自带的分词器远远满足不了中文分词的需求,经查使用最多的分词器是solr是mmseg4j分词器,具体整合大家可以参考 https://github.com/zhuomingliang/mms ...
- Keepalived高可用软件的安装与配置
监听和替换多台服务器之间的来回切换 一.安装tar zxvf keepalived-1.1.15.tar.gzcd keepalived-1.1.15./configure --prefix=/usr ...
- (C#) Lock - 将对象上锁,互斥多个线程,使同步。
C# Lock 原文:http://www.dotnetperls.com/lock Locking is essential in threaded programs. It restricts c ...
- Codeforces Round #363 (Div. 2) One Bomb
One Bomb 题意: 只有一个炸弹,并且一个只能炸一行和一列的'*',问最后能否炸完所以'*',如果可以输出炸弹坐标 题解: 这题做的时候真的没什么好想法,明知道b题应该不难,但只会瞎写,最后越写 ...
- [物理学与PDEs]第1章 电动力学
[物理学与PDEs]第1章第1节 引言 [物理学与PDEs]第1章第2节 预备知识 2.1 Coulomb 定律, 静电场的散度与旋度 [物理学与PDEs]第1章第2节 预备知识 2.2 Ampere ...