Problem:


Score : 300 points

Problem Statement

Given any integer x, Aoki can do the operation below.

Operation: Replace x with the absolute difference of x and K.

You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

Constraints

0 ≤ N ≤ 10^{18}

1 ≤ K ≤ 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible value taken by N after Aoki does the operation zero or more times.

Sample Input 1

7 4

Sample Output 1

1

Initially, N=7.

After one operation, N becomes |7-4| = 3.

After two operations, N becomes |3-4| = 1, which is the minimum value taken by N.

Sample Input 2

2 6

Sample Output 2

2

N=2 after zero operations is the minimum.

Sample Input 3

1000000000000000000 1

Sample Output 3

0



Sample input:
7 4


Sample output:
1

Submission status: Runtime Error

Solution code:
def main():
  	n, k = map(int, input().split())
    n %= k
    
    if abs(n - k) < n:
      print(abs(n-k))
    else:
      print(n)

Submission status: Runtime Error

Solution code:
import math
n,k=map(int,input().split())

if n == 1 or k == 1:
    print(0)
else:
    print(math.gcd(n,k))

Submission status: Runtime Error

Solution code:
#include <stdio.h>
#include <stdlib.h>

int main(void){
    int n, k;
    scanf("%d %d", &n, &k);
    if (k == 1){
        printf("0\n");
    }else{
        while ((double)n >= (double)k / 2){
            n = abs(k - n);
        }
        printf("%d\n", n);
    }
    return 0;
}

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())

if K != 1:
    ans = N%(abs(N-K))
else:
    ans = 0
print(ans)

Submission status: Runtime Error

Solution code:
import math

n, k = map(int, input().split())

if n == 1 or k == 1:
	print(1)
else:
	print(math.gcd(n, k))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
if 2*n<=k:
    num1=k//n
    n=k-n*(num1-1)
    num = int(n / k)
    ans = min(abs(n - num * k), abs(n - (num + 1) * k))
    print(ans)
elif n<=k<2*n:
    print(min(k-n,k-2*n))
else:
    num=int(n/k)
    ans=min(n-num*k,abs(n-(num+1)*k))
    print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
tmp = N


if K > N:
    print(N)
    exit()

a = abs(N - K)
ans = N % a

if ans == 1:
    print(0)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(a) (a).begin(), (a).end()
#define sort(a) sort(all(a))
using P = pair<int, int>;
using ll = long long;
using ld = long double;
typedef vector<int> vi;
int dx[4] = { 0, 1, 0,-1 };
int dy[4] = { 1, 0,-1, 0 };
const ll MOD = 1000000007;

signed main() {
  int N,K;
  cin >> N >> K;
  int ans;
  if (N==K) {
    cout << 0 << endl;
    return 0;
  }
  N %= K;
  ans = min(N, abs(N-K));
  cout << ans << endl;
  return 0;
}

Submission status: Runtime Error

Solution code:
import math
n, k = map(int, input().split())

if k == 1:
    print(0)
    exit()

gcd = math.gcd(n, k)
print(gcd)

Submission status: Runtime Error

Solution code:
#Input
n, k = map(int, input().split())

#Process
mod = n % k
if(mod > abs(mod - k)):
    Ans = aqbs(mod - k)
else:
    Ans = mod

#Output
print(Ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
if k%n == 0 or n%k == 0:
    n=0
while 1==1:
    if abs(n-k) < n:
        n=abs(n-k)
    elif abs(k-n) < n:
        n=abs(k-n)
    else:
        break
print(n)

Submission status: Runtime Error

Solution code:
N, K =map(int,input().split())
r = N % K
print(min(r,abs(r-k)))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
ans = n
done = [n]

if k == 1 or n == k:
    print(0)
elif n > k:
    r = n % k
    print(k-r)
elif n < k:
    print(k%n)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
if n == 0:
    print(0)
else:
    x = abs(n - k)
    a = n % x
    b = abs(a - k)
    print(min(a, b))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**9)

def func(a,b):
    if a <= b:
        return a
    else:
        return func(b,abs(b-K))

N,K=map(int, input().split())
print(func(N,abs(N-K)))

Submission status: Runtime Error

Solution code:
N,K=map(int, input().split())
m=int(N/K)

min_0 = N
min_1 = abs(N-m*K)
min_2 = abs(min_1-K)

print(min(min_0, min_1, min_2))N,K=map(int, input().split())
m=int(N/K)

min_0 = N
min_1 = abs(N-m*K)
min_2 = abs(min_1-K)
m=int(min_2/K)
min_3 = abs(min_2-m*K)

print(min(min_0, min_1, min_2, min_3))

Submission status: Runtime Error

Solution code:
n,k= map(int,input().split())
if n < k:
    print(n % k)
else:

    print(k%(abs(n) % abs(k)))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
ans = N
if N < K:
    q, mod = divmod(K, N)
    ans = min(N, K - N)
elif N == K:
    ans = 0
else:
    q, mod = divmod(N, K)
    ans = min(mod, abs(K - mod))
print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())
from fractions import gcd

if n % k== 0:
  print 0
else:
  print gcd(n,k)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
if(n>k):
    if(n%k==0):
        print(0)
    else:
        print(min((n%k),(n//k+1)*k-n))
elif(n==k):
    print(0)
else:
    if(k%n==0):
        print(n)
    else:
        if(k-n)<n:
            print(k-n)
        else:
            print(n)

Submission status: Runtime Error

Solution code:

n,k = map(int,input().split())

d = abs(n-k)

print(n % d)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
print(min(n%k,k-n%k) if n >= k else min(n,n%k,n-k%n))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
x = abs(n - k)
a = n % x
b = abs(a - k)
print(min(a, b))

Submission status: Runtime Error

Solution code:
if __name__ == '__main__':
    s = input().rstrip().split(' ')
    N,K = int(s[0]), int(s[1])
    cand1 = abs(N - K*math.ceil(N/float(K)))
    cand2 = abs(N - K*math.floor(N/float(K)))
    print(min(cand1, cand2))

Submission status: Runtime Error

Solution code:
import sys

input = sys.stdin.readline

N, K = map(int, input().split())
diff = abs(K - N)

if N < K:
    print(N)
    exit()

if N % K == 0:
    print(0)
else:
    print(min(K - N%K), N%K)

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;

int main(){
 long long int a,b,c;
  cin >> a >> b;
  c = a%b;
  c=min(c,b-a);
  cout << c;
  return 0;
}

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())i

while(1):
  m=abs(n-k)

  if m>n:
    print(n)
    break
  else:
    n=m

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
a = input().split()
a = [int(i) for i in a]
s = sum(a)
dif = (1 / (4*m)) * s
cont = 0
for i in a:
     cont += (1 if i >= dif else 0)
print ('Yes' if cont >= m else 'No')

Submission status: Runtime Error

Solution code:
if __name__ == "__main__":
	N, K = list(map(int, input().split()))
    r = N%K
    print(r) if r < abs(r-K) else print(abs(r-K))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1000000000)

n, k = map(int, input().split())

def a(N, K):
    if abs(K - N) < K / 2:
        print(abs(K - N))
    else:
        a(abs(K - N), K)
        

if k == 1:
    print(0)
elif n < k / 2:
    print(n)
else:
    a(abs(k - n), k)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
if K == 1:
  print(0)
else:
  while N > 0:
    N -= K
  ans = min(N+K,abs(N))
print(ans)

Submission status: Runtime Error

Solution code:
import math
n,k = map(int,input().split())
if n == 0:
	print(0)
elif k == 1:
	print(0)
else:
	print(math.gcd(n,k))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin, stdout


def read():
    x0 = stdin.readline().rstrip()
    return x0


def __main__():

    n, k = map(int, read().split())

    if n == 0 or k == 1:
        r = 0
    else:
        d = abs(n-k)
        r = n % d
        if abs(r-d) < r:
            r = abs(r-d)

    print(r)

__main__()

Submission status: Runtime Error

Solution code:
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') #無限大
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(sys.stdin.readline()) #整数読み込み
def ifn(): return float(sys.stdin.readline()) #浮動小数点読み込み
def isn(): return sys.stdin.readline().split() #文字列読み込み
def imn(): return map(int, sys.stdin.readline().split()) #整数map取得
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) #整数-1map取得
def fmn(): return map(float, sys.stdin.readline().split()) #浮動小数点map取得
def iln(): return list(map(int, sys.stdin.readline().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, sys.stdin.readline().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD
def print_list(l): print(*l, sep='\n')
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def No(): print('No')

def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

N,K = imn()


if N % K == 0:
    print(0)
else:
    print(math.gcd(N,K))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000000000000000)

n, k = map(int, input().split())

def a(N, K):
    if abs(K - N) < K / 2:
        print(abs(K - N))
    else:
        a(abs(K - N), K)
        

if k == 1:
    print(0)
elif n < k / 2:
    print(n)
else:
    a(abs(k - n), k)

Submission status: Runtime Error

Solution code:
import math
n,k = map(int,input().split())

if k == 1:
	print(0)
else:
	print(math.gcd(n,k))

Submission status: Runtime Error

Solution code:
def main():
    n,m = map(int,input().split())
    a = sorted(list(map(int,input().split())),reverse=True)     
    s = sum(a)
    thresh = s/4/m
    if a[m-1]>=thresh:
        print('Yes')
    else:
        print('No')

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import math
NK = input().split()
N = int(NK[0])
K = int(NK[1])
if N==1 or K==1:
    print(0)
    exit()
else:
    print(math.gcd(N,K))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
tmp = N

a = abs(N - K)
ans = N % a

if ans == 1:
    print(0)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
num = abs(n - k)
tmp = n % num
print(min(tmp, abs(tmp - k)))

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
a = sorted(list(map(int,input().split())),reverse=True)
def main():
  s = sum(a)
  thresh = s/4/m
  if a[m-1]>=thresh:
    print('Yes')
  else:
    print('No')

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
from math import ceil
n, k = map(int, input().split())
val = abs(ceil(n/k)
print(min(n%k,abs(n-(k*val)),n))

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N, K = map(int, rl().split())
    if N == 0 or K == 1:
        print(0)
        exit()
    """
    if N < abs(N - K):
        print(N)
        exit()
    """
    print(min(abs(N % (N - K)), abs(N - (N - K) * (N // K + 1))))


if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())

print(min(n % k, n % abs(n - k)))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
gap = abs(N - K)

if K == 1:
    print(0)
else:
    ans = N % gap
    print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int, input().split())
num=abs(n-k)
if num>k:
    print(num%k)
else:
    print(k%num)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
s = sum(a)
vor = -(-s//(4*M))
if a[M-1] >= vor:
    print("Yes")
else:
    print("No")

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
a = sorted(list(map(int,input().split())),reverse=True)
def main():
    s = sum(a)
    thresh = s/4/m
    if a[m-1]<thresh:
        print('No')
    else:
        print('Yes')

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
int H = 0
while True:
  if (N > K):
    H = N-K
  else:
    H = K - N
  if (H>N):
    break
  else:
    N = H
print(N)

Submission status: Runtime Error

Solution code:
import math
n,k = map(int,input().split())

if k == 1:
	print(0)
else:
	print(math.gcd(n,k))

Submission status: Runtime Error

Solution code:
import math
n,k = map(int,input().strip().split())
x = min(math.gcd(n,k),n,k)
f = 1
while f:
    if abs(x-k)<x:
        x = abs(x-k)
        f = 1
    else:
        f = 0
print(x)

Submission status: Runtime Error

Solution code:
import math
n,k = map(int,input().split())

if k == 1:
	print(0)
else:

print(math.gcd(n,k))

Submission status: Runtime Error

Solution code:
N,M=[int(x) for x in input().split()]
print(min(abs(N-N//M*M),abs(N-(N//M+1)*(M))))N,M=[int(x) for x in input().split()]
print(min(abs(N-N//M*M),abs(N-(N//M+1)*(M))))

Submission status: Runtime Error

Solution code:
# coding:utf-8
import math

N,K=map(int, input().split())

if K==0:
    print("N")
elif N==0 or N%K==0:
    print("0")
else:
    print(math.gcd(N,K))

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
a = sorted(list(map(int,input().split())),reverse=True)
def main():
    s = sum(a)
    thresh = s/4/m
    if a[m-1]>=thresh:
        print('Yes')
    else:
        print('No')

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
if K == 1:
    ans = 0
else:
    if N >= K:
        N -= (N//K)*K
        ans = min(N,abs(N-K))
    else:
        min(N,abs(N-K))
print(ans)

Submission status: Runtime Error

Solution code:
n =input();
k = input();

x = n/k;

print(n-x*k)

Submission status: Runtime Error

Solution code:
import math
n,k = map(int,input().split())


print( 0 if k == 1 else math.gcd(n,k))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
import math
print(math.gcd(n,m))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
print(min([n%abs(n-k), abs(n%abs(n-k)-k)]))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())

print(min(N % M, abs(N % M - M))

Submission status: Runtime Error

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline

N, K = map(int, read().split())
d = abs(N - K)
N %= d
if abs(N - K) < N:
    print(abs(N - K))
else:
    print(N)

Submission status: Runtime Error

Solution code:
# coding:utf-8
import math

N,K=map(int, input().split())

if N==0 or N%K==0:
    print("0")
else:
    print(math.gcd(N,K))

Submission status: Runtime Error

Solution code:
n,k= map(int, input().split())
print(min(n%k,abs(n%k-k))

Submission status: Runtime Error

Solution code:
# import sys
# import math #sqrt
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# import itertools
# import collections # deque
# from operator import itemgetter
# from fractions import Fraction

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

def main():
    n,k = map(int,input().split())
    if k==1:
        print(0)
        return
    f = abs(n-k)
    print(n%f)
    return

if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
a=map(int,input().split())
a=sorted(a)
if a[-m] >= sum(a)/4/m:
    print("Yes")
else:
    print("No")

Submission status: Runtime Error

Solution code:
import math

a,b = map(int, input().split())

print(math.gcd(a, b))

Submission status: Runtime Error

Solution code:
from math import gcd
a,b=map(int,input().split())
if a%b==0:
    print(0)
else:
    print(gcd(a,b))

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque
import sys
import heapq
import bisect
import math
import itertools
import string
import queue
import copy
import time
# import numpy as np
from fractions import gcd

sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7


def inp(): return int(sys.stdin.readline())


def inp_list(): return list(map(int, sys.stdin.readline().split()))


def lcm(x, y): return (x * y) // gcd(x, y)


N, M = inp_list()
A = inp_list()
A.sort(reverse=True)
total = sum(A)

bd = total // (4 * M) + 1
if A[M - 1] <= bd:
    print('No')
else:
    print('Yes')

Submission status: Runtime Error

Solution code:
a,b=map(int,input().split())
print(min(a%b,(b%(a%b))))

Submission status: Runtime Error

Solution code:

import heapq
from collections import defaultdict, deque
from math import ceil, factorial
from fractions import gcd
import sys
sys.setrecursionlimit(10**7)
INF = float('inf')
MOD = 10**9+7

si = lambda: input().strip()
ii = lambda: int(input())
mii = lambda: map(int, input().split())
lmii = lambda: list(map(int, input().split()))
smii = lambda: sorted(map(int, input().split()))


N, K = mii()

ans = N % K
ans = min(ans, N % abs(N-K))

print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
from fractions import gcd
if n<k: n,k=k,n
if n%k==0 and n//k%2==0:
  print(0)
else:
  print(gcd(n,k))

Submission status: Runtime Error

Solution code:
from math import gcd, abs
n, k = map(int, input().split())
if k == 1:
  print(0)
else:
  print(gcd(abs(n - k), k))

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
print(n%abs(n-k))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
if k==1:
    print(0)
    exit()

def gcd(x,y):
    if x==y:
        return x
    elif x>y:
        r=-1
        while r!=0:
            r=x%y
            x=y
            y=r
        return x
    else:
        r=-1
        while r!=0:
            r=y%x
            y=x
            x=r
        return y

print(gcd(n,k))

Submission status: Runtime Error

Solution code:
def solve():
    N, M = map(int, input().split())
    a = abs(N-M)
    
    if a >= N:
        return N
    
    return min(N % a, N % M)

print(solve())