Codeforces #319(Div.2) C. Vasya and Petya's Game(数学题)
来源:互联网 发布:消除马赛克的软件 编辑:程序博客网 时间:2024/04/29 13:46
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
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 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.
题意:A给出一个数x,B每次猜一个y,A回答B,x是否可以被y整除,求出要猜的最小次数和需要猜的数。
素数筛处理出所有素数,枚举每个素数p,可以知道如果p^k<n,则p^k一定需要选,根据这个原则求出所有要猜的数。
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<vector>using namespace std;const int MAXN = 1299709;int isprime[MAXN];int prime[MAXN];bool vis[MAXN];void getPrime(){for (int i = 2; i*i < MAXN; i++){if (!vis[i]){isprime[i] = 1;for (int j = i; j < MAXN; j = j + i){vis[j] = true;}}}for (int i = 2; i < MAXN; i++){if (!vis[i]){isprime[i] = 1;}}int ji = 0;for (int i = 2; i < MAXN; i++){if (isprime[i]){prime[ji] = i;ji++;}}}int main(){getPrime();int n;vector<int>ans;while (scanf("%d", &n) != EOF){ans.clear();for (int i = 0; i <= prime[i]; i++){int x = prime[i];while (x <= n){ans.push_back(x);x = x*prime[i];}}printf("%d\n", ans.size());for (int i = 0; i < ans.size(); i++){if (i)printf(" ");printf("%d", ans[i]);}printf("\n");}}
- Codeforces Round #319 (Div. 2) ——C. Vasya and Petya's Game(数学题)
- Codeforces #319(Div.2) C. Vasya and Petya's Game(数学题)
- CodeForce #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
- Codeforces Round #319 (Div. 2) C Vasya and Petya's Game
- CodeForces 576A - Vasya and Petya's Game
- codeforces Vasya and Petya's Game(筛选法)
- A. Vasya and Petya's Game(cf第一题)
- codeforce--Vasya and Petya's Game
- A. Vasya and Petya's Game
- CF#319-C. Vasya and Petya's Game-猜数-数论
- coderforce 576A Vasya and Petya's Game
- 51nod-【codeforce】A. Vasya and Petya's Game
- Codeforces Round #262 (Div. 2)460A. Vasya and Socks(简单数学题)
- 51nod 1536不一样的猜数游戏 O(n)素数筛选法。同Codeforces 576A Vasya and Petya's Game。
- 51nod 1536不一样的猜数游戏 思路:O(n)素数筛选法。同Codeforces 576A Vasya and Petya's Game。
- codeforces #354 div.2 C &&676C Vasya and String
- php查找字符是否存在
- C++ 容器Vector简介
- 【CCF】网络延时
- c++文件读写
- STL之partial_sum
- Codeforces #319(Div.2) C. Vasya and Petya's Game(数学题)
- RMQ问题
- JAVA-变量
- PHP Global变量定义当前页面的全局变量
- Item 26:为什么要推迟变量的定义? Effective C++笔记
- swfit 重载/自定义运算符
- web容器中使用Timer定时器
- Leet Code 21 Merge Two Sorted Lists
- poj2965解题报告