提交时间:2023-05-27 16:56:49
运行 ID: 74631
#include<bits/stdc++.h> typedef long long LL; using namespace std; int prim[9600], vis[100005], tot, num[6], nu[6]; char s[6]; void get_prim(){ for (int i = 2; i < 100000; i++){ if(!vis[i]) prim[tot++] = i; for (int j = 0; j < tot && prim[j] * i < 100000; j++){ vis[prim[j] * i] = 1; if(i % prim[j] == 0) break; } } } int main(){ get_prim(); cin>>s; for (int i = 0; i < 5; i++) num[i] = s[i] - '0'; int tim = 0x3f3f3f3f, ans; for (int i = 0; i < tot; i++){ int cnt = 4, t = prim[i], w = 0; while(t){ nu[cnt--] = t % 10; t /= 10; } while(cnt >= 0) nu[cnt--] = 0; for (int i = 0; i < 5; i++){ int t1 = nu[i] - num[i], t2 = num[i] - nu[i]; if(t1 < 0) t1 += 10; if(t2 < 0) t2 += 10; w += min(t1, t2); } if(tim >= w){ tim = w; ans = prim[i]; } } int t = ans, cnt = 0; while(t) cnt++, t /= 10; for (int i = 1; i <= 5 - cnt; i++) cout<<0; cout<<ans; return 0; }