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<b0≤c<b , and a−ca−c is divisible by bb .

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 且<k,又因为n不可能为0,所以x%k是大于0的.所以在1~(k-1)之间枚举k.

再设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<bits/stdc++.h>
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;
           }
       }
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值