Codeforces Round #655 (Div. 2) B. Omkar and Last Class of Math(数论)
In Omkar's last class of math, he learned about the least common multiple, or LCMLCM . LCM(a,b)LCM(a,b) is the smallest positive integer xx which is divisible by both aa and bb .
Omkar, having a laudably curious mind, immediately thought of a problem involving the LCMLCM operation: given an integer nn , find positive integers aa and bb such that a+b=na+b=n and LCM(a,b)LCM(a,b) is the minimum value possible.
Can you help Omkar solve his ludicrously challenging math problem?
Input
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤101≤t≤10 ). Description of the test cases follows.
Each test case consists of a single integer nn (2≤n≤1092≤n≤109 ).
Output
For each test case, output two positive integers aa and bb , such that a+b=na+b=n and LCM(a,b)LCM(a,b) is the minimum possible.
Example
Input
Copy
3
4
6
9
Output
Copy
2 2
3 3
3 6
观察样例+猜测会发现,n为偶数的话直接除以2,奇数的话两个数一定为倍数关系,而若为倍数,又想lcm尽可能小,必须让小的那个数尽可能大。又因为两个数和为n,故较小的那个数是n的最大的那个因子(除去自己)。凭直觉瞎猜的结论,于是转化为找n最大的因子,复杂度根n。
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while(t--) { int n; cin >> n; if(n & 1) { int mmax = 0; for(int i = 1; i <= sqrt(n); i++) { if(n % i == 0) { mmax = max(mmax, i); if(i != 1) mmax = max(mmax, n / i); } } cout << n - mmax << ' ' << mmax << endl; } else { cout << n/2 << ' ' << n/2 << endl; } } return 0; }
【推荐】国内首个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框架的用法!