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
题意:给你一个数n,你给k个数,告诉你整除不整除,用最少的数确定那个数;
思路:根据唯一分解定理,利用素数和素数的次方即可,详见代码;
#include<bits/stdc++.h> using namespace std; #define ll __int64 #define mod 1000000007 #define pi (4*atan(1.0)) const int N=1e2+10,M=1e6+10,inf=1e9+10; int prime(int n) { if(n<=1) return 0; if(n==2) return 1; if(n%2==0) return 0; int k, upperBound=n/2; for(k=3; k<=upperBound; k+=2) { upperBound=n/k; if(n%k==0) return 0; } return 1; } int p[10010],flag; int ans[10010]; int main() { int x,y,z,i,t; flag=0; for(i=2;i<1000;i++) if(prime(i)) p[flag++]=i; scanf("%d",&x); int ji=0; for(i=0;i<flag;i++) { if(x<p[i])break; int base=p[i]; while(x>=base) { ans[ji++]=base; base*=p[i]; } } printf("%d ",ji); for(i=0;i<ji;i++) printf("%d ",ans[i]); return 0; }