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

题目链接

思路:偶数直接除2就是他的lcm的最小情况,奇数时求暴力求他因子计算lcm的最小情况

#include 
#include 
#include 
#include 
#include
#include
#include
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false)
#define space putchar(' ')
#define enter putchar('\n')
#define lson root<<1
#define rson root<<1|1
typedef pair<int,int> PII;
const int mod=998244353;
const int N=2e6+10;
const int inf=0x7f7f7f7f;


ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
    return a*(b/gcd(a,b));
}

template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-')
            op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op)
        x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0)
        x = -x, putchar('-');
    if(x >= 10)
        write(x / 10);
    putchar('0' + x % 10);
}


int main()
{
   // SIS;

   int t;

  read(t);
   while(t--)
   {
      ll n;
      read(n);
      if(n&1)
      {
          ll x=sqrt(n);
          ll a=1,b=1;
          ll ans=inf;
          ll l=1,r=1;
         for(ll i=2;i<=x;i++)
         {
              a=n/i;
              b=i;
           
             if(n%i==0)
             {
                 
                 ll tm=lcm(a,n-a);
                if(tm<ans)
                {
                    ans=tm;
                    l=a;r=n-a;
                }
                tm=lcm(b,n-b);
                if(tm<ans)
                {
                    ans=tm;
                    l=b;r=n-b;
                }
                
             }
          
         }
        
         if(n-1>lcm(l,r)&&(l!=1&&r!=1))
         printf("%lld %lld\n",l,r);
         else
         printf("1 %lld\n",n-1);
      }
      else
      {
         printf("%lld %lld\n",n/2,n/2);
      }
   }


    return 0;
}


你可能感兴趣的:(CF,数学)