当前位置:网站首页>4520. 质数
4520. 质数
2022-08-05 06:42:00 【NEFU AB-IN】
Powered by:NEFU AB-IN
4520. 质数
题意
给定一个正整数 X,请你在 X 后面添加若干位数字(至少添加一位数字;添加的数不能有前导0),使得结果为质数,在这个前提下所得的结果应尽量小。
思路
打出素数表来,可以看到素数的结尾都是1、3、7、9,所以可以先打出素数表来,然后dfs判断即可
代码
/* * @Author: NEFU AB-IN * @Date: 2022-08-04 10:11:30 * @FilePath: \Acwing\4520\4520.cpp * @LastEditTime: 2022-08-04 11:02:25 */ #include <bits/stdc++.h> using namespace std; #define int long long #define SZ(X) ((int)(X).size()) #define IOS \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define DEBUG(X) cout << #X << ": " << X << '\n' typedef pair<int, int> PII; const int INF = INT_MAX; const int N = 1e5 + 10; int prime[N], st[N], cnt; void solve() { int n; cin >> n; vector<int> v = { 1, 3, 7, 9}; function<int(int, int)> dfs = [&](int n, int p) { int y, x; for (int i = 0; i < 4; ++i) { y = 10 * (p - 1) + v[i]; // 尾数 string t = to_string(n) + to_string(y); x = stol(t); if (!st[x]) return x; } return dfs(n, p + 1); }; cout << dfs(n, 1) << '\n'; return; } signed main() { IOS; int T; cin >> T; function<void()> init = [&] { for (int i = 2; i < N; ++i) { if (!st[i]) { prime[cnt++] = i; } for (int j = 0; prime[j] < N / i; ++j) { st[prime[j] * i] = 1; if (i % prime[j] == 0) break; } } }; init(); while (T--) solve(); return 0; }
边栏推荐
- PCI Pharma Services宣布斥资数百万美元扩建英国制造设施,以满足市场对支持肿瘤治疗的全球高效药制造服务日益增长的需求
- Vulnhub靶机:HA_ NARAK
- 1, Citrix XenDesktop 2203 AD domain system installation (1)
- Takeda Fiscal 2022 First Quarter Results Strong; On Track to Achieve Full-Year Management Guidance
- 给网站套上Cloudflare(以腾讯云为例)
- RNote108---显示R程序的运行进度
- UDP broadcast
- IO process thread -> communication between processes -> day7
- 开源中国活动合作说明书
- After working for 3 years, I recalled the comparison between the past and the present when I first started, and joked about my testing career
猜你喜欢
随机推荐
After working for 3 years, I recalled the comparison between the past and the present when I first started, and joked about my testing career
typescript62-泛型工具类型(record)
MySql面试题总结
线程池的使用(结合Future/Callable使用)
Vulnhub靶机:HA_ NARAK
C# FileSystemWatcher
【Dynamic type detection Objective-C】
对数据类型而言运算符无效。运算符为 add,类型为 text。
Rapid Medical's Ultra-Small and Only Adjustable Thromb Retriever Receives FDA Clearance
Modeling of the MAYA ship
re正则表达式
《PyTorch深度学习实践》第十一课(卷积神经网络CNN高级版)
MySQL: basic part
在STM32中使用printf函数
AI+视频技术助力保障校园安全,校园智能安防平台该如何建设?
UDP组(多)播
Shiny04---DT和进度条在shiny中的应用
Kioxia and Aerospike Collaborate to Improve Database Application Performance
RK3568 environment installation
共享内存+inotify机制实现多进程低延迟数据共享