Codeforces Round #655 (Div. 2) B. Omkar and Last Class of Math

B. Omkar and Last Class of Math

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 (1t101≤t≤10). Description of the test cases follows.

Each test case consists of a single integer nn (2n1092≤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
Note

For the first test case, the numbers we can choose are 1,31,3 or 2,22,2. LCM(1,3)=3LCM(1,3)=3 and LCM(2,2)=2LCM(2,2)=2, so we output 2 22 2.

For the second test case, the numbers we can choose are 1,51,5, 2,42,4, or 3,33,3. LCM(1,5)=5LCM(1,5)=5, LCM(2,4)=4LCM(2,4)=4, and LCM(3,3)=3LCM(3,3)=3, so we output 3 33 3.

For the third test case, LCM(3,6)=6LCM(3,6)=6. It can be shown that there are no other pairs of numbers which sum to 99 that have a lower LCMLCM.

题意:给你一个n,然后选择两个正整数a和b,满足a+b=n并且LCM(a,b)是最小的。

解题思路:已知:a+b=n

        LCM(a,b)=a*b/gcd(a,b)

        要让LCM尽可能小,gcd(a,b)就要尽可能大

        可以设b=k*a  (k>0)

        gcd(a,b)=a;

        我们可以得到LCM(a,b)=k*a

        所以b=k*a

        然后讲b=k*a带入a+b=n中

        a=n/(k+1) (k>0&&k<n)

然后我们可以得到代码:

#include<cstdio>
#include<algorithm>
using namespace std;
int main(void)
{
    long long t,a,b,n;
    scanf("%lld",&t);
    while(t--)
    {
        scanf("%lld",&n);
        long long min=INF,i,j,k;
        if(n&1)
        {
        for(k=1;k<n;++k)
        {
            if(n%(k+1)==0)
            break;
        }
        printf("%lld %lld
",n/(k+1),n*k/(k+1));
        }
        else
        printf("%lld %lld
",n/2,n/2);
    }
    return 0;
}

你以为这就完了?,看下数据1e10,铁定TLE,果不其然:

 然后我们可以发现这个和素数的筛选有着类似的地方,当k>sqrt(n)的时候后面的数字已经被我们筛出过了,所以直接就跳到n-1的地方

所以我们可以得到下面的代码

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int main(void)
{
    int t,a,b,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int k;
        int fg=0;
        for(k=1;k*k<=n;++k)
        {
            if(n%(k+1)==0)
            {
            fg=1;break;
            }    
        }
        if(fg)
        printf("%d %d
",n/(k+1),n-n/(k+1));//这里也可用k*n/(k+1)表示b,但是请注意,int类型的可能会溢出
        else
        printf("%d %d
",1,n-1);
    }
    return 0;
}

溢出:

 AC:

 刚好前天我做了一道LCM的题目给大家分享一个链接:

http://www.fjutacm.com/Problem.jsp?pid=2473

题解链接:https://www.cnblogs.com/YHH520/p/13287839.html

这道题也蛮有意思的。

原文地址:https://www.cnblogs.com/Mangata/p/13287752.html