description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
class Solution: def longestDupSubstring(self, S: str) -> str: def check(Val): base, modulus = 26, 2**32 AL = base**Val % modulus hk = 0 for i in range(Val): hk = hk * base + ord(S[i]) - ord("a") hk %= modulus hm = {hk: 0} for i in range(Val, N): hk = ( hk * base - (ord(S[i - Val]) - ord("a")) * AL + ord(S[i]) - ord("a") ) hk %= modulus if hk in hm and S[hm[hk] : hm[hk] + Val] == S[i - Val + 1 : i + 1]: return i - Val + 1 hm[hk] = i - Val + 1 N = len(S) start, end = 1, N res = 0 while start <= end: mid = start + (end - start) // 2 pos = check(mid) if pos: res = pos start = mid + 1 else: end = mid - 1 return S[res : res + end]
CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR DICT VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP VAR VAR VAR
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
class Solution: def longestDupSubstring(self, S: str) -> str: p = 239017 pows = [[1], [1]] hsh = [[0], [0]] mods = [int(1000000000.0) + 7, int(1000000000.0) + 9] for ch in S: for i in range(len(mods)): hsh[i].append((hsh[i][-1] + ord(ch) * pows[i][-1]) % mods[i]) pows[i].append(pows[i][-1] * p % mods[i]) l = 0 r = len(S) substrs = {} ans = "" while r - l > 1: m = (r + l) // 2 found = False for i in range(0, len(S) - m + 1): h0 = (hsh[0][i + m] - hsh[0][i]) * pows[0][-(i + 1)] % mods[0] h1 = (hsh[1][i + m] - hsh[1][i]) * pows[1][-(i + 1)] % mods[1] if (h0, h1) in substrs: found = True ans = S[i : i + m] break else: substrs[h0, h1] = True if found: l = m else: r = m substrs.clear() return ans
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR LIST LIST NUMBER LIST NUMBER ASSIGN VAR LIST LIST NUMBER LIST NUMBER ASSIGN VAR LIST BIN_OP FUNC_CALL VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR STRING WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR RETURN VAR VAR
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
class Solution: def longestDupSubstring(self, S: str) -> str: def has_duplicate(m, S): val, Mod = 0, 2**63 - 1 for i in range(m): val = (26 * val + ord(S[i])) % Mod d = set([val]) const = 26**m % Mod for i in range(m, len(S)): val = (26 * val + ord(S[i]) - ord(S[i - m]) * const) % Mod if val in d: return i - m + 1 d.add(val) return -1 l, r = 0, len(S) start, length = -1, 0 while l <= r: mid = l + (r - l) // 2 idx = has_duplicate(mid, S) if idx != -1: l = mid + 1 start, length = idx, mid else: r = mid - 1 if start == -1: return "" return S[start : start + length]
CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN STRING RETURN VAR VAR BIN_OP VAR VAR VAR
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
class Solution: def longestDupSubstring(self, S: str) -> str: arr = [(ord(ch) - ord("a")) for ch in S] n = len(arr) B = 29 mod = 2**63 - 1 def exists(L): seen = {} P = pow(B, L, mod) h = 0 for i in range(n): h = (h * B + arr[i]) % mod if i >= L: h = (h - arr[i - L] * P) % mod if i >= L - 1: if h in seen: return seen[h] seen[h] = i return -1 lo, hi = 0, len(S) pos = -1 while lo < hi: mid = (lo + hi + 1) // 2 idx = exists(mid) if idx != -1: lo = mid pos = idx else: hi = mid - 1 return S[pos - lo + 1 : pos + 1]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
class Solution: def find(self, s, m): seen = collections.defaultdict(list) mod = 1 << 63 - 1 base = 26 d = pow(26, m - 1, mod) chal = 0 for i in range(len(s)): if i >= m: l_chal = ord(s[i - m]) - ord("a") chal = (chal - l_chal * d) % mod l_chal = ord(s[i]) - ord("a") chal = (chal * base + l_chal) % mod if i >= m - 1: s_i = s[i - m + 1 : i + 1] if chal in seen: for j in seen[chal]: s_j = s[j - m + 1 : j + 1] if s_j == s_i: return s_i else: seen[chal].append(i) return "" def longestDupSubstring(self, S: str) -> str: l = 2 h = len(S) - 1 ans = "" while l <= h: m = (l + h) // 2 s = self.find(S, m) if s != "": ans = s l = m + 1 else: h = m - 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR VAR RETURN STRING FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
class Solution: def longestDupSubstring(self, S: str) -> str: A = [ord(c) for c in S] mod = 2**63 - 1 lo = 0 hi = len(S) res = 0 def test(sz): val = 0 p = pow(26, sz, mod) for i in range(sz): val = val * 26 + A[i] val %= mod seen = {val} for i in range(sz, len(A)): val = val * 26 + A[i] - A[i - sz] * p val = val % mod if val in seen: return i - sz + 1 seen.add(val) return -1 while lo < hi: mid = (lo + hi) // 2 pos = test(mid) if not pos >= 0: hi = mid else: res = pos lo = mid + 1 return S[res : res + lo - 1]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
p = 2**63 - 1 class Solution: def longestDupSubstring(self, S: str) -> str: def rabin_karp(mid): cur_hash = 0 for i in range(mid): cur_hash = (cur_hash * 26 + nums[i]) % p hashes = {cur_hash} pos = -1 max_pow = pow(26, mid, p) for i in range(mid, len(S)): cur_hash = (26 * cur_hash - nums[i - mid] * max_pow + nums[i]) % p if cur_hash in hashes: pos = i + 1 - mid hashes.add(cur_hash) return pos low, high = 0, len(S) - 1 end = 0 start = 0 nums = [(ord(c) - ord("a")) for c in S] while low <= high: mid = (low + high) // 2 pos = rabin_karp(mid) if pos == -1: high = mid - 1 else: start = pos low = mid + 1 return S[start : start + low - 1]
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
def check(arr, n, l, mod): p = pow(26, l, mod) window_hash = 0 hash_set = set() for i in range(l): window_hash = (26 * window_hash + arr[i]) % mod hash_set.add(window_hash) for i in range(1, n - l + 1): window_hash = (window_hash * 26 - arr[i - 1] * p + arr[i + l - 1]) % mod if window_hash in hash_set: return i hash_set.add(window_hash) return False class Solution: def longestDupSubstring(self, S: str) -> str: n = len(S) min_length, max_length = 1, n ans, prev_len = 0, 0 mod = (1 << 63) - 1 nums = [(ord(S[i]) - ord("a")) for i in range(n)] while min_length <= max_length: mid_length = int((max_length + min_length) / 2) start = check(nums, n, mid_length, mod) if start != False: if prev_len < mid_length: ans = start prev_len = mid_length min_length = mid_length + 1 else: max_length = mid_length - 1 return S[ans : ans + prev_len]
FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP VAR VAR VAR
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
class Solution: def longestDupSubstring(self, S: str) -> str: lo = 1 hi = len(S) nums = [(ord(i) - ord("a")) for i in S] self.modulus = 2**32 def dup(l): seen = set() hval = 0 for i in range(l): hval = (hval * 26 + nums[i]) % self.modulus seen.add(hval) al = pow(26, l, self.modulus) for i in range(1, len(S) - l + 1): hval = (hval * 26 - nums[i - 1] * al + nums[i + l - 1]) % self.modulus if hval in seen: return i seen.add(hval) return -1 start = -1 while lo <= hi: mi = lo + (hi - lo) // 2 begin = dup(mi) if begin != -1: lo = mi + 1 start = begin else: hi = mi - 1 return S[start : start + lo - 1]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
class Solution: def longestDupSubstring(self, S: str) -> str: def rk(m): nonlocal idx mp = {} p = b ** (m - 1) p %= mod y = 0 for i in range(m): y = y * b + num[i] y %= mod mp[y] = 0 for i in range(1, n - m + 1): y = ((y - num[i - 1] * p % mod) * b % mod + num[i - 1 + m]) % mod if y in mp: idx = i return 0 else: mp[y] = i return 1 n = len(S) b = 26 mod = 2**32 z = ord("a") num = [(ord(S[i]) - z) for i in range(n)] l = 1 r = n idx = 0 while l < r: m = (l + r) // 2 if rk(m) > 0: r = m else: l = m + 1 return S[idx : idx + l - 1]
CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
class Solution: def longestDupSubstring(self, S: str) -> str: N = len(S) l, r = 1, N nums = [(ord(x) - ord("a")) for x in S] KMAX = 2**63 - 1 def check(m): MAXL = pow(26, m, KMAX) total = 0 hashset = set() for i in range(m): total = (total * 26 + nums[i]) % KMAX hashset.add(total) for i in range(1, N - m + 1): total = (total * 26 - MAXL * nums[i - 1] + nums[i + m - 1]) % KMAX if total in hashset: return i hashset.add(total) return -1 while l < r: m = (l + r) // 2 if check(m) < 0: r = m else: l = m + 1 idx = check(r - 1) return S[idx : idx + r - 1]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
class Solution: def longestDupSubstring(self, S: str) -> str: n = len(S) mod = 10**30 ord_a = 97 def find_len_k_dup(k): hash_val = 0 seen = set() for i in range(k): hash_val = (hash_val * 26 + (ord(S[i]) - ord_a)) % mod seen.add(hash_val) power = 26**k % mod for i in range(1, n - k + 1): hash_val = ( (hash_val * 26 - (ord(S[i - 1]) - ord_a) * power) % mod + (ord(S[i + k - 1]) - ord_a) ) % mod if hash_val in seen: return i seen.add(hash_val) return None low = 0 high = len(S) res = "" while low < high: mid = (low + high) // 2 idx = find_len_k_dup(mid) if idx is not None: res = S[idx : idx + mid] low = mid + 1 else: high = mid return res
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR RETURN NONE ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
class Solution: def findRepSubstrGivenLength( self, nums: List[int], l: int, base: int, modulus: int ) -> int: n = len(nums) h = 0 for i in range(l): h = (h * base + nums[i]) % modulus seen = {h} for start in range(1, n - l + 1): h = ( h * base - nums[start - 1] * pow(base, l, modulus) + nums[start + l - 1] ) % modulus if h in seen: return start seen.add(h) return -1 def longestDupSubstring(self, S: str) -> str: if not S: return "" nums = [(ord(S[i]) - ord("a")) for i in range(len(S))] base = 26 modulus = 2**32 left = 1 right = len(S) while left <= right: mid = left + (right - left) // 2 if self.findRepSubstrGivenLength(nums, mid, base, modulus) != -1: left = mid + 1 else: right = mid - 1 start = self.findRepSubstrGivenLength(nums, left - 1, base, modulus) return S[start : start + left - 1]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR FUNC_DEF VAR IF VAR RETURN STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
class Solution: def longestDupSubstring(self, S: str) -> str: def code(t): return ord(t) - ord("a") + 1 def check(length): seen = collections.defaultdict(list) MOD = 10**9 + 7 P = 113 INV_P = pow(P, MOD - 2, MOD) h = 0 power = 1 for i, x in enumerate(S): h = (h + power * code(x)) % MOD if i < length - 1: power = power * P % MOD else: if h in seen: for j in seen[h]: if S[i - (length - 1) : i + 1] == S[j : j + length]: return S[j : j + length], True seen[h].append(i - (length - 1)) h = (h - code(S[i - (length - 1)])) * INV_P % MOD return "", False res = "" l, r = 1, len(S) - 1 while l <= r: mid = (l + r) // 2 sub, is_check = check(mid) if is_check: res = sub l = mid + 1 else: r = mid - 1 return res
CLASS_DEF FUNC_DEF VAR FUNC_DEF RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR FOR VAR VAR VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR RETURN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN STRING NUMBER ASSIGN VAR STRING ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
class Solution: def longestDupSubstring(self, S: str) -> str: n = len(S) modulus = 2**32 a = 26 nums = [(ord(c) - ord("a")) for c in S] def search(L): nonlocal a, n, modulus aL = pow(a, L, modulus) h = 0 for i in range(L): h = (h * a + nums[i]) % modulus seen = {h} for start in range(1, n - L + 1): h = (h * a - nums[start - 1] * aL + nums[start + L - 1]) % modulus if h in seen: return start seen.add(h) return -1 l, r = 1, n while l <= r: mid = l + r >> 1 if search(mid) != -1: l = mid + 1 else: r = mid - 1 start = search(r) return S[start : start + r]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR BIN_OP VAR VAR VAR
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
class Solution: def longestDupSubstring(self, S: str) -> str: ords = [(ord(ch) - 97) for ch in S] mod = 2**32 seen = set() def has_dup_with_length(length): seen.clear() p = pow(26, length, mod) hashed_prefix = 0 for i in range(length): hashed_prefix = (hashed_prefix * 26 + ords[i]) % mod seen.add(hashed_prefix) for i in range(length, len(S)): hashed_prefix = ( hashed_prefix * 26 + ords[i] - ords[i - length] * p ) % mod if hashed_prefix in seen: return i - length + 1 seen.add(hashed_prefix) start, lo, hi = 0, 0, len(S) - 1 while lo < hi: mid_length = (lo + hi + 1) // 2 idx = has_dup_with_length(mid_length) if idx: start = idx lo = mid_length else: hi = mid_length - 1 return S[start : start + lo]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP VAR VAR VAR
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
class Solution: def longestDupSubstring(self, S: str) -> str: def check(sz): seen = defaultdict(list) cur, base, MOD = 0, 256, (1 << 31) - 1 h = (1 << sz * 8) % MOD for i in range(sz): cur *= base cur += ord(S[i]) cur %= MOD seen[cur].append(0) for i in range(sz, len(S)): cur *= base cur += ord(S[i]) cur -= ord(S[i - sz]) * h cur %= MOD for j in seen[cur]: if S[j : j + sz] == S[i - sz + 1 : i + 1]: return True, S[i - sz + 1 : i + 1] seen[cur].append(i - sz + 1) return False, "" lo, hi = 1, len(S) res = "" while lo <= hi: mid = (lo + hi) // 2 flag, tmp = check(mid) if flag: lo = mid + 1 res = tmp else: hi = mid - 1 return res
CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER STRING ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR STRING WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
class Solution: def longestDupSubstring(self, S: str) -> str: n = len(S) nums = [(ord(S[i]) - ord("a")) for i in range(n)] def search(position): h = 0 for i in range(position): h = (h * 26 + nums[i]) % 2**32 seen = {h} const = 26**position % 2**32 for start in range(1, n - position + 1): h = ( h * 26 - nums[start - 1] * const + nums[start + position - 1] ) % 2**32 if h in seen: return start seen.add(h) return -1 left, right = 1, n while left <= right: pivot = (left + right) // 2 if search(pivot) != -1: left = pivot + 1 else: right = pivot - 1 start = search(left - 1) return S[start : start + left - 1]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER NUMBER IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
class Solution: def longestDupSubstring(self, S: str) -> str: def rk(m): mp = {} p = b ** (m - 1) p %= mod y = 0 for i in range(m): y = y * b + num[i] y %= mod mp[y] = 0 for i in range(1, n - m + 1): y = ((y - num[i - 1] * p % mod) * b % mod + num[i - 1 + m]) % mod if y not in mp: mp[y] = i else: return i return n n = len(S) b = 26 mod = 2**32 num = [] for i in range(n): num.append(ord(S[i]) - ord("a")) l = 1 r = n while l < r: m = (l + r) // 2 if rk(m) == n: r = m else: l = m + 1 x = l - 1 if x == 0: return "" k = rk(x) return S[k : k + x]
CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR BIN_OP VAR VAR VAR
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
class Solution: def longestDupSubstring(self, S: str) -> str: n = len(S) low = 0 high = n - 1 nums = [(ord(S[i]) - ord("a")) for i in range(n)] def findDuplicate(L): h = 0 a = 26 modulus = 2**32 for i in range(L): h = (h * a + nums[i]) % modulus seen = {h} aL = pow(a, L, modulus) for start in range(1, n - L + 1): h = (h * a - nums[start - 1] * aL + nums[start + L - 1]) % modulus if h in seen: return start seen.add(h) return -1 res = "" while low < high: mid = (low + high + 1) // 2 start = findDuplicate(mid) if start != -1: low = mid res = S[start : start + mid] else: high = mid - 1 return res
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR STRING WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
class Solution: def longestDupSubstring(self, S: str) -> str: base, mod = 26, 2**63 - 1 n = len(S) A = [(ord(ch) - ord("a")) for ch in S] def k_dup(k): curr = 0 for i in range(k): curr = (curr * base + A[i]) % mod seen = {curr} for i in range(1, n - k + 1): curr = (base * curr - pow(base, k, mod) * A[i - 1] + A[i + k - 1]) % mod if curr in seen: return i seen.add(curr) return 0 l, r, ans = 0, n, 0 while l < r: mid = l + r + 1 >> 1 pos = k_dup(mid) if pos: l = mid ans = pos else: r = mid - 1 return S[ans : ans + l]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP VAR VAR VAR
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.) Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)   Example 1: Input: "banana" Output: "ana" Example 2: Input: "abcd" Output: ""   Note: 2 <= S.length <= 10^5 S consists of lowercase English letters.
class Solution: def longestDupSubstring(self, S: str) -> str: n = len(S) BASE = 26 MOD = (1 << 63) - 1 POWS = [1] * n for i in range(1, n): POWS[i] = POWS[i - 1] * BASE % MOD def search(k): seen = set() h = 0 for i in range(k): h = (h * BASE + ord(S[i]) - 97) % MOD seen.add(h) for i in range(k, n): h = ( (h - (ord(S[i - k]) - 97) * POWS[k - 1]) * BASE + ord(S[i]) - 97 ) % MOD s = S[i - k + 1 : i + 1] if h in seen: return i seen.add(h) return -1 l, r = 0, n - 1 while l <= r: m = (l + r) // 2 if search(m) >= 0: l = m + 1 else: r = m - 1 if r < 0: return "" i = search(r) return S[i - r + 1 : i + 1]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
import sys ints = (int(x) for x in sys.stdin.read().split()) sys.setrecursionlimit(3000) def main(): a, b, c, d = (next(ints) for i in range(4)) ans = 0 A = (b - a + 1) * (c - b + 1) h = min(c - b, b - a) lo, hi = min(a + c, b + b), max(a + c, b + b) for z in range(min(a + b, c), d + 1): if z < a + b: pass elif z < lo: A -= 1 + z - (a + b) elif z < hi: A -= 1 + h elif z <= b + c: A -= 1 + (b + c) - z else: assert A == 0 if c <= z <= d: ans += A print(ans) return main()
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) s = 0 for z in range(c, d + 1): if b + c <= z: break u = min(b + b, a + c) v = max(b + b, a + c) if z >= v: s += (b + c - z) * (b + c - z + 1) // 2 elif u <= z: s += (b + c - v) * (b + c - v + 1) // 2 + (min(c - b, b - a) + 1) * (v - z) elif z >= a + b: s += (c - b + 1) * (b - a + 1) - (z - a - b + 2) * (z - b - a + 1) // 2 else: s += (c - b + 1) * (b - a + 1) print(s)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) s = 0 mz = d - c + 1 for x in range(a, b + 1): ym = max(b, c - x + 1) ny = c - ym + 1 nzm = min(ym + x - c, mz) if x > mz: n = mz - nzm + 1 s += n * (2 * nzm + n - 1) // 2 + (ny - n) * mz else: s += ny * (nzm * 2 + ny - 1) // 2 print(s)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
import sys from itertools import accumulate def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") INF = 10**18 MOD = 10**9 + 7 a, b, c, d = MAP() MAX = 10**6 + 7 imos = [0] * MAX for ai in range(a, b + 1): imos[ai + b] += 1 imos[ai + c + 1] -= 1 imos = list(accumulate(imos)) acc = list(accumulate(imos[::-1]))[::-1] ans = 0 for z in range(c, d + 1): ans += acc[z + 1] print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = [int(x) for x in input().split()] xymin = a + b xymax = b + c cntsPeak = min(b - a, c - b) + 1 xytotal = list(range(xymin, xymax + 1)) cnts = list(range(len(xytotal), 0, -1)) for i in range(len(xytotal)): cnts[i] = min(i + 1, cntsPeak) j = 1 for i in range(len(cnts) - 1, -1, -1): if j == cntsPeak: break cnts[i] = j j += 1 ans = 0 for i in range(len(xytotal)): ans += cnts[i] * max(0, min(xytotal[i] - 1, d) - c + 1) print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
import sys def read_input(input_path=None): if input_path is None: f = sys.stdin else: f = open(input_path, "r") a, b, c, d = map(int, f.readline().split()) return a, b, c, d def f(x): if x < 1: return 0 else: return x * (x + 1) * (x + 2) // 6 def sol(a, b, c, d): ans = ( (a + 2 * b - c) * (b + 1 - a) * (c + 1 - b) // 2 + f(c - a - b) - f(c - 2 * b - 1) - f(b + c - d - 1) + f(a + c - d - 2) + f(2 * b - d - 2) - f(a + b - d - 3) ) return [f"{ans}"] def solve(input_path=None): return sol(*read_input(input_path)) def main(): for line in sol(*read_input()): print(f"{line}") main()
IMPORT FUNC_DEF NONE IF VAR NONE ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR RETURN VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN LIST VAR FUNC_DEF NONE RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
import sys try: sys.stdin = open("input.txt", "r") sys.stdout = open("output.txt", "w") except: pass input = sys.stdin.readline a, b, c, d = map(int, input().split()) ans = 0 for i in range(a, b + 1): ans += 1 if c + (i - 1) <= d: ans += i - 1 else: ans += d - c if c - (i - 1) >= b: ans += i - 1 else: ans += c - b if c + (i - 2) <= d: calculate = (i - 2) * (i - 1) // 2 if c - (i - 2) >= b: pass else: sub = b - (c - (i - 2)) subSum = sub * (sub + 1) // 2 calculate -= subSum ans += calculate else: calculate = (i - 2) * (i - 1) // 2 if c - (i - 2) >= b: sub = c + (i - 2) - d subSum = sub * (sub + 1) // 2 ans += calculate - subSum else: sub = c + (i - 2) - d subSum = sub * (sub + 1) // 2 sub2 = b - (c - (i - 2)) subSum2 = sub2 * (sub2 + 1) // 2 ans += calculate - subSum - subSum2 gar = max(sub2 - (d - c), 0) ans += gar * (gar + 1) // 2 print(max(ans, 1))
IMPORT ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
def s(j): j -= A + B if t1 >= j and t2 >= j: return j + 1 elif t1 >= j: return t2 + 1 elif t2 >= j: return t1 + 1 else: return t1 + t2 - j + 1 A, B, C, D = map(int, input().split()) ans = 0 t1 = B - A t2 = C - B for i in range(A + B, B + C + 1): if i > D: ans += s(i) * (D - C + 1) else: ans += max(s(i) * (i - C), 0) print(ans)
FUNC_DEF VAR BIN_OP VAR VAR IF VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER IF VAR VAR RETURN BIN_OP VAR NUMBER IF VAR VAR RETURN BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) ans = 0 n = d + c + 3 ar = [0] * n for x in range(a, b + 1): ar[x + b] += 1 ar[x + c + 1] -= 1 for i in range(n - 1): ar[i + 1] += ar[i] for i in range(n - 1): ar[i + 1] += ar[i] for i in range(c, d + 1): ans += ar[-1] - ar[i] print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
def solve(x, a, b, c, d): low = b high = c yleft = -1 while low <= high: mid = (low + high) // 2 if x + mid > c: yleft = mid high = mid - 1 else: low = mid + 1 if yleft < b or yleft > c: return 0 zleft = min(x + yleft - 1, d) if zleft < c or zleft > d: return 0 a0 = zleft - c + 1 ydiff = c - yleft + 1 zdiff = d - zleft + 1 if ydiff <= zdiff: ans = ydiff * (2 * a0 + ydiff - 1) // 2 else: ans = zdiff * (2 * a0 + zdiff - 1) // 2 ydiff -= zdiff ans += ydiff * (d - c + 1) return ans def main(): a, b, c, d = map(int, input().split()) ans = 0 for x in range(a, b + 1): ans += solve(x, a, b, c, d) print(ans) main()
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = [int(x) for x in input().split()] if a == b == c == d: print("1") else: p = b + c - (a + b) arr = [(0) for x in range(0, p + 1)] for i in range(a, b + 1): arr[i + b - (a + b)] = arr[i + b - (a + b)] + 1 if i + c + 1 - (a + b) < len(arr): arr[i + c + 1 - (a + b)] = arr[i + c + 1 - (a + b)] - 1 for j in range(1, len(arr)): arr[j] = arr[j] + arr[j - 1] for k in range(len(arr) - 2, -1, -1): arr[k] = arr[k] + arr[k + 1] count = 0 for m in range(c, d + 1): if m - (a + b) >= len(arr) - 1: count += 0 elif m - (a + b) < 0: count += arr[0] else: count += arr[m + 1 - (a + b)] print(count)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) sol = 0 z = c sm = min(c - b, b - a) bg = max(c - b, b - a) while b + c >= z and z <= d: mn = a + b m1 = mn + sm mx = b + c m2 = mx - sm maxi = (sm + 1) * (bg + 1) if mn > z: sol += maxi if mn <= z and m1 > z: foo = z + 1 - mn sol += maxi sol -= foo * (foo + 1) // 2 if m1 <= z and m2 > z: foo = sm * (sm + 1) foo //= 2 maxi -= foo foo = z + 1 - m1 maxi -= (sm + 1) * foo sol += maxi if m2 <= z and mx > z: foo = mx - z sol += foo * (foo + 1) // 2 z += 1 print(sol)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) ans = 0 arr = [0] * (2 * d + 5) for i in range(a, b + 1): arr[i + b] += 1 arr[i + c + 1] -= 1 s = 0 for ii in range(2 * d + 5): s += arr[ii] if c < ii: ans += min(ii - c, d - c + 1) * s print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
N = int(1000000.0) + 77 a, b, c, d = map(int, input().split()) l = [0] * N for i in range(a, b + 1): l[i + b] += 1 l[i + c + 1] -= 1 for i in range(a, b + c + 2): l[i] = l[i] + l[i - 1] for i in range(a, d + c + 2): l[i] = l[i] + l[i - 1] ans = 0 for i in range(c, d + 1): z = l[d + c + 1] - l[i] ans += z print(ans)
ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
def prog(): a, b, c, d = map(int, input().split()) psum = [(0) for i in range(d + 2)] left = [(False) for i in range(d + 2)] right = [(False) for i in range(d + 2)] for i in range(a + b, min(2 * b + 1, d + 2)): left[i] = True for i in range(a + c, min(b + c + 1, d + 2)): right[i] = True maximum = (b - a + 1) * (c - b + 1) for i in range(min(a + b + 1, d + 2)): psum[i] = maximum bars = 0 for i in range(a + b + 1, d + 2): if left[i - 1] == True: bars += 1 if right[i - 2] == True: bars -= 1 psum[i] = psum[i - 1] - bars total = 0 for i in range(c + 1, d + 2): total += psum[i] print(total) prog()
FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) xplusy = [0] * (10**6 + 10) for x in range(a, b + 1): xplusy[x + b] += 1 xplusy[x + c + 1] -= 1 for i in range(1, 10**6 + 10): xplusy[i] += xplusy[i - 1] for i in range(1, 10**6 + 10): xplusy[i] += xplusy[i - 1] ans = 0 allxy = (b - a + 1) * (c - b + 1) for z in range(c, d + 1): ans += allxy - xplusy[z] print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
A, B, C, D = map(int, input().split()) def gauss_count(n): if n <= 0: return 0 return n * (n + 1) // 2 count = 0 for z in range(C, D + 1): if A + B > z: count += (B - A + 1) * (C - B + 1) else: x0 = z - C + 1 y0 = z - B + 1 incl1 = gauss_count(B - x0 + 1) excl1 = gauss_count(A - x0) excl2 = gauss_count(B - y0) count += incl1 - excl1 - excl2 print(count)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
def int_array(): return list(map(int, input().strip().split())) def total(start, times): an = start + times - 1 summ = times * (start + an) // 2 return summ a, b, c, d = int_array() ans = 0 for i in range(c, d + 1): b_first = i + 1 - b b_last = i + 1 - a if b_first > c: continue if b_last <= c: if b_last < b: b_range = c - b + 1 a_range = b - a + 1 ans += a_range * b_range else: extra = c - b_last b_range = b_last - b_first + 1 ans += b_range * (b_range + 1) // 2 an = 1 + (b_range - 1) ans += extra * an if b_first < b: extra = b - b_first ans -= extra * (extra + 1) // 2 else: b_range = min(b_last, c) - b_first + 1 ans += b_range * (b_range + 1) // 2 if b_first < b: extra = b - b_first ans -= extra * (extra + 1) // 2 print(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
A, B, C, D = [int(i) for i in input().split()] count_ymz = [None for i in range(D - B + 1)] for i in range(len(count_ymz)): count_ymz[i] = min(C, D - i) - max(C - i, B) + 1 pre_count_ymz = [None for i in range(len(count_ymz))] pre_count_ymz[0] = count_ymz[0] for i in range(1, len(count_ymz)): pre_count_ymz[i] = pre_count_ymz[i - 1] + count_ymz[i] ans = 0 for x in range(A, B + 1): if x > D - B + 1: ans += pre_count_ymz[-1] else: ans += pre_count_ymz[x - 1] print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NONE VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) ans = 0 for i in range(a + b, b + c + 1): counter = min(min(i - a, c) - max(i - b, b) + 1, b - a + 1) ans += counter * max(min(i, d + 1) - c, 0) print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
A, B, C, D = map(int, input().split()) ans = 0 howmany = [(0) for i in range(10**6 + 1)] cnt = 0 for i in range(A + B + 1): if A <= i <= B and B <= A + B - i <= C: cnt += 1 howmany[A + B] = cnt for z in range(A + B + 1, B + C + 1): if z <= min(A + C, 2 * B): cnt += 1 elif min(A + C, 2 * B) < z <= max(A + C, 2 * B): pass elif max(A + C, 2 * B) < z: cnt -= 1 howmany[z] = cnt for i in range(1, 10**6 + 1): howmany[i] += howmany[i - 1] for i in range(C, D + 1): ans += howmany[10**6] - howmany[i] print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
def summ(x): return x * (x + 1) // 2 a, b, c, d = map(int, input().split()) ans = 0 for i in range(a, b + 1): lb = i + b if lb > d: ans += (c - b + 1) * (d - c + 1) continue x = b if lb < c: rb = 0 x = c - i if i > d - c + 1: ans += (i - (d - c + 1)) * (d - c + 1) ans += summ(min(d - c + 1, c + i - c)) - summ(max(0, x + i - c - 1)) print(ans)
FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
def ip(): return int(input()) def Ip(): return map(int, input().split()) N = 500001 a, b, c, d = Ip() l = [0] * (2 * N) k = [0] * (2 * N) for i in range(a, b + 1): l[i + b] += 1 l[i + c + 1] -= 1 for i in range(1, 2 * N): k[i] = l[i] + k[i - 1] for i in range(len(k) - 2, -1, -1): k[i] += k[i + 1] ans = 0 for i in range(c, d + 1): ans += k[i + 1] print(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
def count_triangles(a, b, c, d): bc = c - b + 1 dc = d - c + 1 mx = min(bc, dc) l = -1 r = d - b + 1 ttl = 0 cnt = [(0) for i in range(d + 1)] while l < r: l += 1 r -= 1 ttl += 1 cnt[l] = min(ttl, mx) cnt[r] = cnt[l] sum = [(0) for i in range(d + 1)] sum[0] = cnt[0] for i in range(1, d + 1): sum[i] = sum[i - 1] + cnt[i] res = 0 for i in range(a, b + 1): res += sum[i - 1] return res def count_triangles2(a, b, c, d): res = 0 for i in range(c, d + 1): if a + b > i: res += (b - a + 1) * (c - b + 1) continue if b + c < i: break l = a h = c comb = 0 while l <= b <= h: if l + h > i: comb += b - l + 1 h -= 1 else: l += 1 res += comb return res abcd = input().split(" ") a = int(abcd[0]) b = int(abcd[1]) c = int(abcd[2]) d = int(abcd[3]) res = count_triangles(a, b, c, d) print(res)
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) ans = 0 for diff in range(max(0, c - a - b + 1), c - a + 1): k = min(b - a, diff) - max(0, diff - c + b) kk = min(diff, c - b) - max(0, diff - b + a) ch = (min(a + b + diff, d + 1) - c) * (min(k, kk, diff) + 1) if k >= 0 and kk >= 0: ans += ch print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
for _ in range(1): a, b, c, d = map(int, input().split()) ans = 0 for i in range(a, b + 1): x = b + i - 1 y = c + i - 1 if y < c: pass elif x > d: m = y - x + 1 ans += m * (d - c + 1) else: m = max(0, y - d) ans += m * (d - c + 1) m = min(d - c + 1, min(d, y) - c + 1) if m < 0: pass else: ans += m * (m + 1) // 2 m = max(1, max(c, x) - c + 1) - 1 ans -= m * (m + 1) // 2 print(ans)
FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) total, temp = 0, 0 freq = [0] * 2000007 for x in range(a, b + 1): freq[x + b] += 1 freq[x + c + 1] -= 1 for i in range(1, b + c + 2): freq[i] += freq[i - 1] for i in range(b + c + 1, -1, -1): freq[i] += freq[i + 1] for z in range(c, d + 1): total += freq[z + 1] print(total)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) count = 0 for s in range(max(a + b, c), b + c + 1): m = max(min(s - b, b) - max(s - c, a) + 1, 0) n = max(min(s - 1, d) - c + 1, 0) count += m * n print(count)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
def solve(): a, b, c, d = [int(x) for x in input().split()] count, left = 0, b for z in range(c, d + 1): x = z - left + 1 while x > b and left <= c: left += 1 x = z - left + 1 if x < a: x = a if left > c: break avail = b - x + 1 l = x - a count += avail * (c - left + 1) if c - left + 1 == l: l -= 1 count += l * (l + 1) // 2 elif c - left + 1 < l: l = c - left + 1 l -= 1 count += l * (l + 1) // 2 else: l -= 1 count += l * (l + 1) // 2 l += 1 count += l * (c - left + 1 - l) print(count) solve()
FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
def main(): a, b, c, d = map(int, input().split()) cnt = [0] * (b + c + 1) mid1 = min(a + c, 2 * b) mid2 = max(a + c, 2 * b) for z in range(c + 1, b + c + 1): if z < a + b: continue if z > 2 * c: continue if z < mid1: cnt[z] = z - a - b + 1 elif z > mid2: cnt[z] = b + c - z + 1 else: cnt[z] = mid1 - a - b + 1 cum = [0] * (b + c + 2) for z in range(b + c, c, -1): cum[z] = cum[z + 1] + cnt[z] res = sum(cum[c + 1 : d + 2]) return res print(main())
FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR IF VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) prefix = [0] * max(b + c + 1, d + 2) for i in range(a, b + 1): mins = i + b maxs = i + c prefix[maxs] += 1 prefix[mins - 1] -= 1 for i in range(len(prefix) - 2, -1, -1): prefix[i] += prefix[i + 1] ans = 0 for i in range(len(prefix) - 2, -1, -1): prefix[i] += prefix[i + 1] for i in range(c, d + 1): ans += prefix[i + 1] print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
A, B, C, D = map(int, input().split()) li = [0] * (B + C + 2) for i in range(A, B + 1): twosidemin = i + B twosidemax = i + C li[twosidemin] += 1 li[twosidemax + 1] -= 1 for i in range(1, len(li)): li[i] = li[i - 1] + li[i] answer = 0 summ = li[0] for i in range(1, len(li)): summ += li[i] li[i] = li[i - 1] + li[i] for i in range(C, min(B + C + 1, D + 1)): answer += summ - li[i] print(answer)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
A, b, c, d = map(int, input().split()) ans = 0 for a in range(A, b + 1): if a + b < c: t = c - a t += 1 if t > c: continue else: sv = c - t + 1 tv = d - c + 1 if sv > tv: ans = ans + sv * tv - (tv - 1) * tv // 2 else: ans = ans + sv * (sv + 1) // 2 elif a + b >= c: t = a + b if t > d: ans = ans + (c - b + 1) * (d - c + 1) continue tb = b ans = ans + (t - c) * (c - b + 1) b += 1 sv = c - b + 1 tv = d - t + 1 if sv > tv: ans = ans + sv * tv - (tv - 1) * tv // 2 else: ans = ans + sv * (sv + 1) // 2 b = tb print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
import sys input = sys.stdin.buffer.readline def solution(): a, b, c, d = map(int, input().split()) ans = [0] * 1000002 for i in range(a, b + 1): ans[i + b] += 1 ans[i + c + 1] -= 1 for i in range(1, 1000002): ans[i] += ans[i - 1] for i in range(1000000, -1, -1): ans[i] += ans[i + 1] sol = 0 for i in range(c, d + 1): sol += ans[i + 1] print(sol) solution()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
import sys input = sys.stdin.readline a, b, c, d = map(int, input().split()) ans = 0 pattern = 0 min_diff = min(b - a + 1, c - b + 1) for xy_sum in range(a + b, b + c + 1): pattern = min(xy_sum - a - b + 1, b + c - xy_sum + 1) if pattern > min_diff: pattern = min_diff z_ceil = min(d + 1, xy_sum) z_floor = c if z_ceil > z_floor: val = (z_ceil - z_floor) * pattern ans += val print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
def f(n): if n <= 0: return 0 else: return n def l(p, q, s): return f(s + 1) - f(s - p) - f(s - q) + f(s - p - q - 1) a, b, c, d = list(map(int, input().split())) p = b - a q = c - b r = d - c ans = 0 for i in range(1, c + b - d + 1): ans += l(p, q, d + i - a - b) if c == d: print(ans) else: ans *= d - c + 1 for i in range(d, c, -1): m = l(p, q, i - a - b) ans += m * (i - c) print(ans)
FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) Min1 = max(c + 1, a + b) cnt = 0 for i in range(Min1, b + c + 1): cntZ = min(d, i - 1) - c + 1 cntXY = min(b, i - b) - max(a, i - c) + 1 cnt = cnt + cntZ * cntXY print(cnt)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
import sys def main(): import sys input = sys.stdin.readline a, b, c, d = map(int, input().split()) al = rec(a, b, b, c) ans = 0 for z in range(c, d + 1): t = 0 if b + c <= z: break if a + b > z: t = al elif c - b <= b - a: if z <= a + c: t = al - tri1(a, b, z - b, z - a) elif z >= 2 * b: t = tri2(z - c, z - b, b, c) else: t = rec(z - b, b, b, c) + tri2(z - c, b, z - b, c) - (c - b + 1) elif z <= 2 * b: t = al - tri1(a, b, z - b, z - a) elif z >= a + c: t = tri2(z - c, z - b, b, c) else: t = rec(a, b, z - a, c) + tri2(a, z - b, b, z - a) - (b - a + 1) ans += t print(ans) def rec(l, r, b, t): return (r - l + 1) * (t - b + 1) def tri1(l, b, r, t): assert r - l == t - b R = rec(l, r, b, t) ol = r - l + 1 return (R - ol) // 2 + ol def tri2(l, b, r, t): assert r - l == t - b R = rec(l, r, b, t) ol = r - l + 1 return (R - ol) // 2 main()
IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_DEF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) dp = [0] * (b + c + 2) su = 0 for i in range(a, b + 1): sl = b + i sh = c + i dp[sl] += 1 dp[sh + 1] -= 1 for i in range(1, b + c + 2): dp[i] = dp[i] + dp[i - 1] for i in range(len(dp) - 3, -1, -1): dp[i] = dp[i + 1] + dp[i] for i in range(c, d + 1): if i < len(dp) - 1: su += dp[i + 1] print(su)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
def cnt(x): return x * (x + 1) // 2 t = 1 while t > 0: t -= 1 a, b, c, d = map(int, input().split()) ans = 0 for x in range(a, b + 1): l, r = x + b, x + c ans += cnt(min(r, d) - c) - cnt(max(min(l - 1, d) - c, 0)) ans += max(0, r - max(d, l - 1)) * (d - c + 1) print(ans)
FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) maxa = c + b mini = b + a lene = maxa - mini + 1 ans = [(0) for i in range(lene)] c1 = b - a + 1 c2 = c - b + 1 mini = min(c1, c2) ans[0] = 1 ans[-1] = 1 for i in range(1, len(ans) // 2): if i + 1 > mini: ans[i] = mini ans[-1 * (i + 1)] = mini else: ans[i] = i + 1 ans[-1 * (i + 1)] = i + 1 if len(ans) % 2 == 1: ans[len(ans) // 2] = mini total = 0 mini = a + b for i in range(len(ans)): ram = i + mini if ram <= c: continue elif ram > d: total += ans[i] * (d - c + 1) else: total += ans[i] * (ram - c) print(total)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) zmax = b + c zmin = max(c, a + b) res = 0 for z in range(zmin, zmax + 1): xmax = min(b, z - b) xmin = max(a, z - c) if xmin <= xmax: res += min(z - c, d - c + 1) * (xmax - xmin + 1) print(res)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
strli = input().split(" ") A = int(strli[0]) B = int(strli[1]) C = int(strli[2]) D = int(strli[3]) count = 0 numX = B - A + 1 numY = C - B + 1 for z in range(C, D + 1, 1): yG = z - A if yG < B: count += numY * numX continue elif yG >= C: validYs = C - yG add1sLeft = numX + validYs - 1 validYs = 0 if add1sLeft > 0: if add1sLeft <= numY: count += (1 + add1sLeft) * add1sLeft / 2 else: count += (1 + numY) * numY / 2 add1sLeft -= numY count += numY * add1sLeft else: validYs = C - yG if validYs + numX - 1 <= numY: count += (validYs + (validYs + numX - 1)) * numX / 2 else: opsToMax = numY - validYs count += (validYs + numY) / 2 * (opsToMax + 1) count += numY * (numX - 1 - opsToMax) print(round(count))
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
def read_int(): return int(input()) def read_ints(): return list(map(int, input().split(" "))) a, b, c, d = read_ints() ans = 0 m = min(c - b, d - c) n = max(c - b, d - c) for delta in range(d - b + 1): right = 0 if delta <= m: right = delta + 1 elif delta <= n: right = m + 1 else: right = d - b - delta + 1 left = max(0, b - max(delta + 1, a) + 1) ans += left * right print(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) ans = 0 for x in range(a, b + 1): min_, max_ = max(b, c - x + 1), min(c, d - x + 1) ta, tb = min_ + x - c, max_ + x - c ans += (ta + tb) * max(tb - ta + 1, 0) // 2 + (d - c + 1) * min(c - max_, c - b + 1) print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = list(map(int, input().split())) y = c otvet = 0 for x in range(a, b + 1): z = x + y - 1 tr_xy = min(z, d) - c + 1 y_kolvo = min(z - c, c - b) y_v_z = max(0, min(z - d, y_kolvo)) an = tr_xy - y_kolvo + y_v_z otvet += y_v_z * tr_xy + (tr_xy * tr_xy + tr_xy - an * an + an) // 2 print(otvet)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
def sumn(n1, n2): return (n2 * (n2 + 1) - (n1 - 1) * n1) // 2 a, b, c, d = map(int, input().split()) cnt = 0 for u in range(a, b + 1): for i in range(max(b, c - u), c + 1): if u + i > d: cnt += (d - c + 1) * (c - i + 1) break elif c < u + i <= d: if u + c >= d: t = d - u cnt += (u - c) * (t - i + 1) + sumn(i, t) i = t + 1 cnt += (d - c + 1) * (c - i + 1) break else: cnt += (u - c) * (c - i + 1) + sumn(i, c) break print(cnt)
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) s = 0 k = 0 l = 0 rar = min(b - a, c - b) rer = max(b - a, c - b) while l <= rar: if a + b + l > c: s += (k + 1) * (min(a + b + l, d + 1) - c) k += 1 l += 1 k -= 1 while l <= rer: if a + b + l > c: s += (k + 1) * (min(a + b + l, d + 1) - c) l += 1 k -= 1 while k >= 0: if a + b + l > c: s += (k + 1) * (min(a + b + l, d + 1) - c) k -= 1 l += 1 print(s)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
import sys (*data,) = sys.stdin.read().split("\n")[::-1] def input(): return data.pop() def fprint(*args, **kwargs): print(*args, **kwargs, flush=True) def eprint(*args, **kwargs): print(*args, **kwargs, file=sys.stderr) def ari(start, end, n): return (start + end) * n // 2 a, b, c, d = map(int, input().split()) ans = 0 for x in range(a, b + 1): mn_y = max(c - x + 1, b) mx_y = c start_d_num = min(mn_y + x, d + 1) - c end_d_num = min(mx_y + x, d + 1) - c n = end_d_num - start_d_num + 1 ans += ari(start_d_num, start_d_num + n - 1, n) mn_y += n ans += (mx_y + 1 - mn_y) * (d + 1 - c) print(ans)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
from itertools import accumulate a, b, c, d = map(int, input().split()) ans = 0 cnt = 0 ls = [0] * (b + c + 1) for i in range(a + b, b + c + 1): if not (i - a <= c and i - b <= b): ls[i] = min(c - b + 1, b - a + 1) elif i - a <= c and i - b > b: ls[i] = min(i - a - b + 1, b - a + 1) elif i - b <= b and i - a > c: ls[i] = min(c - b + 1, i - b - a + 1) else: ls[i] = min(i - a - b + 1, i - b - a + 1) if ls[a + 2 * b + c - i]: ls[i] = ls[a + 2 * b + c - i] acc = list(accumulate(ls)) for z in range(c, d + 1): if z >= b + c: continue ans += acc[-1] - acc[z] print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
import sys A, B, C, D = map(int, sys.stdin.readline().split()) def count(n): upper = min(B - A, C - B) + 1 est = min(n - A, C) - max(B, n - B) + 1 return min(upper, est) ans = 0 for rest in range(max(A + B, C + 1), B + C + 1): num_avail_z = min(D - C + 1, rest - C) ans += num_avail_z * count(rest) print(ans)
IMPORT ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
def main(): a, b, c, d = map(int, input().split()) q = c - b + 1 su = 0 d = min(d, b + c - 1) for z in range(c, d + 1): ma = q - max(b, z - b + 1) + b minx = max(a, z - c + 1) l = q - max(b, z - minx + 1) + b if l == ma: su += ma * (b - minx + 1) elif b - minx + 1 >= ma - l + 1: su += (b - minx + 1 - ma + l - 1) * ma su += sun(ma, l) else: su += sun(b - minx + 1, l) print(su) def sun(a, b): return (a + b) * (a - b + 1) // 2 main()
FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) s = 0 for z in range(c, min(d + 1, b + c)): if z - a <= c: s += (b - a + 1) * (c - max(b - 1, z - a)) if z - a >= b: x = min(z - a - b + 1, b - a) s += (b - a) * x - (x - 1) * x / 2 elif z - a > c: x = min(b + c - z, c - b + 1) s += (b + c - z) * x - (x - 1) * x / 2 print(int(s))
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
A, B, C, D = map(int, input().split()) def n_x(d): return min(max(0, B - d), B - A + 1) c = 0 m = min(D - C + 1, C - B + 1) dist = D - B + 1 for delta in range(0, dist): if delta < m: c += (delta + 1) * n_x(delta) elif dist - delta - 1 < m: c += (dist - delta) * n_x(delta) else: c += m * n_x(delta) print(c)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
def main(): t = 1 for _ in range(t): a, b, c, d = map(int, input().split()) ans = 0 for i in range(b, c + 1): mix = min(b, max(a, c - i + 1)) mx = min(b, max(a, d - i + 1)) if mx + i <= c: continue ok = min(d, mx + i - 1) - c + 1 l = min(ok, mix + i - c) l -= 1 ans += ok * (ok + 1) // 2 ans -= l * (l + 1) // 2 ans += (b - mx) * ok print(ans) return main()
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) ans = 0 for i in range(max(a + b, c), b + c + 1): if i == a + b or i == b + c: ans += min(i - c, d - c + 1) elif i - b < b: ans += min(max(0, i - b - a + 1), c - b + 1) * min(i - c, d - c + 1) elif i - b > b: ans += min(max(0, c + 1 - i + b), b + 1 - a) * min(i - c, d - c + 1) else: ans += min(c + 1 - b, b + 1 - a) * min(i - c, d - c + 1) print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
import sys sum = 0 a, b, c, d = map(int, input().split()) for i in range(max(a + b, c + 1), c + b + 1): sum += (min(d + 1, i) - c) * (min(i - b, b) - max(i - c, a) + 1) print(sum)
IMPORT ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
A, B, C, D = map(int, input().split()) ans = 0 bg = max(C + 1, A + B) for i in range(bg, B + C + 1): res = i - A - B x, y = A, B if res > B - A: x = B res -= B - A else: x += res res = 0 y += res ans += (min(x - A, C - y) + 1) * (min(i - 1, D) - C + 1) print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
[a, b, c, d] = input().split(" ") a = int(a) b = int(b) c = int(c) d = int(d) ans = (a + 2 * b - c) * (b + 1 - a) * (c + 1 - b) // 2 if a + b - c < 0: ans += (c - a - b) * (c - a - b + 1) * (c - a - b + 2) // 6 if 2 * b - c < 0: ans -= (c - 2 * b - 1) * (c - 2 * b) * (c - 2 * b + 1) // 6 if b > d + 1 - c: ans -= (b + c - d - 1) * (b + c - d) * (b + c - d + 1) // 6 if a > d + 1 - c: ans += (a + c - d) * (a + c - d - 1) * (a + c - d - 2) // 6 if 2 * b - c > d + 1 - c: ans += (2 * b - d) * (2 * b - d - 1) * (2 * b - d - 2) // 6 if a + b - c > d + 1 - c: ans = (d + 1 - c) * (b + 1 - a) * (c + 1 - b) print(ans)
ASSIGN LIST VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
inp = input().split() a = int(inp[0]) b = int(inp[1]) c = int(inp[2]) d = int(inp[3]) ans = c + 1 other = ans - a + 1 - b other2 = b - a + 1 correct = min(other, other2) if correct < 0: correct = 0 i = ans k = 0 condition = min(b * 2, c + a) condition2 = max(b * 2, c + a) count = 0 while i <= b + c: if k <= d - c: k = k + 1 count = count + k * correct if i < condition: correct = correct + 1 elif i >= condition2: correct = correct - 1 if other < 0: correct = 0 other += 1 i = i + 1 print(count)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
A, B, C, D = map(int, input().split()) ans = 0 for z in range(C, D + 1): y_STEPS = {(0): B, (1): max(B, z - B), (2): max(B, min(C, z - A + 1)), (3): C} y_MIN = min(max(0, 2 * B - z), B - A + 1) y_MAX = max(0, min(B - A + 1, C + B - z)) ans += (y_STEPS[3] - y_STEPS[2]) * y_MAX ans += (y_MIN + y_MAX) * (y_STEPS[2] - y_STEPS[1] + 1) // 2 print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = [int(x) for x in input().split()] maxAmt = min(b - a + 1, c - b + 1) low = b + a high = c + b sums = {high: 1} for x in range(high - 1, low - 1, -1): sums[x] = sums[x + 1] + min(maxAmt, x - low + 1, high - x + 1) total = 0 for x in range(c, d + 1): if x + 1 <= high: if x + 1 in sums: total += sums[x + 1] elif x < high: total += sums[low] print(total)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR DICT VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
import sys A, B, C, D = map(int, sys.stdin.readline().split()) N = D * 3 a = [0] * N ans = 0 for i in range(A, B + 1): a[i + B] += 1 a[i + C + 1] -= 1 for i in range(1, N): a[i] += a[i - 1] for i in range(1, N): a[i] += a[i - 1] for i in range(C, D + 1): ans += a[N - 1] - a[i] print(ans)
IMPORT ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) def an(l, r): return (r - l) * (r - l + 1) // 2 + (r - l + 1) * l ans = 0 for x in range(a, b + 1): l = b + x - 1 r = c + x - 1 if l > d: ans += (d - c + 1) * (c - b + 1) elif l >= c and r <= d: ans += an(l - c + 1, r - c + 1) elif l >= c and r > d: ans += an(l - c + 1, d - c + 1) + (d - c + 1) * (r - d) elif l < c and r <= d: ans += an(c - c + 1, r - c + 1) elif l < c and r > d: ans += an(c - c + 1, d - c + 1) + (d - c + 1) * (r - d) print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
import sys def MI(): return map(int, sys.stdin.readline().split()) def main(): a, b, c, d = MI() cnt = [] for z in range(c + 1, c + b + 1): b1 = z - b c1 = z - a l = max(b, b1) r = min(c, c1) cur = max(r - l + 1, 0) cnt.append(cur) m = len(cnt) for i in range(m - 2, -1, -1): cnt[i] += cnt[i + 1] print(sum(cnt[: d - c + 1])) main()
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
def countGreater(arr, n, k): l = 0 r = n - 1 leftGreater = n while l <= r: m = int(l + (r - l) / 2) if arr[m] >= k: leftGreater = m r = m - 1 else: l = m + 1 return n - leftGreater a, b, c, d = map(int, input().split()) s = dict() l = [] for i in range(c, d + 1): l.append(i) for i in range(a + b, b + c + 1): if i - a > c: st = i - c else: st = a if i - b < b: end = i - b else: end = b s.update({i: end - st + 1}) ans = 0 for i in s: ans += (len(l) - countGreater(l, len(l), i)) * s[i] print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR DICT VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
from itertools import accumulate from sys import stdin, stdout def MI(): return map(int, stdin.readline().split()) a, b, c, d = MI() ans = 0 arr = [0] * (b + c + 2) for x in range(a, b + 1): arr[x + b] += 1 arr[x + c + 1] -= 1 psum = list(accumulate(arr)) psum = list(accumulate(psum)) e = min(d, b + c) for z in range(c, e + 1): ans += psum[-1] - psum[z] stdout.write(str(ans) + "\n")
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
N = int(1000000.0 + 7) seg = [(0) for i in range(N)] a, b, c, d = map(int, input().split()) for i in range(a, b + 1): seg[i + c + 1] -= 1 seg[i + b] += 1 for i in range(1, N): seg[i] += seg[i - 1] for i in range(1, N): seg[i] += seg[i - 1] ans = 0 for i in range(c, d + 1): ans += seg[N - 1] - seg[i] print(ans)
ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
def fun(x): val = x * (x + 1) // 2 return val a, b, c, d = map(int, input().split()) ans = 0 for i in range(a, b + 1): rr = min(c + i - 1, d) s3 = rr - c + 1 x = rr - i + 1 if x <= b: s2 = c - b + 1 ans += s2 * s3 else: ans += (c - x) * s3 s2 = x - b + 1 if s2 >= s3: ans += fun(s3) else: ans += fun(s3) - fun(s3 - s2) print(ans)
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
def main(): a, b, c, d = map(int, input().split()) ans = 0 for x in range(a, b + 1): k = max(c + 1 - x, b) p = min(d - c + 1, x) q = min(d - c + 1, x + k - c) ans += p * (p + 1) // 2 - q * (q - 1) // 2 + (c - k - p + q) * p print(ans) main()
FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) ans = 0 for t in range(a + b, b + c + 1): M = min(b, t - b) m = max(a, t - c) dx = M - m + 1 if c <= t - 1 <= d: dx *= t - 1 - c + 1 ans += dx elif t - 1 > d: dx *= d - c + 1 ans += dx print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split(" ")) li = [0] * int(pow(10, 6) + 2) n = int(pow(10, 6) + 2) for i in range(a, b + 1): li[i + b] += 1 li[i + c + 1] -= 1 for i in range(1, n): li[i] += li[i - 1] for i in range(1, n): li[i] += li[i - 1] ans = 0 for i in range(c, d + 1): ans += li[n - 1] - li[i] print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) ans = (b - a + 1) * (c - b + 1) * (d - c + 1) arr = [0] * (d + 5) for i in range(a, b + 1): if i + b < len(arr): arr[i + b] += 1 if i + c + 1 < len(arr): arr[i + c + 1] -= 1 for i in range(1, len(arr)): arr[i] += arr[i - 1] for i in range(1, len(arr)): arr[i] += arr[i - 1] for i in range(c, d + 1): ans -= arr[i] print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
A, B, C, D = [int(i) for i in input().split()] ans = 0 for x in range(A, B + 1): ans += max(0, C - max(D - x + 1, B) + 1) * (D - C + 1) m = max(0, min(C, D - x) - max(B, C - x + 1) + 1) ans += m * (x - C) ans += m * (min(C, D - x) + max(B, C - x + 1)) // 2 print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
import sys input = sys.stdin.readline def sum1(x): k = min(d, c + x - 1, b + x - 1) if k < c: return 0 else: return (c - b + 1) * (k - c + 1) def sum2(x): k = min(d, c + x - 1) l = max(b + x, c) if k < l: return 0 else: return (c + x) * (k - l + 1) - (k + l) * (k - l + 1) // 2 a, b, c, d = map(int, input().split()) res = 0 for x in range(a, b + 1): res += sum1(x) + sum2(x) print(res)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
A, B, C, D = map(int, input().split()) X = B - A + 1 Y = C - B + 1 def cnt(c): if A + B > c: return X * Y if B + C <= c: return 0 m = B + C - c if X > Y: if m <= Y: return m * (m + 1) // 2 elif m <= X: return (m + m - Y + 1) * Y // 2 else: n = X + Y - 1 - m return X * Y - n * (n + 1) // 2 elif m <= X: return m * (m + 1) // 2 elif m <= Y: return (m + m - X + 1) * X // 2 else: n = X + Y - 1 - m return X * Y - n * (n + 1) // 2 ans = 0 for c in range(C, D + 1): ans += cnt(c) print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF IF BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR IF VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
A, B, C, D = map(int, input().split()) out = 0 for s in range(A + B, B + C + 1): xSmall = max(A, s - C) xBig = min(B, s - B) count = xBig - xSmall + 1 if count < 0: continue zCount = max(0, min(s - 1, D) - C + 1) out += count * zCount print(out)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Like any unknown mathematician, Yuri has favourite numbers: $A$, $B$, $C$, and $D$, where $A \leq B \leq C \leq D$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides $x$, $y$, and $z$ exist, such that $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds? Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property. The triangle is called non-degenerate if and only if its vertices are not collinear. -----Input----- The first line contains four integers: $A$, $B$, $C$ and $D$ ($1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5$) — Yuri's favourite numbers. -----Output----- Print the number of non-degenerate triangles with integer sides $x$, $y$, and $z$ such that the inequality $A \leq x \leq B \leq y \leq C \leq z \leq D$ holds. -----Examples----- Input 1 2 3 4 Output 4 Input 1 2 2 5 Output 3 Input 500000 500000 500000 500000 Output 1 -----Note----- In the first example Yuri can make up triangles with sides $(1, 3, 3)$, $(2, 2, 3)$, $(2, 3, 3)$ and $(2, 3, 4)$. In the second example Yuri can make up triangles with sides $(1, 2, 2)$, $(2, 2, 2)$ and $(2, 2, 3)$. In the third example Yuri can make up only one equilateral triangle with sides equal to $5 \cdot 10^5$.
a, b, c, d = map(int, input().split()) def ncombi(n): if n <= 0: return 0 else: return n * (n + 1) // 2 ans = 0 for x in range(c, d + 1): ans += ( ncombi(b + c - x) - ncombi(2 * b - x - 1) - ncombi(a + c - x - 1) + (2 * b - x - 1 > b - a) * ncombi(2 * b - x - 2 + a - b) ) print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR