Codeforces Round #641 (Div. 2) A. Orac and Factors(数学)
Orac is studying number theory, and he is interested in the properties of divisors.
For two positive integers aa and bb , aa is a divisor of bb if and only if there exists an integer cc , such that a⋅c=ba⋅c=b .
For n≥2n≥2 , we will denote as f(n)f(n) the smallest positive divisor of nn , except 11 .
For example, f(7)=7,f(10)=2,f(35)=5f(7)=7,f(10)=2,f(35)=5 .
For the fixed integer nn , Orac decided to add f(n)f(n) to nn .
For example, if he had an integer n=5n=5 , the new value of nn will be equal to 1010 . And if he had an integer n=6n=6 , nn will be changed to 88 .
Orac loved it so much, so he decided to repeat this operation several times.
Now, for two positive integers nn and kk , Orac asked you to add f(n)f(n) to nn exactly kk times (note that nn will change after each operation, so f(n)f(n) may change too) and tell him the final value of nn .
For example, if Orac gives you n=5n=5 and k=2k=2 , at first you should add f(5)=5f(5)=5 to n=5n=5 , so your new value of nn will be equal to n=10n=10 , after that, you should add f(10)=2f(10)=2 to 1010 , so your new (and the final!) value of nn will be equal to 1212 .
Orac may ask you these queries many times.
The first line of the input is a single integer t (1≤t≤100)t (1≤t≤100) : the number of times that Orac will ask you.
Each of the next tt lines contains two positive integers n,k (2≤n≤106,1≤k≤109)n,k (2≤n≤106,1≤k≤109) , corresponding to a query by Orac.
It is guaranteed that the total sum of nn is at most 106106 .
Print tt lines, the ii -th of them should contain the final value of nn in the ii -th query by Orac.
3 5 1 8 2 3 4
10 12 12
首先注意到n为偶数的话其最小因子一定是2,直接输出n+2*k即可。
然后预处理出每个数的最小因子a[i](类似筛法),n是奇数的话直接输出n+a[i]+2*(n-1)即可。因为奇数的最小因子还是奇数,奇数加奇数就成了偶数。
#include <bits/stdc++.h> using namespace std; int a[2000005]={0}; int main() { int t; cin>>t; int i; for(i=2;i<=2000000;i++) { if(!a[i]) { int j; for(j=i;j<=2000000;j+=i) { if(!a[j])a[j]=i; } } } while(t--) { int n,k; cin>>n>>k; if(n&1) cout<<n+a[n]+(k-1)*2<<endl; else cout<<n+2*k<<endl; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!