题意:
求(x div k) * (x mod k) = n的最小解x,保证有解
1<=n<=1e6, k<=1000,1s
思路:
注意到k的范围是1e3,
1<=x mod k<1e3,这并不能看到x的上限
而x div k要达到1e6,所以x最大可能达到1e9
所以不能枚举x
因为两个因子相乘正好为n,所以我们可以枚举x mod k
x mod k<=n且x mod k < k
所以我们只需枚举[1,k)就可以了
此时x mod k = i ,且n%i==0
所以x div k = n / i
所以x = n / i * k + i;
算出最小值即可
代码:
枚举范围错了居然也能pp。。还好没掉分
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<stack> #include<queue> #include<deque> #include<set> #include<vector> #include<map> #include<functional> #define fst first #define sc second #define pb push_back #define mem(a,b) memset(a,b,sizeof(a)) #define lson l,mid,root<<1 #define rson mid+1,r,root<<1|1 #define lc root<<1 #define rc root<<1|1 #define lowbit(x) ((x)&(-x)) using namespace std; typedef double db; typedef long double ldb; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> PI; typedef pair<ll,ll> PLL; const db eps = 1e-6; const int mod = 1e9+7; const int maxn = 2e6+100; const int maxm = 2e6+100; const int inf = 0x3f3f3f3f; const db pi = acos(-1.0); int main(){ ll n, k; scanf("%lld %lld",&n, &k); //(x/k)*(x%k)==n ll ans = 0x3f3f3f3f3f3f3f3f; for(ll i = 1; i < k; i++){ if(n%i==0){ //x/k=n/i //x%k=i ll y= n/i; ans = min(y*k+i,ans); } } printf("%lld", ans); return 0; } /* */