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 ...
随机推荐
- ES6之Class类
一.Class的基本语法 1.简介 基本上,ES6的class可以看作只是一个 语法糖,它的绝大部分功能,ES5都可以做到,新的class写法只是让 对象原型 的写法更加清晰.更像面向对象编程的语法而 ...
- 人体分析Demo-百度API
本示例是采用Delphi 7 调用百度人体分析API:首先说明一下,怎么创建测试应用. 1. 登录百度云官网 https://cloud.baidu.com/ 当然需要一个百度账号 2. 进入管理 ...
- 选择IT公司的雇主提问
做为IT从业人员,我们去一家公司时,判断一家公司的专业性时,可以通过以下提问获得反馈: 技术问题 1.这个项目使用了哪些技术(语言,框架,库)?2.应用程序是一体化架构还是微服务架构?3.采用了哪些设 ...
- 洛谷 UVA1328 Period
洛谷 UVA1328 Period 洛谷传送门 题目描述 PDF 输入格式 无 输出格式 无 题意翻译 题意描述 对于给定字符串S的每个前缀,我们想知道它是否为周期串.也就还是说,它是否为某一字符串重 ...
- Goland安装
Goland安装 http://c.biancheng.net/view/6124.html
- HTML连载53-网易注册界面实战之content的头部、content注册信息
一. 这次完成了content部分的右边图片以及content的top部分的边角填充 <!DOCTYPE html> <html lang="en"> &l ...
- js获取select显示的值
html代码: <select id="myid"> <option value ="1">one</option> < ...
- 用二进制写程序,提升装 X 境界
用二进制来写程序这么反人类的事情,的确是很装的事情,但是它不但是一件很装的事情,也是掌握底层知识的基础能力之一.听我慢慢道来. 程序设计语言有高级语言和低级语言之分,尤其是现在各种编程语言的不断发展, ...
- 使用matplotlib.pyplot中scatter()绘制散点图
1.二维散点图 二维散点图的函数原型: matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=Non ...
- 【朝花夕拾】Android性能篇之(八)来自官网的自白
前言 转载请声明,转自[https://www.cnblogs.com/andy-songwei/p/10823372.html],谢谢! Android性能优化无疑是Android中的一个重点,也是 ...