Codeforces Round #319 (Div. 2) C Vasya and Petya's Game
2016-05-29 11:18
441 查看
思路:素数筛处理出所有素数,枚举每个素数p,可以知道如果p^k<n,则p^k一定需要选,根据这个原则求出所有要猜的数。
#include<bits\stdc++.h>
using namespace std;
const int maxn = 1105;
int b[maxn];
int a[maxn];
int prim()
{
memset(a,0,sizeof(a));
for(int i = 2;i<=maxn;i++)
{
if(!a[i])
a[++a[0]] = i;
for(int j = 1;j<=a[0]&&a[j]<=maxn/i;j++)
{
a[a[j]*i]=1;
if(i%a[j]==0)
break;
}
}
}
int main()
{
int n;
cin >> n;
prim();
int pos = 0;
for(int i = 1;i<a[0];i++)
{
int temp = a[i];
int p = 1;
for(int j = 1;j<maxn;j++)
{
p*=a[i];
if(p<=n)
b[pos++]=p;
else
break;
}
if(temp > n)
break;
}
cout << pos << endl;
if(pos>=1)
cout << b[0];
for(int i = 1;i<pos;i++)
cout << " " << b[i];
cout << endl;
}
C. 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.
Examples
input
output
input
output
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.
#include<bits\stdc++.h>
using namespace std;
const int maxn = 1105;
int b[maxn];
int a[maxn];
int prim()
{
memset(a,0,sizeof(a));
for(int i = 2;i<=maxn;i++)
{
if(!a[i])
a[++a[0]] = i;
for(int j = 1;j<=a[0]&&a[j]<=maxn/i;j++)
{
a[a[j]*i]=1;
if(i%a[j]==0)
break;
}
}
}
int main()
{
int n;
cin >> n;
prim();
int pos = 0;
for(int i = 1;i<a[0];i++)
{
int temp = a[i];
int p = 1;
for(int j = 1;j<maxn;j++)
{
p*=a[i];
if(p<=n)
b[pos++]=p;
else
break;
}
if(temp > n)
break;
}
cout << pos << endl;
if(pos>=1)
cout << b[0];
for(int i = 1;i<pos;i++)
cout << " " << b[i];
cout << endl;
}
C. 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.
Examples
input
4
output
3 2 4 3
input
6
output
42 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.
相关文章推荐
- linux生成随机密码
- 在vs环境中跑动sift特征提取(原理部分)
- 程序是怎么跑起来的-2
- scala学习之Map、Tuple、zip
- springmvc--为什么用它
- 第十四周项目2.1 两个成员的类模板 (1)
- [MongoDB]增删改查
- V
- js实现可控制左右方向的无缝滚动效果
- 第五届河南省程序设计大赛
- Git Day03,GitHub 1st
- Android 命名规范 (提高代码可以读性)
- Mysql常用命令行大全
- 比例矩形RatioLayout
- 控制台输入一个数组,然后倒序输出
- php的控制器链
- 正则表达式在文本过滤中的使用
- 1023-W专题三
- Linux I2C子系统分析之(一) ----- 用GPIO模拟I2C总线
- 第十四周项目 1 排序函数模板