Ultraviolet A 11388 GCD lcm (number theory)

Source: Internet
Author: User

 

 

Ultraviolet A 11388 GCD lcm (number theory)

Question: Check whether there is a, B so that lcm (a, B) = L, gcd (a, B) = g, there is no output-1, there is output A, B, and a is as small as possible

Analysis: Forced violence is impossible. The data is huge. LlU is used. There are two ideas here.

Idea 1: a * B = g * l

If a is a multiple of a = g, enumerate
Then judge whether g * l can divide a, and finally judge whether B is a multiple of.
A from G to SQRT (G * l)

// Enter two integers, G, l // locate A and B so that gcd (a, B) = g lcm (A, B) = L // requires a to be as small as possible // train of thought:/* from question a * B = g * l enumerative A and B TLE enumerative, then, when determining whether B is to ensure that A is an appointment of G, judge whether g * l % A is zero, and then judge whether B % G is zero, prove it directly .... * // # Define bug # include <cstdio> # include <cstring> # include <algorithm> # include <map> # include <vector> # include <list> # include <queue> # include <ctime> # include <iostream> # include <cmath> # include <set> # include <string> using namespace STD; typedef long ll; typedef unsigned long ull; # define INF 0x7fffffff # define Max 0x3f3f3fvoid orz () {int t; ull T, G, L, A, B; scanf ("% d", & T); While (t --) {scanf ("% LlU", & G, & L); t = g * l; int flag = 0; for (a = g; A * A <= T; A + = g) {If (T % A = 0) {B = t/; if (B % G = 0) {flag = 1; break ;}} if (FLAG) printf ("% LlU \ n", a, B ); else printf ("-1 \ n") ;}} int main () {orz (); Return 0 ;}
Code Junyi

Train of Thought 2: directly prove that when l * g = a * B exists, a = G, B = L

If B %! = 0, there is no solution (not proof)
If B % A = 0, the solution exists, and the solution is a = G, B = L;
It can be thought that a should be as small as possible, then the minimum can only be the maximum public approx. g of two numbers
The larger the value of B is, and the maximum value is the least public multiple of two numbers.
A = G, B = L;


#include <cstdio>typedef unsigned long long ULL;void Orz(){    int T;    ULL G, L;    scanf("%d",&T);    while(T--)    {        scanf("%llu %llu",&G,&L);        if(L % G)            puts("-1");        else            printf("%llu %llu\n",G,L);    } }int main(){    Orz();    return 0;}
Code jun'er

 



 

Ultraviolet A 11388 GCD lcm (number theory)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.