码迷,mamicode.com
首页 > 编程语言 > 详细

Python input/output boilerplate for competitive programming

时间:2019-10-14 01:27:52      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:sse   following   mat   math   split()   problem   pre   iss   tput   

The following code is my submission for Codeforces 1244C The Football Season.

import io
import sys
import math

def inverse(a, m):
    u = 0
    v = 1
    while a != 0:
        t = m // a
        m -= t * a
        a, m = m, a
        u -= t * v
        u, v = v, u
    assert m == 1
    return u

def main():
    # It helps to use a input file when testing or debugging your code locally.
    # with open("main.in", "r", encoding='utf-8') as f:
    with sys.stdin as f:
        n, p, w, d = map(int, f.readline().split())
        g = math.gcd(w, d)
        if p % g:
            print(-1)
            return
        W = w // g
        D = d // g
        x = inverse(W, D)
        y = (1 - W * x) // D
        assert W * x + D * y == 1
        m = p // g
        x *= m
        y *= m
        ub = min(x // D, (n - x - y) // (W - D))
        lb = (-y + W - 1) // W
        if lb > ub:
            print(-1)
            return
        X = x - lb * D
        Y = y + lb * W
        assert X >= 0 and Y >= 0 and X * w + Y * d == p and X + Y <= n
        print(X, Y, n - X - Y)
main()

Notes:

  1. // does floor divison in Python.

Python input/output boilerplate for competitive programming

标签:sse   following   mat   math   split()   problem   pre   iss   tput   

原文地址:https://www.cnblogs.com/Patt/p/11669251.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!