//一次性全部生成完,想用多少用多少,时间长点#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];//装78498个素数,0号废弃。 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位), //unsigned long long最大18446744073709551615 //20位 void generateFbnc( int n){//形成n个fibonacci数列数,供用。。 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)){//输出最近的和距离。 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; } /*找到第一个超过prime[i]的i*/ for(int i = 1; i <= 78499; i++){ if(t < prime[i]){ record = i; break; } } record -= 1; //三者成等差. if(prime[record] + prime[record+1] == 2* 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; } /* 样例输入 3 6 8 10 样例输出 5 1 7 1 11 1 */
时间: 2024-11-09 00:56:54