题目来源
题干
Polycarp wants to buy exactly nn shovels. The shop sells packages with shovels. The store has k types of packages: the package of the i-th type consists of exactly i shovels (1≤i≤k). The store has an infinite number of packages of each type.
Polycarp wants to choose one type of packages and then buy several (one or more) packages of this type. What is the smallest number of packages Polycarp will have to buy to get exactly n shovels?
For example, if n=8 and k=7, then Polycarp will buy 2 packages of 4 shovels.
Help Polycarp find the minimum number of packages that he needs to buy, given that he:
- will buy exactly n shovels in total;
- the sizes of all packages he will buy are all the same and the number of shovels in each package is an integer from 1 to k, inclusive.
Input
The first line contains an integer t (1≤t≤100) — the number of test cases in the input. Then, t test cases follow, one per line.
Each test case consists of two positive integers n (1≤n≤10^9) and kk (1≤k≤10^9) — the number of shovels and the number of types of packages.
Output
Print t answers to the test cases. Each answer is a positive integer — the minimum number of packages.
Example
input
5
8 7
8 1
6 10
999999733 999999732
999999733 999999733
output
2
8
1
999999733
1
Note
The answer to the first test case was explained in the statement.
In the second test case, there is only one way to buy 8 shovels — 8 packages of one shovel.
In the third test case, you need to buy a 1 package of 6 shovels.
题目大意
已知整数A和B,如果存在C使其满足式子C|A∧C≤B
(A被C整除并且C小于等于B),记A÷C的结果为D,找满足式子最小的D。
解题思路
通过枚举因子找到符合题意的值。
实现
#include <iostream>
#include <cmath>
#define INF 1<<30
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, k, rst = INF; // rst用来记录“A÷C的结果D”的最小值
cin >> n >> k;
for (int i = 1; i * i <= n; i++) { // 枚举因子时一次能找到一对,i的枚举范围可以缩小到 i * i <= n
if (n % i == 0) { // 如果n被i整除
int a = i, b = n / i; // i与n/i是n的一对因子
if (a <= k)rst = min(rst, b); // 更新最小值rst
if (b <= k)rst = min(rst, a); // 更新最小值rst
}
}
cout << rst << endl;
}
return 0;
}