問題描述:
Vasya likes to solve equations. Today he wants to solve (x div k)?(xmodk)=n, where div and mod stand for integer division and modulo operations (refer to the Notes below for exact definition). In this equation, k and n are positive integer parameters, and x is a positive integer unknown. If there are several solutions, Vasya wants to find the smallest possible x. Can you help him?
(其實就是已知n,k,(x/k)*(x%k)=n雕凹,求x的值)
輸入內(nèi)容:
(The first line contains two integers n and k (1≤n≤, 2≤k≤1000))
輸出內(nèi)容:x
樣例:
http://codeforces.com/contest/1085/problem/B
由題可知:n不為0仪际,所以x肯定大于k
第一思路:由k+1開始遍歷x的值,但失敗了,因為當n的值很大時,x也很大,要遍歷很多次景馁,最終導(dǎo)致超時;
第二思路:將x%k當作新的值i陵且,這樣最多只需遍歷k-1次數(shù)字就可以找到i,而,所以,代碼如下:
所以說裁僧,很多次遇到帶有余數(shù)的場景時个束,一定要對它多加利用,因為題目往往喜歡設(shè)置很大的數(shù)字聊疲,此時不用余數(shù)計算來遍歷就特別容易超時茬底。。