[Programming question] Number pairs

【topic】

Niu Niu got a positive integer pair (x, y) from the teacher before, Niu Niu forgot how many they were.

But Niu Niu remembers that the teacher told him that neither x nor y is greater than n, and the remainder of dividing x by y is greater than or equal to k.

Niu Niu wants you to help him calculate how many possible pairs there are.

Enter description:

The input consists of two positive integers n,k (1 <= n <= 10^5, 0 <= k <= n - 1).

Output description:

For each test case, output a positive integer representing the number of possible pairs.

Example 1

enter

5 2

output

7

illustrate

The number pairs that satisfy the condition are (2,3),(2,4),(2,5),(3,4),(3,5),(4,5),(5,3)

【solve】

① https://www.nowcoder.com/questionTerminal/bac5a2372e204b2ab04cc437db76dc4f

x can be taken on [1, n], but y can only be taken on [k, n] , because there is no remainder greater than or equal to k below k.

So traverse y, for each y, count the number of matching x and add it to count.

Assuming that x can take values ​​from [0, n], then this interval can be divided into at least (n/k) complete intervals of length y .

x = 【0,1……y-2,y-1】【y,y+1,……,2y-2,2y-1】……【……】……【……,n】

In each small interval a, the remainder of the ith number a[i]%y is i . In this way , there are yk x greater than or equal to k in each small segment (obviously, when k=0, the number of y satisfies the meaning of the question) .

【0,1,……,k,k+1,……,y-1

In this way , the total number of traversed is (n/y)*y, and the total number of x where the condition is satisfied is (n/y)*(yk)

因为n = (n/y)*y + n%y

So there are n+1-(n/y)*y = n+1 - (n - n%y) = 1+n%y numbers that have not been traversed, let it be t.

Since n%y∈[0, y-1], then t∈[1, y].

That is to say, in my method, at least one number is left, and at most an entire interval (the number is y) is left .

But in any case, the ith number last[i]%y of this interval last must be i. Then the remainder of the last number (n) is n%y.

As a result, the interval from [k,n%y] contains a total of n%y-k+1 numbers. However, if you calculate a number less than 0, you don't need to subtract it back, just treat it as nothing.

So the last interval contains max( n %y-k+1, 0 ) x that satisfy the condition.

Finally, note that all the numbers of x∈ [0, n] are actually enumerated here. When k==0, an extra 0 is counted and must be subtracted .

// Assuming n=10, k=3, it can only be 4, 5, 6, 7, 8, 9, 10 for y
// When y=4, (n/y)*(yk) represents x When less than or equal to 8 (8 is an integer multiple of 4), there are (3,4), (7,4), Math.max(0,n%y-k+1) represents the logarithm that meets the meaning of the question when x is greater than 8 0
// When y=5, (n/y)*(yk) means x is less than or equal to 10 (10 is an integer multiple of 5), there are (3,5),(4,5),(8,5) ,(9,5), Math.max(0,n%y-k+1) means that when x is greater than 10, the logarithm that meets the meaning of the question is 0
// When y=6,(n/y)*(yk) Represents (3,6), (4,6), (5,6) when x is less than or equal to 6, Math.max(0,n%y-k+1) represents the logarithm that meets the meaning of the question when x is greater than 6 is 2, respectively (9,6), (10,6)
// When y=7, (n/y)*(yk) means x is less than or equal to 7, there are (3,7), (4,7) ,(5,7),(6,7),Math.max(0,n%y-k+1) represents that when x is greater than 7, the logarithm that meets the meaning of the question is 1, which is (10,7)
// . ..and so on

import java.util. *;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextInt();
        long k = sc.nextInt();
        long res = 0;
        if (k == 0){
           res = n * n;//The remainder of any two logarithms is greater than or equal to 0
        }else {
            for (long y = k + 1;y <= n;y++){
                res + = (n / y) * (y - k) + Math.max(n % y - k + 1,0);
            }

        }
        System.out.println(res);
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325109862&siteId=291194637