UVA 11388 GCD LCM 思维题==

根本用不上lcm gcd什么的==

The GCD of two positive integers is the largest integer that divides both the integers without any remainder. The LCM of two positive integers is the smallest positive integer that is divisible by both the integers. A positive integer can be the GCD of many pairs of numbers. Similarly, it can be the LCM of many pairs of numbers. In this problem, you will be given two positive integers. You have to output a pair of numbers whose GCD is the first number and LCM is the second number.

 

Input

The first line of input will consist of a positive integer TT denotes the number of cases. Each of the next T lines will contain two positive integer, G and L.

 

Output

For each case of input, there will be one line of output. It will contain two positive integers a and ba ≤ b, which has a GCD of G and LCM ofL. In case there is more than one pair satisfying the condition, output the pair for which a is minimized. In case there is no such pair, output -1.

 

Constraints

-           T ≤ 100

-           Both and will be less than 231.

 

Sample Input

Output for Sample Input

2

1 2

3 4

1 2

-1

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long prime[100000],num_prime=0;
bool isnotprime[200000004];
//int a[100000],b[100000];
void init()
{
     memset(isnotprime,false,sizeof(isnotprime));
     long  maxn=200000000;
     for(long i=2;i<maxn;i++)
     {
          if(!isnotprime[i])
               prime[num_prime++]=i;
          for(long j=0;j<num_prime&&i*prime[j]<maxn;j++)
          {
               isnotprime[i*prime[j]]=true;
               if(!(i%prime[j])) break;
          }
     }
}
int gcd(int a,int b)
{
     return b?gcd(b,a%b):a;
}
int main()
{
    //freopen("cin.txt","r",stdin);
    int l,g,n,GCD,LCM,i0,j0;
    while(~scanf("%d",&n))
    {
         while(n--)
         {
              scanf("%d%d",&g,&l);
              int sum=0;
              if(l%g)
              {
                   printf("-1\n");
                   continue;
              }
              /*init();
             // memset(a,0,sizeof(a));
              //memset(b,0,sizeof(b));
              int mark=0;
              for(int i=0;i<num_prime&&prime[i]<=g;i++)
              {
                   int numa=0,numb=0;
                   if(g%prime[i]==0&&l%prime[i]==0)
                   {
                        while(g%prime[i]==0)
                         numa++,g/=prime[i];
                        while(l%prime[i]==0)
                         numb++,l/=prime[i];
                         if(numa>numb)
                         {
                              mark=1;
                              break;
                         }
                   }
                   else if(g%prime[i]==0&&l%prime[i])
                   {
                        mark=1;
                        break;
                   }
              }
              if(mark==1) printf("-1\n");*/
              else
              {
                   printf("%d %d\n",g,l);
              }
         }
    }
    return 0;
}

有用的没几行==
全部评论

相关推荐

昨天 12:35
吉首大学 Java
写不来代码的小黑:这不就是想白嫖方案吗
点赞 评论 收藏
分享
2024-12-18 12:05
华东师范大学 golang
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务