2021 Niu Guest Winter Holiday Algorithm Basic Training Camp 2 D. Niu Niu and Divide Blocks (Regulations)

D. Niu Niu and Divide Division
Title link: https://ac.nowcoder.com/acm/contest/9982/D

Title description:

Divide division, also known as number theory division. It is an important technique in number theory algorithms. You can see it in various continuous summation problems that require enumerated factors. Such as Du Jiaosi, Möbius reverse evolutionary simplified divisibility subsection summation and so on.
Dividing blocks is based on such a mathematical phenomenon: for any positive integer N, Insert picture description herethe size of the set is always strictly less than 2 √N, for example, when N=10, S={10,5,3,2,1}, which makes Insert picture description here
For the type of summation problem, as long as the S set is quickly enumerated, the problem can be solved within the complexity of √N.

The ⌊ ⌋ symbol is a round-down character, and ⌊x⌋ represents the largest positive integer not greater than x.

Niuniu asked a new question after learning the algorithm of dividing and dividing. For a given positive integer N,x, what is the largest Insert picture description heretime Insert picture description herein S (the number after de-duplication descending sorting)?

Enter a description:

Enter a positive integer T (1 ≤ T ≤ 10^6) in the first line, indicating the number of test cases, for each group of cases.
Two positive integers N,x in a row (1 ≤ x ≤ N ≤ 10^9 ).

Output description:

For each case, output a positive integer, which is Insert picture description herethe highest number in the set S in descending order.

Example 1:

Input
2
25 9
1000000000 1000000000
Output
8
63244
shows that
when n=25, S={25,12,8,6,5,4,3,2,1}⌊ 25/9⌋=2, 2 is in the S set It is the 8th in descending order, so 8 is output.

Problem-solving ideas:

Finding the law
When k is small, the answer is k. By looking for the law, we can find that the boundary of division is about √N. Taking 25 as an example, the value of 25/i is 25,12,8,6,5,4,3, 2, 1. Pair the largest number with the smallest number and find that their product is the closest to 25. Therefore, when x≤√N, each x corresponds to a different answer. Otherwise, depending on the difference between N/√N and ⌊ N/x⌋, the result can be obtained based on the symmetry.

code show as below:

#include <iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
typedef long long ll;
int main()
{
    
    
	int t;
	scanf("%d", &t);
	while(t--)
	{
    
    
		ll x,n;
		scanf("%lld %lld", &n,&x);
		ll cnt = floor(sqrt(1.0*n));
		ll k = n/(cnt+1);
		if(x <= cnt)
			printf("%lld\n",x);
		else
			printf("%lld\n",cnt+(k-n/x)+1);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45894701/article/details/113674847