Once all generated, want to use how much, a long time
#include <iostream> #include <stdio.h>char min (char A, char b) {return (a>b) b:a;} Char Max (char A, char b) {return (a>b) a:b;} void Swapintorchar (int a, int b) {a^=b^=a^=b;} BOOL IsPrime (int n) {if (n==1) return false; for (int i = 2; i*i <= n; i++) if (n% i = = 0) return false; return true;} int prime[78500];//78,498 primes, no. No. 0 discarded. void Generateprimearray (int n) {for (int i = 2, j = 1; I <= n; i++) if (IsPrime (i)) prime[j++] = i;} /*--------Fibonacci-----*/unsigned Long Long f[50];//f[92]= 12200 16041 51218 76738 (20 bit),//unsigned long long Max 18446744 073709551615//20-bit void generatefbnc (int n) {//n Fibonacci sequence number, for use. F[0] = f[1] = 1; for (int i = 2; i < n; i++) f[i] = F[i-1] + f[i-2];} Using namespace Std;int main () {int A, B, C, D, E; printf ("%d\n", IsPrime (1000003)); Generateprimearray (1000003); printf ("%d%d\n", prime[78498], prime[78499]); int loop_rounds; while (~SCANF ("%d", &loop_rounds)) {//output closest and distance. while (loop_rounds--) { int T, record; scanf ("%d", &t); if (IsPrime (t)) {printf ("%d 0\n", t); Continue }else if (t = = 1) {printf ("2 1\n"); Continue }/* Find the first i*/for (int i = 1; I <= 78499; i++) {if (T < Prime[i]) {prime[i record = i; Break }} record-= 1; The three are in a linear form. if (Prime[record] + prime[record+1] = = t) {printf ("%d%d\n", Prime[record], T-prime[record]); Continue } if (T-prime[record] < prime[record+1]-t) printf ("%d%d\n", Prime[record], T-prime[record]); else printf ("%d%d\n", prime[record+1], prime[record+1]-t); }} return 0;} /* Sample input 36810 sample output 5 17 111 1*/
Nyoj basic problem: prime distance