Find the prime number between 1~n |
Difficulty level: A; run time limit: 1000ms; operating space limit: 256000KB; code length limit: 2000000B |
Question Description |
The number of primes is greater than 1 and cannot be divisible by other integers except 1 and itself. Requires a prime number between the output 1~n. |
Input |
Positive integer n |
Output |
All primes between 1~n, including N, are separated by a space, and no spaces are allowed before the first number. |
Input example |
10 |
Output example |
2 3 5 7 |
Other Notes |
n is a positive integer greater than 1 and not greater than 100 |
The puzzle: You can practice Mr (neither of them)
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <algorithm>5#include <queue>6#include <cstring>7#include <ctime>8 #definePAU Putchar (")9 #defineENT Putchar (' \ n ')Ten using namespacestd; One Const intSafe=3; A intgcdintAintb) {return!B?A:GCD (b,a%b);} - intMulintAintBintp) { - intTmp= (a*b-(int)((Double) a/p*b+1e-8)*p); the returntmp<0? tmp+p:tmp; - } - intPowintAintBintp) { - intans=1; a%=p; + for(inti=b;i;i>>=1, a=Mul (a,a,p)) - if(i&1) ans=Mul (ans,a,p); + returnans; A } at BOOLCheckintAintNintRints) { - intAns=pow (a,r,n), p=ans; - for(intI=1; i<=s;i++){ -ans=Mul (ans,ans,n); - if(ans==1&&p!=1&&p!=n-1)return true; -p=ans; in}if(ans!=1)return true;return false; - } to BOOLMR (intN) { + if(n<=1)return false; - if(n==2)return true; the if(! (n&1))return false; * intr=n-1, s=0; $ while(! (r&1)) r>>=1, s++;Panax Notoginseng for(intI=0; i<safe;i++) - if(Check (rand ()% (n1)+1, n,r,s))return false; the return true; + } AInlineintRead () { the intx=0, sig=1;CharCh=GetChar (); + while(!isdigit (CH)) {if(ch=='-') sig=-1; ch=GetChar ();} - while(IsDigit (CH)) x=Ten*x+ch-'0', ch=GetChar (); $ returnx*=Sig; $ } -InlinevoidWriteintx) { - if(x==0) {Putchar ('0');return;}if(x<0) Putchar ('-'), x=-x; the intlen=0, buf[ the]; while(x) buf[len++]=x%Ten, x/=Ten; - for(inti=len-1; i>=0; i--) Putchar (buf[i]+'0');return;Wuyi } the BOOLfirst=true; - voidinit () { WuSrand (Time (0)); - intn=read (); About for(intI=2; i<=n;i++){ $ if(MR (i)) { - if(first) first=false; - ElsePAU; -printf"%d", i); A } + } the return; - } $ voidWork () { the return; the } the voidprint () { the return; - } in intMain () {init (); work ();p rint ();return 0;}
Coj 3018 to find the prime number between 1~n