D. Buying Shovels (violence, tricks)

D. Buying Shovels

Question: Given a, b, ask in the interval [1, b], find the largest factor n of a, so that a/n is the smallest.

Idea: Violence! ! !
Because of the data range, I didn't dare to think about it.
Because factors always appear in pairs, one is greater than n \sqrt nn , One less than n \sqrt nn , So that we can only enumerate to n \sqrt nn Will suffice.

Code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
    
    
	ll t, a, b, mi, idx;
	scanf("%lld", &t);
	while(t--) {
    
    
		mi = 0, idx = 0;
		scanf("%lld%lld", &a, &b);
		if(a <= b) printf("1\n"); //如果b大与a,最大因子就是a了。
		else {
    
    
			for(ll i=1; i<sqrt(a)+1; i++) {
    
    
				if(a % i == 0) {
    
    //找到一个最大的因子。
					if(a/i <= b)	mi = max(mi, a/i);
					if(i <= b) 		mi = max(mi, i);
				}
			}
			printf("%lld\n", a/mi);//求答案。
		}
	}
	return 0;
}

Find a factor of x without enumerating to x, just to x \sqrt xx That's it.

Guess you like

Origin blog.csdn.net/weixin_45363113/article/details/106844874