結果

問題 No.1803 Remainder of Sum
ユーザー chineristACchineristAC
提出日時 2022-01-07 22:40:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 138 ms / 2,000 ms
コード長 2,157 bytes
コンパイル時間 336 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 77,440 KB
最終ジャッジ日時 2024-04-26 18:49:09
合計ジャッジ時間 2,745 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
55,808 KB
testcase_01 AC 135 ms
77,440 KB
testcase_02 AC 133 ms
77,312 KB
testcase_03 AC 137 ms
77,056 KB
testcase_04 AC 134 ms
77,312 KB
testcase_05 AC 138 ms
77,184 KB
testcase_06 AC 130 ms
76,928 KB
testcase_07 AC 117 ms
77,056 KB
testcase_08 AC 128 ms
77,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import e, log,gcd

class UnionFindVerSize():
    def __init__(self, N):
        self._parent = [n for n in range(0, N)]
        self._size = [1] * N
        self.group = N

    def find_root(self, x):
        if self._parent[x] == x: return x
        self._parent[x] = self.find_root(self._parent[x])
        stack = [x]
        while self._parent[stack[-1]]!=stack[-1]:
            stack.append(self._parent[stack[-1]])
        for v in stack:
            self._parent[v] = stack[-1]
        return self._parent[x]

    def unite(self, x, y):
        gx = self.find_root(x)
        gy = self.find_root(y)
        if gx == gy: return

        self.group -= 1

        if self._size[gx] < self._size[gy]:
            self._parent[gx] = gy
            self._size[gy] += self._size[gx]
        else:
            self._parent[gy] = gx
            self._size[gx] += self._size[gy]

    def get_size(self, x):
        return self._size[self.find_root(x)]

    def is_same_group(self, x, y):
        return self.find_root(x) == self.find_root(y)

input = lambda :sys.stdin.readline()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

def check(N,M):
    uf = UnionFindVerSize(N)
    E = []
    for i in range(N):
        for j in range(i+1,N):
            c = (i+1+j+1) % M
            E.append((c,i,j))
    E.sort()
    cnt = [0 for i in range(M)]
    ans = 0
    for c,i,j in E:
        if not uf.is_same_group(i,j):
            cnt[c] += 1
            uf.unite(i,j)
            ans += c
        if uf.group==1:
            return ans
    
    
#M = int(input())
#for n in range(M//2,3*M):
    #m = M
    #print(n,m,check(n,m))

for _ in range(int(input())):
    n,m = mi()


    if n < m:
        if m&1==0:
            rest_count = n-1-2*(n-(m//2))
            res = n - (m//2) + (3+2+rest_count)*rest_count//2
        else:
            rest_count = n-1-(2*(n-m//2)-1)
            res = n - (m//2) + (3+2+rest_count)*rest_count//2-1
        
    else:
        res = m//2
    
    print(res)

0