結果
問題 |
No.3307 Almost Equal
|
ユーザー |
|
提出日時 | 2025-10-05 14:57:17 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 48 ms / 2,000 ms |
コード長 | 3,082 bytes |
コンパイル時間 | 163 ms |
コンパイル使用メモリ | 82,104 KB |
実行使用メモリ | 57,224 KB |
最終ジャッジ日時 | 2025-10-05 14:57:31 |
合計ジャッジ時間 | 3,844 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 46 |
ソースコード
# input import sys input = sys.stdin.readline II = lambda : int(input()) MI = lambda : map(int, input().split()) LI = lambda : [int(a) for a in input().split()] SI = lambda : input().rstrip() LLI = lambda n : [[int(a) for a in input().split()] for _ in range(n)] LSI = lambda n : [input().rstrip() for _ in range(n)] MI_1 = lambda : map(lambda x:int(x)-1, input().split()) LI_1 = lambda : [int(a)-1 for a in input().split()] mod = 998244353 inf = 1001001001001001001 ordalp = lambda s : ord(s)-65 if s.isupper() else ord(s)-97 ordallalp = lambda s : ord(s)-39 if s.isupper() else ord(s)-97 yes = lambda : print("Yes") no = lambda : print("No") yn = lambda flag : print("Yes" if flag else "No") prinf = lambda ans : print(ans if ans < 1000001001001001001 else -1) alplow = "abcdefghijklmnopqrstuvwxyz" alpup = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" alpall = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" URDL = {'U':(-1,0), 'R':(0,1), 'D':(1,0), 'L':(0,-1)} DIR_4 = [[-1,0],[0,1],[1,0],[0,-1]] DIR_8 = [[-1,0],[-1,1],[0,1],[1,1],[1,0],[1,-1],[0,-1],[-1,-1]] DIR_BISHOP = [[-1,1],[1,1],[1,-1],[-1,-1]] prime60 = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59] sys.set_int_max_str_digits(0) # sys.setrecursionlimit(10**6) # import pypyjit # pypyjit.set_param('max_unroll_recursion=-1') from collections import defaultdict,deque from heapq import heappop,heappush from bisect import bisect_left,bisect_right DD = defaultdict BSL = bisect_left BSR = bisect_right def floor_sum(n, m, a, b): """ sum_{0 <= i <= n - 1} floor(a * i + b / m) """ res = 0 while n: if a < 0 or m <= a: k, a = divmod(a, m) res += n*(n-1) * k >> 1 if b < 0 or m <= b: k, b = divmod(b, m) res += n * k n, b = divmod(a*n+b, m) a, m = m, a return res # https://qiita.com/sounansya/items/51b39e0d7bf5cc194081 def floor_sum_pq(n, m, a, b): """ (p, q) = (0, 1), (0, 2), (1, 1) """ if m == 0: return 0, 0, 0 a1, a2 = divmod(a, m) b1, b2 = divmod(b, m) y = (a2 * n + b2) // m s01, s02, s11 = floor_sum_pq(y, a2, m, m + a2 - b2 - 1) nn = n * (n - 1) // 2 r01 = n * y - s01 r02 = n * y * y - s01 - 2 * s11 r11 = nn * y + (s01 - s02) // 2 r02 += n * (2 * n - 1) * (n - 1) * a1 * a1 // 6 r02 += 2 * nn * a1 * b1 r02 += b1 * b1 * n r02 += 2 * a1 * r11 r02 += 2 * b1 * r01 r01 += a1 * nn + b1 * n r11 += nn * (a1 * (2 * n - 1) + 3 * b1) // 3 return r01, r02, r11 a, b, c, d = MI() if a * d == b * c: print(-1) exit() if a * d < b * c: a, c = c, a b, d = d, b b2 = b // 2 d2 = d // 2 def calc(u): return (a * u + b2) * d - (c * u + d2) * b < b * d ok = 0 ng = 10 ** 18 while ng - ok > 1: m = ok + ng >> 1 if calc(m): ok = m else: ng = m # print(ok, ng) u = ok + 1 k = floor_sum(u, b, a, b2) - floor_sum(u, d, c, d2) # print(u, k) # for i in range(u): # k = floor_sum(i, b, a, b2) - floor_sum(i, d, c, d2) # print(i, i - 1 - k) print(u - 1 - abs(k))