A Commentary Boxes
算出$N \pmod M$
然后分别讨论是加还是减
#include<cstdio> #include<algorithm> #include<map> #include<vector> #define int long long using namespace std; inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int N, M, A, B; main() { N = read(); M = read(); A = read(), B = read(); int res = N % M; printf("%lld", min((M - res) * A, res * B)); }