Nyoj basic problem: prime distance

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.