概述
题目描述
Polycarp wants to buy exactly n 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≤109) and k (1≤k≤109) — 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.
题目大意
你现在要买n个铁锹,商店中有k中不同的卖法,依次每一次卖1到k个铁锹,现在你只能选择其中的一种买法,问最少买几次同一种的买法,使得刚好买到n个铁锹。
题目分析
这道题其实就是求约数。
因为有1-k种买法,每种买法买1-k把铁锹,而且只能选一种买法。因此,我们就要找到小于等于k的n的最大约数,所以我们可以先求出n的所有约数,再从大到小遍历所有的约数,找到小于等于k的最大约数即可。
答案即为:n/a[k]
。
代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <map>
#include <unordered_map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
int const N=1e6+5;
int a[N],cnt=0;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,k;
scanf("%d%d",&n,&k);
if(k>=n) {puts("1"); continue;} //k>=n,可直接输出n
cnt=0;
for(int i=1;i<=n/i;i++) //求n的约数
if(n%i==0)
{
a[cnt++]=i;
if(i!=n/i) a[cnt++]=n/i;
}
sort(a,a+cnt); //将约数进行排序
int u;
for(int i=cnt-1;i>=0;i--) //找到小于等于k的最大约数
if(k>=a[i]) {u=i; break;}
printf("%dn",n/a[u]);
}
return 0;
}
最后
以上就是单纯万宝路为你收集整理的#644 (Div. 3) D. Buying Shovels(约数)的全部内容,希望文章能够帮你解决#644 (Div. 3) D. Buying Shovels(约数)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复