Hankson's Interesting questions

Source: Internet
Author: User
Tags gcd greatest common divisor

Dr. Hanks is a well-known expert in the field of BT (Bio-tech, Biotechnology) whose son is named Hankson. Now, Hankson, who just came home from school, is thinking about an interesting question.

Today in class, the teacher explained how to ask for two positive integers c1 and C2 greatest common divisor and least common multiple. Now that Hankson thinks he has mastered this knowledge, he begins to think of a "inverse problem" of a "number of conventions" and "common multiple", the question of which is: a known positive integer a0,a1,b0,b1, an unknown positive integer x satisfies:

The greatest common divisor of 1.x and A0 is A1;
The least common multiple of 2.x and B0 is B1.
The "Inverse problem" of Hankson is to find the positive integer x that satisfies the condition. But after a little deliberation, he found that such an x is not unique and may not even exist. So instead he began to think about how to solve the number of x that satisfies the condition. Please help him to solve the problem by programming.

First, turn the conditions into mathematical formulas.

A1=GCD (X,A0);

B1=LCM (X,B0);

Obviously you'll find GCD is better than LCM, so let's convert the second one.

First you need to know a theorem a*b=gcd (a, B) *LCM (A, b);

So the second Formula transformation process

X*B0=LCM (x,b0) *GCD (x,b0);

X*B0=B1*GCD (X,B0);

X=B1/B0*GCD (X,B0);

Tidy up.

A1=GCD (X,A0);

X=B1/B0*GCD (X,B0);

First of all, it's very violent to think of enumeration x, so we can only get 50 points, so we need to optimize

Because GCD (x,b0) <=b0 and GCD (x,b0) are approximate b0, we enumerate gcd (x,b0) and gcd (x,b0) <=sqrt (B0);

Make I=GCD (x,b0) 1<=i<=sqrt (B0);

Determine if X=b1/b0*i and x=b1/b0* (b0/i) meet the conditions

And some special sentences, see the code.

#include <algorithm>#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<cmath>#include<queue>#definell Long Longusing namespacestd;Const intmaxn=1000000+10101; inlineintRead () {intx=0, f=1;CharCh=GetChar ();  for(;! IsDigit (CH); Ch=getchar ())if(ch=='-') f=-1;  for(; isdigit (ch); Ch=getchar ()) x= (x<<3) + (x<<1) +ch-'0'; returnx*F;}intn,a0,a1,b0,b1;intgcdintXinty) {    if(x<y) Swap (x, y); if(y==0)returnx; returnGCD (y,x%y); }intMain () {n=read ();  for(intI=1; i<=n;i++){        intans=0; A0=read (); A1=read (); B0=read (); b1=read (); intkk=sqrt (B0);  for(intI=1; i<=kk;i++){            if(b0%i!=0)Continue;//Note 1            intx1=b1/b0*i; if(GCD (x1,a0) ==a1 && gcd (x1,b0) ==i) ans++; if(b0/i==i)Continue;//NOTE 2x1=b1/b0* (b0/i); if(GCD (x1,a0) ==a1 && gcd (x1,b0) ==b0/i) ans++; } printf ("%d\n", ans); }    return 0;}

Hankson's Interesting questions

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.