问题 D: JP Partition
题目描述
You are given integers N and M.
Consider a sequence a of length N consisting of positive integers such that a1+a2+…+aN = M. Find the maximum possible value of the greatest common divisor of a1,a2,…,aN.
Constraints
·All values in input are integers.
·1≤N≤105
·N≤M≤109
输入
Input is given from Standard Input in the following format:
N M
输出
Print the maximum possible value of the greatest common divisor of a sequence a1,a2,…,aN that satisfies the condition.
样例输入
3 14
样例输出
2
提示
Consider the sequence (a1,a2,a3)=(2,4,8). Their greatest common divisor is 2, and this is the maximum value.
解题思路:
AC代码:
#include <iostream>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
int ans=-1000;
if(m%n==0)
ans=m/n;
else
{
for(int i=1;i<=m/n;i++)
{
if(m%i==0)
{
ans=max(ans,i);
}
}
}
cout<<ans;
return 0;
}