AtCoder Beginner Contest 161 The number of contestants is 9927 fast, see all questions 5 minutes after the start of the contest
AtCoder Beginner Contest 161 C Replacing Integer formula derivation
See https://blog.csdn.net/mrcrack/article/details/104454762 for the general catalog
Online evaluation address https://atcoder.jp/contests/abc161/tasks/abc161_c
Manual simulation example
7 4
1
|7-4|=3
|3-4|=1
|1-4|=3
|3-4|=1
2 6
2
|2-6|=4
|4-6|=2
|2-6|=4
|4-6|=2
Confirmed by the above simulation, the minimum value is found in the difference operation of n% k, k. First n% = k , then n <k
|n-k|=k-n,
|k-n-k|=n,
|n-k|=k-n,
|k-n-k|=n
Therefore, the answer only needs to find the minimum value in n, kn .
AC code is as follows
#include <stdio.h>
#define LL long long
int main(){
LL n,k;
scanf("%lld%lld",&n,&k);
n%=k;
if(n>k-n)printf("%lld\n",k-n);
else printf("%lld\n",n);
}