Div Times Mod

2036 단어 사유수 문제
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;            }        }     } }
 

좋은 웹페이지 즐겨찾기