Python input/output boilerplate for competitive programming
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:
//does floor divison in Python.
References
Python input/output boilerplate for competitive programming的更多相关文章
- Docker 在转发端口时的这个错误Error starting userland proxy: mkdir /port/tcp:0.0.0.0:3306:tcp:172.17.0.2:3306: input/output error.
from:https://www.v2ex.com/amp/t/463719 系统环境是 Windows 10 Pro,Docker 版本 18.03.1-ce,电脑开机之后第一次运行 docker ...
- PHP-FPM-failed to ptrace(PEEKDATA) pid 123: Input/output error
If you're running PHP-FPM you can see these kind of errors in your PHP-FPM logs. $ tail -f php-fpm.l ...
- NFS挂载异常 mount.nfs: Input/output error
[root@localhost ~]# vi /etc/exports #增加/nfs 192.168.10.132(rw,no_root_squash,no_all_squash,async) [r ...
- BIOS(Basic Input/Output System)是基本输入输出系统的简称
BIOS(Basic Input/Output System)是基本输入输出系统的简称 介绍 操作系统老师说,平时面试学生或者毕业答辩的时候他都会问这个问题,可见这个问题对于计算机专业的学生来说是如此 ...
- read()、write()返回 Input/output error, Device or resource busy解决
遇到的问题,通过I2C总线读.写(read.write)fs8816加密芯片,报错如下: read str failed,error= Input/output error! write str fa ...
- Angular 个人深究(三)【由Input&Output引起的】
Angular 个人深究(三)[由Input&Output引起的] 注:最近项目在做别的事情,angular学习停滞了 1.Angular 中 @Input与@Output的使用 //test ...
- dpdk EAL: Error reading from file descriptor 23: Input/output error
执行test程序时输出: EAL: Error reading from file descriptor 23: Input/output error 原因: 在虚拟机添加的网卡,dpdk不支持导致的 ...
- python's output redirect
[python's output redirect] fin = open("xx.txt", 'r'); print >>fin, "hello world ...
- html5 填表 表单 input output 与表单验证
1.<output> Js计算结果 <form oninput="res.value = num1.valueAsNumber*num2.valueAsNumber ...
随机推荐
- AcWing:172. 立体推箱子(bfs)
立体推箱子是一个风靡世界的小游戏. 游戏地图是一个N行M列的矩阵,每个位置可能是硬地(用”.”表示).易碎地面(用”E”表示).禁地(用”#”表示).起点(用”X”表示)或终点(用”O”表示). 你的 ...
- conda程序使用
conda -c 参数 使用清华镜像时不要使用-c 参数.-c参数是anaconda的默认channel. 查询安装源中某个包的可以安装的版本 conda search -f package_name ...
- SSRF和XSS-filter_var(), preg_match() 和 parse_url()绕过学习
0x01:url标准的灵活性导致绕过filter_var与parse_url进行ssrf filter_var() (PHP 5 >= 5.2.0, PHP 7) filter_var — 使用 ...
- LeetCode 109. 有序链表转换二叉搜索树(Convert Sorted List to Binary Search Tree)
题目描述 给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定的有序链表: ...
- iOS真机调试之免费预配(Free provisioning)
免费预配允许开发者在不加入Applec Developer Program的情况下,可以发布和测试App 注意:免费预配(Free Provisioning)与自动预配(Auto Provisioni ...
- win7远程连接ubuntu,出现灰屏解决方法
问题: win7远程虚拟机ubuntu 12.04出现灰色屏幕 打开windows自带的远程桌面连接.输入ubuntu虚拟机的IP地址 可以连接上,输入username和password 点击OK ...
- LC 648. Replace Words
In English, we have a concept called root, which can be followed by some other words to form another ...
- Fiddler Wireshark 抓包
使用 层次 Fiddler 简单 第七层应用层的 HTTP(S) 协议的包 Wireshark 复杂 第三层网络层的包 注意:Fiddler 只能抓到走系统代理的流量.不走系统代理的流量(比如浏览器选 ...
- 安卓之Android.mk编写
题记:编译环境可以参考https://www.cnblogs.com/ywjfx/p/9960817.html 不管是写C还是java,我想所有的程序员都经历过HelloWorld程序的编写,现在让我 ...
- ControlTemplate in WPF —— Calendar
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...