题目:
B. Omkar and Last Class of Math
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
In Omkar’s last class of math, he learned about the least common multiple, or LCM. LCM(a,b) is the smallest positive integer x which is divisible by both a and b.
Omkar, having a laudably curious mind, immediately thought of a problem involving the LCM operation: given an integer n, find positive integers a and b such that a+b=n and 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 t (1≤t≤10). Description of the test cases follows.
Each test case consists of a single integer n (2≤n≤109).
Output
For each test case, output two positive integers a and b, such that a+b=n and LCM(a,b) is the minimum possible.
Example
input
3
4
6
9
output
2 2
3 3
3 6
Note
For the first test case, the numbers we can choose are 1,3 or 2,2. LCM(1,3)=3 and LCM(2,2)=2, so we output 2 2.
For the second test case, the numbers we can choose are 1,5, 2,4, or 3,3. LCM(1,5)=5, LCM(2,4)=4, and LCM(3,3)=3, so we output 3 3.
For the third test case, LCM(3,6)=6. It can be shown that there are no other pairs of numbers which sum to 9 that have a lower LCM.
##题解
简短的解法:两个整数k和n-k,(1<=k<=n),k是n的最大的合适的因数。
证明:现有两个整数k和n-k,不妨假设k<=n-k,这也就意味着n-k>=n/2。
我们首先说明:
如果k|n的话,LCM(k,n-k)=n-k
我们现在演示当k/|n时LCM(k,n-k)>=n的情况。这里的证明借助之前那个a|b时,LCM(a,b)=b,如果k/|n,那么k/|n-k.所以LCM(k,n-k)<>n-k.并且因为LCM(k,n-k)必须是k和n-k的乘积,这里遵循LCM(k,n-k)>=2(n-k)>=2(n/2)=n(基本不等式证明)
我们现在已经给出了,如果想让LCM(k,n-k)最小,k必须是n的一个因数,并且,由于当k是n的因数时,想让LCM(k,n-k)最小,我们必须要使得k是n的最大因数(n除外)
下面开始寻找这个符合条件的最大的k。如果p是可以被n整除的最小质数,那么k=n/p.所以只要从最小素数2开始枚举就行了,(2<=p<=sqrt(n)),如果n不是质数,那么这个p一定可以找到,否则结果一定是1,n-1这个组合。
code:
#include
using namespace std;
int t,n;
bool f;
bool isPrime( int num ) //用时最快40ms
{
if(num ==2|| num==3 )
return 1 ;
if(num %6!= 1&&num %6!= 5)
return 0 ;
int tmp =sqrt( num);
for(int i= 5;i <=tmp; i+=6 )
if(num %i== 0||num %(i+ 2)==0 )
return 0 ;
return 1 ;
}
int main(){
cin>>t;
while (t--){
cin>>n; f=false;
if (n%2==0){
cout<<n/2<<' '<<n/2<<endl;
}
else{
if (isPrime(n)){
cout<<1<<' '<<n-1<<endl;
continue;
}
for (int i=2; i<=sqrt(n); i++){
if (isPrime(i) && n%i==0){
cout<<n/i<<' '<<n-n/i<<endl;
f=true;
break;
}
}
}
}
return 0;
}