Niukeshu’s detailed explanation of the questions

Insert image description here

 Due to the time limit for this question, it is not possible to pass it using violent solutions, and the ideas cannot be observed, so the pass rate is less than 15%. Today I will show you how to solve this question in detail. I hope it can help. Everyone. (personal thoughts)

  • Topic description:

Insert image description here
Enter two positive integers. The first positive integer n is the range of this positive integer pair, and the second number is the remainder of the two numbers in the pair. As long as a number in this range is the first The remainder of the number is greater than or equal to k.
Example 1 is as follows
Insert image description here
It can be seen that in order to get all the number pairs, they can be divided into two parts. The x in one part is less than y, and the remainder is greater than Just equal to the provided k.

  • Idea:

First analyze the first part to find the pattern.
Insert image description here
It can be easily discovered by observing the rules. The first part is the summation of the decreasing sequence. Just use the arithmetic sequence summation formula to calculate how many number pairs there are in the part where x is greater than y.
Input n equals 5, k equals 2, and the formula for summing an arithmetic sequence is the last term minus the first term multiplied by the number of terms divided by 2.
Insert image description here
The troublesome thing about this question is that it is difficult to determine the number of partial pairs in the latter part. Of course, we can’t tell anything using Example 1. We can give a more comprehensive example.
Assume n=10,k=2. List all pairs of numbers that meet the conditions and observe the pattern. As follows
Insert image description here
Observe and observe, it feels very chaotic and has no rules. In fact, you can find their rules by changing the arrangement.
Insert image description here
What is the pattern?
The rules are as follows:
Insert image description here
You can first find out what is divisible. For example, if you divide by 3 in the first line, the remainder is 2. After the division, you can multiply the result by y-k. , and then find the remainder, for example, in the second line, (10-4) + 1 divided by 4, the remainder is 3, 3 is greater than k, so the remainder -k must be added, which is the part included in the remainder.
Before looking at the code, you can type it yourself based on your ideas. This question has many details and tests your logical ability.
Code implementation (many details are commented)

int main() {
    
    
    int a = 0, b = 0;
    unsigned long long count = 0;
    scanf("%d %d", &a, &b);
    for (int i = b + 1; i <= a; i++)
    {
    
    
        unsigned long long num = (a - i) / i;
        count += num * (i - b);//可以整除的部分
        if(b>0)//这里有第二个数为0的情况
        {
    
    
            if (((a - i) % i + 1) > b)
            count += (a-i) % i - b+1;//余数部分
        }
        else {
    
    
        count+=a%i;//如果为零,直接加上余数即可。
        }
    }
    count += (unsigned long long)(a - b + 1) * (a - b) / 2;//由于a和b都是int类型,要强转long long,不然部分用例通不过。
    printf("%lld", count);
    return 0;
}

After running it, you can pass it successfully.
Insert image description here

I hope you all gained something.

Guess you like

Origin blog.csdn.net/qq_75270497/article/details/134843336