标签:while swap name wap bsp 个数 include sizeof algorithm
题意:有T个样例,每个样例输入两个数 a 和 b,让你找到一个数,使得 a + x = c, b + x = d, c 和 d
是质数并且相邻。
解法:打素数表(埃式筛法)
不成立: (b - a)&1
或 b == a
成立:
特殊: a = 1 , b = 2, ans = 1;
a = 2, b = 3, ans = 0;
(c != a, d != b, b - a = d - c ) && b <= d, ans = d - b;
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int N = 2e7 + 7; bool vis[N]; int primes[N]; int idx; int T,a,b; void prime() //打表 { memset(vis,0,sizeof(vis)); idx = 0; for(int i = 2; i < N; i++) { if(!vis[i]) { primes[idx] = i; idx++; for(int j = 2*i; j < N; j += i) vis[j] = true; } } } int main() { int s = 0; prime(); scanf("%d",&T); while(T--) { s++; scanf("%d%d",&a,&b); if(a>b) swap(a,b); int c = b - a; int t = 0; if(a == 1 && b == 2) { printf("Case %d: %d\n",s,1); continue; } if(a == 2 && b == 3) { printf("Case %d: %d\n",s,0); continue; } if(c & 1 || a == b) { printf("Case %d: %d\n",s,-1); continue; } else { for(int i = 1; i < idx; i++) { t = primes[i] - primes[i-1]; if(t == c && b <= primes[i]) { printf("Case %d: %d\n",s,primes[i]-b); break; } } } } return 0; }
标签:while swap name wap bsp 个数 include sizeof algorithm
原文地址:https://www.cnblogs.com/Edviv/p/12234631.html