CF1244C The Football Season
problem
给定\(n,p,w,d\),求解任意一对\((x,y)\)满足$$xw+yd=p\ x + y \le n$$
\(1\le n\le 10^{12},0\le p\le 10^{17},1\le d<w \le 10^5\)
solution
注意到\(n,p\)非常大,\(w,d\)比较小。而且\(w>d\)。所以我们就想让\(y\)尽量小。
实际上如果最终有解,那在\(y\le w\)中肯定有解。
证明如下:
如果有\(y'=aw+k(a\ge 1,0\le k < w)\)使得\(xw+y'd=p\)。即\(xw+(aw+k)d=xw+awd+kd=(x+ad)w+kd=p\)。
发现\(xw+(aw+k)d\)的系数和为\(x+aw+k\)。\((x+ad)w+kd\)的系数和为\(x+ad+k\)。又因为\(w>d\)。所以后者的系数和要小。所以\(d\)的系数一定小于等于\(w\)
然后在区间\([0,w]\)中枚举\(y\)。计算\(x\)即可。
code
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<ctime>
#include<cmath>
#include<map>
#include<string>
using namespace std;
typedef long long ll;
ll read() {
ll x = 0,f = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0',c = getchar();}
return x * f;
}
int main() {
ll n = read(),p = read(),w = read(),d = read();
for(ll y = 0;y <= w;++y) {
if((p - y * d) % w) continue;
ll x = (p - y * d) / w;
if(x >= 0 && x + y <= n) {
printf("%I64d %I64d %I64d\n",x,y,n - x - y);
return 0;
}
}
puts("-1");
return 0;
}
CF1244C The Football Season的更多相关文章
- [CF1244C] The Football Season【数学,思维题,枚举】
Online Judge:Luogu,Codeforces Round #592 (Div. 2) C Label:数学,思维题, 枚举 题目描述 某球队一共打了\(n\)场比赛,总得分为\(p\), ...
- CF 1244 C - The Football Season
C - The Football Season 先考虑求解 \[ x\times w + y\times d=p \] 若存在一组解 \[ \begin{cases} x_0\\ y_0 = kw + ...
- [Codeforces 1244C] The Football Season
思维加枚举 题意 :足球赛,赢平所得到的分数分别为w和d,w>d,分别求赢平输的场数,输出一组即可,即x+y+z=n 且 xw+yd=p的一组解. 可以扩展公约数做,但由于注意到d和w<1 ...
- American Football Vocabulary!
American Football Vocabulary! Share Tweet Share You’ll learn all about the vocabulary of American fo ...
- CodeForces-1244C-The Football Season-思维
The football season has just ended in Berland. According to the rules of Berland football, each matc ...
- Codeforces Round #592 (Div. 2)
A. Pens and Pencils 题目链接:https://codeforces.com/contest/1244/problem/A 题意: 给定五个数 a , b , c , d , k 求 ...
- codeforces #592(Div.2)
codeforces #592(Div.2) A Pens and Pencils Tomorrow is a difficult day for Polycarp: he has to attend ...
- Python input/output boilerplate for competitive programming
The following code is my submission for Codeforces 1244C The Football Season. import io import sys i ...
- 【Codeforces】CF Round #592 (Div. 2) - 题解
Problem - A Tomorrow is a difficult day for Polycarp: he has to attend \(a\) lectures and \(b\) prac ...
随机推荐
- js-06-字符串
一.查找字符串的字符串 a:indexOf:没有查询到返回值为-1: b:lastIndexoOf:查找到的为重复的最后一个: c:search 查找: var str="good good ...
- VC遍历访问目录下的文件
访问目录文件夹下的文件是经常需要的操作,C/C++和win32接口都没有提供直接调用的函数.在这里总结了几个经常用到的函数,通过MFC的CFileFind函数递归遍历实现,包括以下几个功能函数: 查找 ...
- resource和autowired
spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@PreDestroy. @Resour ...
- centos重启出现type Control-D to continue【fsck使用】
VMware vSphere克隆异常/dev/sda2:UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.(i.e.,without -a or -p optio ...
- CentOS7 安装Python3.6.8
CentOS7 安装Python3.6.8 1. 安装依赖环境 yum -y groupinstall "Development tools" yum -y install zli ...
- Vim 命令常用功能详解
Vim编辑器 文本编辑器 , 字处理器ASCIIvi:Visual Interface vim :VI iMproved 全屏编辑器,模式化编辑器vim 模式:编辑模式(命令模式)输入模式末行模式 模 ...
- Python-类的几种调用方法
一:实例 二:静态 可以调用类以外的变量,只限于此模块. 三:类方法 可以调用该类中定义的变量进行使用. 直接上代码
- str函数之不同变量之间如何连接,外加浮点运算注意事项
最近看书了解到不同字符串之间可以用“+”号来连接,遂思考如何将不同变量连接起来,思考试验并上网查询后得出了结果,在此将所学分享给在阅读的各位. 数据类型的转换 常识可知,在python中,不同的数 ...
- javascript地址引用
javascript地址引用 var a = new Object(); a.price = ; var b = a; b.price = ; //b更改了属性值,a的属性值一起会被改变 alert( ...
- error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in
问题: error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_ ...