• CodeForces 1372B


    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 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.

    题目大意:

    输入一个t表示有t组数据,对于每组数据输入一个n,求a+b==n且 lcm(a,b)尽可能的小。

    解题思路:

    要使lcm(a,b)尽可能小,我们要求a和b尽可能靠近,并且a能够被b整除,比如27 a=9,b=18。要求这样的a,b首先使两数都能被n整除,然后再尽可能靠近。可以利用一下求素数的思想,从2枚举到sqrt(n),一旦发现发现n%i==0就输出 n/i 和 n-n/i 。如果一直没有则输出 1 和 n-1 。从2开始枚举如果可以除尽则说明可以分成两份,两两相等,如果%3=0则说明可以分成3份,令a等于1份,b等于2份即可。AC代码:

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <cstring>
    #include <queue>
    #include <cmath>
    using namespace std;
    int main()
    {
    	int t,n;
    	cin>>t;
    	while(t--)
    	{
    		int n;
    		cin>>n;
            bool flag=true;
            for(int i=2;i<=floor(sqrt(n)+0.5);i++)
              if(n%i==0)
              {
                int a=n/i,b=n-a;
                cout<<a<<" "<<b<<endl;
                flag=false;
                break;
              }
            if(flag)
              cout<<1<<" "<<n-1<<endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    require.js使用
    favico是针对网页图标内容更改
    web图片转换小工具制作
    控制显示input隐藏和查看密码
    程序员图片注释字符串制作工具
    c语言基础, , ,
    【理解】column must appear in the GROUP BY clause or be used in an aggregate function
    ps aux命令解析
    while(std::cin>>val)怎么结束的思考
    【转】NativeScript的工作原理:用JavaScript调用原生API实现跨平台
  • 原文地址:https://www.cnblogs.com/Hayasaka/p/14294219.html
Copyright © 2020-2023  润新知