加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python input/output boilerplate for competitive programming

发布时间:2020-12-20 10:19:26 所属栏目:Python 来源:网络整理
导读:The following code is my submission for Codeforces 1244C The Football Season. import ioimport sysimport mathdef 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 udef main():

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读