Div Times Mod

                                                                         Div Times Mod

Vasya likes to solve equations. Today he wants to solve (x div k)⋅(xmodk)=n(x div k)⋅(xmodk)=n , where divdiv and modmod stand for integer division and modulo operations (refer to the Notes below for exact definition). In this equation, kk and nn are positive integer parameters, and xx is a positive integer unknown. If there are several solutions, Vasya wants to find the smallest possible xx . Can you help him?

Input

The first line contains two integers nn and kk (1≤n≤1061≤n≤106 , 2≤k≤10002≤k≤1000 ).

Output

Print a single integer xx  — the smallest positive integer solution to (x div k)⋅(xmodk)=n(x div k)⋅(xmodk)=n . It is guaranteed that this equation has at least one positive integer solution.

Examples

Input

6 3

Output

11

Input

1 2

Output

3

Input

4 6

Output

10

Note

The result of integer division a div ba div b is equal to the largest integer cc such that b⋅c≤ab⋅c≤a . aa modulo bb (shortened amodbamodb ) is the only integer cc such that 0≤c

In the first sample, 11 div 3=311 div 3=3 and 11mod3=211mod3=2 . Since 3⋅2=63⋅2=6 , then x=11x=11 is a solution to (x div 3)⋅(xmod3)=6(x div 3)⋅(xmod3)=6 . One can see that 1919 is the only other positive integer solution, hence 1111 is the smallest one.

思路:

找一个最小的x,使得(x/k)*(x%k)==n,x%k的值一定>=0 且

再设x%k=i,上式可以变成(x-i)/k * i =n,所以x=n/i * k +i.

详解:

设x%k=i;

变为正常数学形式即为x/k=y.....i

y=(x-i)/k 同时 y=x/k 即y=n/i;

x=y*k+i

 =n/i*k+i;

代码附上

#include
using namespace std;
typedef long long ll;
ll i,j,k;
int main ()
{
    int n;
    while(cin >> n >>k)
    {
       for(i=k-1; i>0; i--)
       {
           if(n%i==0)
           {
               cout<< i+n/i*k<< endl;
               break;
           }
       }
    }
}

 

 

你可能感兴趣的:(思维水题)