Find all positive integer pairs so that they greatest common divisor to n and least common multiple to M

Source: Internet
Author: User
Tags greatest common divisor

The topic is like this: Click to open the link


The main idea is to find all positive integer pairs so that they greatest common divisor to n and least common multiple to M. (1 <= N, M <= 10^10)

The problem can be converted to: set A, B is that integer pair, N, a, B., M, these 4 numbers can be divisible by n, can be divided by N, the title is converted to find out greatest common divisor 1, least common multiple for the logarithm of m/n.

That is to find the logarithm of the product in 1 to m/n and the m/n and coprime. can be resolved within O (sqrt (m/n)).


#include <algorithm> #include <cstdio> #include <cmath> #include <cstring> #include <cstdlib > #include <iostream> #define MAX 0x3f3f3f3f#define N 2000005typedef Long Long ll;using namespace Std;int T; LL N, M; ll GCD (ll A, ll b) {    return b = = 0? a:gcd (b, a% b);} int main () {    cin>>t;while (t--) {        cin >> n >> m;        if (m% n) {            printf ("0\n");            Continue;        }        LL x = m/n;        int ans = 0;        for (ll i = 1; I <= (ll) sqrt (x); i++) {            if (x% i = = 0) {                ll j = x/i;                if (GCD (i, j) = = 1) ans++;}        }        printf ("%d\n", ans);    }    return 0;}




Find all positive integer pairs so that they greatest common divisor to n and least common multiple to M

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.