📌  相关文章
📜  计算对数(A <= N,B <= N)以使gcd(A,B)为B

📅  最后修改于: 2021-04-25 01:07:31             🧑  作者: Mango

给定数字n。我们需要找到a和b的有序对的数量,例如gcd(a,b)是b本身
例子 :

Input : n = 2
Output : 3
(1, 1) (2, 2) and (2, 1) 

Input : n = 3
Output : 5
(1, 1) (2, 2) (3, 3) (2, 1) and (3, 1)

天真的方法: gcd(a,b)= b意味着b是a的因数。因此,对的总数将等于每个a = 1至n的除数之和。请参阅查找自然数的所有除数以进行实施。
有效方法: gcd(a,b)= b表示a是b的倍数。因此,对的总数将是小于或等于n的每个b的倍数(b从1到n的变化)的总和。
对于数字i,i的倍数小于或等于floor(n / i)。因此,我们需要做的就是将每个i = 1到n的floor(n / i)相加并打印出来。但是可以进行更多优化。对于i> = sqrt(n),floor(n / i)最多可具有2 * sqrt(n)值。 floor(n / i)的范围可以从1到sqrt(n),并且类似地,对于i = 1到sqrt(n),floor(n / i)的值可以从1到sqrt(n)。因此共有2 * sqrt(n)个不同的值

let floor(n/i) = k
k <= n/i < k + 1
n/k+1 < i <= n/k
floor(n/k+1) < i <= floor(n/k)
Thus for given k the largest value of i for 
which the floor(n/i) = k is floor(n/k)
and all the set of i for which the 
floor(n/i) = k are consecutive
CPP
// C++ implementation of counting pairs
// such that gcd (a, b) = b
#include 
using namespace std;
 
// returns number of valid pairs
int CountPairs(int n)
{
    // initialize k
    int k = n;
 
    // loop till imin <= n
    int imin = 1;
 
    // Initialize result
    int ans = 0;
 
    while (imin <= n) {
 
        // max i with given k floor(n/k)
        int imax = n / k;
 
        // adding k*(number of i with
        // floor(n/i) = k to ans
        ans += k * (imax - imin + 1);
 
        // set imin = imax + 1 and k = n/imin
        imin = imax + 1;
        k = n / imin;
    }
 
    return ans;
}
 
// Driver function
int main()
{
    cout << CountPairs(1) << endl;
    cout << CountPairs(2) << endl;
    cout << CountPairs(3) << endl;
    return 0;
}


Java
// Java implementation of counting pairs
// such that gcd (a, b) = b
class GFG {
     
    // returns number of valid pairs
    static int CountPairs(int n) {
         
        // initialize k
        int k = n;
     
        // loop till imin <= n
        int imin = 1;
     
        // Initialize result
        int ans = 0;
     
        while (imin <= n) {
     
            // max i with given k floor(n/k)
            int imax = n / k;
         
            // adding k*(number of i with
            // floor(n/i) = k to ans
            ans += k * (imax - imin + 1);
         
            // set imin = imax + 1
            // and k = n/imin
            imin = imax + 1;
            k = n / imin;
        }
     
        return ans;
    }
     
    // Driver code
    public static void main(String[] args) {
        System.out.println(CountPairs(1));
        System.out.println(CountPairs(2));
        System.out.println(CountPairs(3));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python implementation of counting
# pairs such that gcd (a, b) = b
 
# returns number of valid pairs
def CountPairs(n):
     
    # initialize k
    k = n
 
    # loop till imin <= n
    imin = 1
 
    # Initialize result
    ans = 0
 
    while(imin <= n):
 
        # max i with given k floor(n / k)
        imax = n / k
 
        # adding k*(number of i with
        # floor(n / i) = k to ans
        ans += k * (imax - imin + 1)
 
        # set imin = imax + 1 and
        # k = n / imin
        imin = imax + 1
        k = n / imin
 
    return ans
     
# Driver code
print(CountPairs(1))
print(CountPairs(2))
print(CountPairs(3))
 
# This code is contributed by Anant Agarwal.


C#
// C# implementation of counting
// pairs such that gcd (a, b) = b
using System;
 
class GFG {
     
    // returns number of valid pairs
    static int CountPairs(int n)
    {
         
        // initialize k
        int k = n;
     
        // loop till imin <= n
        int imin = 1;
     
        // Initialize result
        int ans = 0;
     
        while (imin <= n) {
     
            // max i with given
            // k floor(n / k)
            int imax = n / k;
         
            // adding k * (number of i 
            // with floor(n / i) = k
            // to ans
            ans += k * (imax - imin + 1);
         
            // set imin = imax + 1
            // and k = n / imin
            imin = imax + 1;
            k = n / imin;
        }
     
        return ans;
    }
     
    // Driver code
    public static void Main(String []args)
    {
        Console.WriteLine(CountPairs(1));
        Console.WriteLine(CountPairs(2));
        Console.WriteLine(CountPairs(3));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

1
3
5