思路
简单的模拟,答案就是\(min\{(\lfloor\frac{d\times n}{u+d}\rfloor+1)\times(u+d)-d\times n\}\)
代码
#include <cstdio>
#include <algorithm>
#include <cstring>
#define int long long
using namespace std;
int n,m,ans=0x3f3f3f3f3f3f3f3fLL;
signed main(){
while(scanf("%lld %lld",&n,&m)==2){
ans=0x3f3f3f3f3f3f3f3fLL;
for(int i=1;i<=m;i++){
int u,d;
scanf("%lld %lld",&u,&d);
int t=(d*n)/(u+d);
t++;
ans=min(ans,(u+d)*t-d*n);
}
printf("%lld\n",ans);
}
return 0;
}