zoukankan      html  css  js  c++  java
  • 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 (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
    3
    4
    6
    9
    
    output
    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)尽可能小。
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int t,x;
     4 bool prim(int x) {
     5     if(x==2)
     6         return true;
     7     if(x<=1)
     8         return false;
     9     for(int i=2; i*i<=x; i++)
    10         if(x%i==0)
    11             return false;
    12     return true;
    13 }
    14 int gcd(int x) {
    15     int ans=-1;
    16     for(int i=2; i<x; i++)
    17         if(x%i==0)
    18         {
    19             ans=i;
    20             break;
    21         }
    22     ans=x/ans;
    23     return ans;
    24 }
    25 int main() {
    26     scanf("%d",&t);
    27     while(t--) {
    28         scanf("%d",&x);
    29         if(x%2==0)
    30             printf("%d %d
    ",x/2,x/2);
    31         else {
    32             if(prim(x))
    33                 cout<<1<<" "<<x-1<<endl;
    34             else
    35                 printf("%d %d
    ",gcd(x),x-gcd(x));
    36         }
    37     }
    38     return 0;
    39 }
  • 相关阅读:
    SqlServer 数据库同步的两种方式 (发布、订阅),主从数据库之间的同步
    Haproxy+Heartbeat 高可用集群方案操作记录
    HAProxy+Keepalived高可用负载均衡
    Centos 6/7 忘记root密码处理方法
    Centos 7配置nginx反向代理负载均衡集群
    各种官网系统镜像文件
    Tomcat通过Redis实现session共享的完整部署记录
    ELK平台搭建及日志监控
    快速搭建ELK日志分析系统
    idea各种快捷键
  • 原文地址:https://www.cnblogs.com/sbwll/p/13370502.html
Copyright © 2011-2022 走看看