Submission #17871107
Source Code Expand
Copy
def floor_sum(n, m, a, b):ans = 0if a >= m:ans += (n - 1) * n * (a // m) // 2a %= mif b >= m:ans += n * (b // m)b %= my_max = (a * n + b) // mx_max = y_max * m - bif y_max == 0: return ansans += (n - (x_max + a - 1) // a) * y_maxans += floor_sum(y_max, a, m, (-x_max)% a)return ansdef main():t = int(input())for _ in range(t):n, m, a, b = map(int, input().split())print(floor_sum(n, m, a, b))
def floor_sum(n, m, a, b): ans = 0 if a >= m: ans += (n - 1) * n * (a // m) // 2 a %= m if b >= m: ans += n * (b // m) b %= m y_max = (a * n + b) // m x_max = y_max * m - b if y_max == 0: return ans ans += (n - (x_max + a - 1) // a) * y_max ans += floor_sum(y_max, a, m, (-x_max)% a) return ans def main(): t = int(input()) for _ in range(t): n, m, a, b = map(int, input().split()) print(floor_sum(n, m, a, b)) if __name__ == "__main__": main()
Submission Info
Submission Time | |
---|---|
Task | C - Floor Sum |
User | Akari__ |
Language | Python (3.8.2) |
Score | 100 |
Code Size | 573 Byte |
Status | AC |
Exec Time | 1162 ms |
Memory | 9216 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 100 / 100 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | example_00 |
All | example_00, random_00, random_01, random_02, random_03, random_04, small_00, small_01, small_02, small_03, small_04 |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
example_00 | AC | 20 ms | 8864 KB |
random_00 | AC | 318 ms | 9216 KB |
random_01 | AC | 1162 ms | 8884 KB |
random_02 | AC | 907 ms | 9004 KB |
random_03 | AC | 621 ms | 9000 KB |
random_04 | AC | 269 ms | 8992 KB |
small_00 | AC | 144 ms | 8888 KB |
small_01 | AC | 499 ms | 8884 KB |
small_02 | AC | 385 ms | 9136 KB |
small_03 | AC | 271 ms | 8884 KB |
small_04 | AC | 125 ms | 9216 KB |