codeforces 1244C (思维 or 扩展欧几里得)
题意分析
已知 n , p , w, d ,求x , y, z的值 ,他们的关系为:
x + y + z = n
x * w + y * d = p
思维法
当 y < w 的时候,我们最多通过1e5次枚举确定答案
而当 y >= w 的时候,平局所得分为:y * d = (y-w)*d + w*d ,可以看作平局的局数为 y - w ,多出的w*d贡献给 (w*d)/w = d 局胜局,所以胜局为 x + d ,说明此时用x+y局胜局和平局得到的分数可以由 x + d + y - w 局胜局和平局得到,同时此时的胜局+平局次数更少,所以枚举 y > w 的情况是没有意义的,完全可以通过枚举 y = {0 ~ w - 1} 得到答案
综上所述,我们只需要在 [0,w) 范围内枚举y即可得到答案
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<string>
#include<fstream>
#include<vector>
#include<stack>
#include <map>
#include <iomanip> #define bug cout << "**********" << endl
#define show(x, y) cout<<"["<<x<<","<<y<<"] "
#define LOCAL = 1;
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + ;
const int Max = 5e3 + ; ll n, sum;
ll win, draw; int main()
{
#ifdef LOCAL
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
scanf("%lld%lld%lld%lld", &n, &sum, &win, &draw);
for(ll y = ; y < win ; y ++)
{
if ((sum - y * draw) % win)
continue;
ll x = (sum - y * draw) / win;
if(x + y <= n && x >= )
{
printf("%lld %lld %lld\n",x,y,n-x-y);
return ;
}
}
printf("-1\n");
return ;
}
扩展欧几里得法
扩展欧几里得算法才是这个题目的正解,从式子:x * w + y * d = p 中,可以看出其正好对应扩展欧几里得公式:x * a + y * b = gcd(a,b) ,借助扩展欧几里得公式,我们可以得到x * w + y * d = p的一组解,具体步骤如下:
1)用扩展欧几里得公式求:
x0 * w + y0 * d = gcd(w,d)
得到了x0,y0
2)将上述式子两边同乘 k = p / gcd(w,d) ,【必须保证 (p % gcd(w,d)) == 0 ,否则无解】,得到:
x0 * k * w + y0 * k * d = gcd(w,d) * k
即
x0 * k * w + y0 * k * d = p
这样, x = x0*k , y = y0*k 就是式子 x * w + y * d = p 的一组解,
3)由于x,y,z需要满足
x + y + z = n
因此,我们通过调整x,y,使得上式得以满足,为此,我们需要让x + y 的值尽量小;注意到 w > d ,说明当 y 取最小值的时候,x + y 取最小值,如果此时 x + y <= n ,说明有解,否则无解
又根据 x * w + y * d = p ,我们得知 y 每次变化:+- w/gcd(w,d) ,所以y 的最小值即为
y = ( y0 * k ) % (w/gcd(w,d))
4)最后,根据得到的y,求出x 和 z 的值,随后,判断 x + y 是否小于 n ,满足即输出 x , y , z,否则说明无解
(注意求 y 的时候进行取模操作,不然会爆 long long )
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<string>
#include<fstream>
#include<vector>
#include<stack>
#include <map>
#include <iomanip> #define bug cout << "**********" << endl
#define show(x, y) cout<<"["<<x<<","<<y<<"] "
#define LOCAL = 1;
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int Max = 5e3 + ; ll n, sum;
ll win, draw; ll exgcd(ll a, ll b, ll &x, ll &y)
{
if (b == )
{
x = ;
y = ;
return a;
}
ll r = exgcd(b, a % b, x, y);
ll t = x;
x = y;
y = t - (a / b) * y;
return r;
} int main()
{
#ifdef LOCAL
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
scanf("%lld%lld%lld%lld", &n, &sum, &win, &draw);
ll x, y;
ll gcd = exgcd(win, draw, x, y); if ((sum % gcd) == )
{
ll mod = win / gcd;
y = ((y%mod)*(sum/gcd%mod)%mod + mod)%mod;
x = (sum - y * draw) / win;
if(x >= && y >= && x + y <= n)
{
printf("%lld %lld %lld\n",x,y,n-x-y);
return ;
}
}
printf("-1\n");
return ;
}
codeforces 1244C (思维 or 扩展欧几里得)的更多相关文章
- Codeforces 724C Ray Tracing 扩展欧几里得
吐槽:在比赛的时候,压根就没想到这题还可以对称: 题解:http://blog.csdn.net/danliwoo/article/details/52761839 比较详细: #include< ...
- 【扩展欧几里得】BAPC2014 I Interesting Integers (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【数论】【扩展欧几里得】Codeforces 710D Two Arithmetic Progressions
题目链接: http://codeforces.com/problemset/problem/710/D 题目大意: 两个等差数列a1x+b1和a2x+b2,求L到R区间内重叠的点有几个. 0 < ...
- [codeforces 200 E Tractor College]枚举,扩展欧几里得,三分
题目出自 Codeforces Round #126 (Div. 2) 的E. 题意大致如下:给定a,b,c,s,求三个非负整数x,y,z,满足0<=x<=y<=z,ax+by+cz ...
- Codeforces 7C 扩展欧几里得
扩展欧几里得是计算 ax + by = gcd(a,b) 的 x,y的整数解. 现在是ax + by + c = 0; 只要 -c 是 gcd(a,b) 的整数倍时有整数解,整数解是 x = x*(- ...
- AC Codeforces Round #499 (Div. 2) E. Border 扩展欧几里得
没想出来QAQ....QAQ....QAQ.... 对于一般情况,我们知道 ax+by=gcd(a,b)ax+by=gcd(a,b)ax+by=gcd(a,b) 时方程是一定有解的. 如果改成 ax+ ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)
http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...
- Codeforces7C 扩展欧几里得
Line Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Status ...
- hdu 5512 Pagodas 扩展欧几里得推导+GCD
题目链接 题意:开始有a,b两点,之后可以按照a-b,a+b的方法生成[1,n]中没有的点,Yuwgna 为先手, Iaka后手.最后不能再生成点的一方输: (1 <= n <= 2000 ...
随机推荐
- 使用python开发ansible自定义模块的简单案例
安装的版本ansible版本<=2.7,<=2.8是不行的哦 安装模块 pip install ansible==2.7 先导出环境变量 我们自定义模块的目录. 我存放的目录 export ...
- Asp.Net跨平台 Jexus 5.8.1 独立版
在Linux上运行ASP.NET网站或WebApi的传统步骤是,先安装libgdiplus,再安装mono,然后安装Jexus.在这个过程中,虽然安装Jexus是挺简便的一件事,但是安装mono就相对 ...
- C语言学习笔记6-数组
本系列文章由jadeshu编写,转载请注明出处.http://blog.csdn.net/jadeshu/article/details/50752170 作者:jadeshu 邮箱: jades ...
- scrapy框架之基础
一.安装scrapy 安装失败看博客>>>scrapy安装失败解决方案 pip install wheel pip install twisted pip install pywin ...
- 表单 Flask-WTF - 使用
1 配置 可以使用Flask-WTF来处理web表单,在使用之前要先配置下,打开config.py,编辑添加如下内容 WTF_CSRF_ENABLED = True SECRET_KEY = 'you ...
- Linux设备驱动程序 之 中断下半部
中断处理程序的局限 1. 中断处理程序以异步的方式执行,并且它有可能会打断其他重要代码的执行,因此,为了避免被打段的代码停止时间过长,中断处理程序应该执行的越快越好: 2. 如果当前有一个中断处理程序 ...
- Event---事件详解
1.焦点事件 焦点:使浏览器能够区分用户输入的对象,当一个元素有焦点的时候,那么他就可以接收用户的输入. 可以通过以下方式给元素设置焦点: 点击.tab.js 不是所有元素都能够接收焦点的,能够响应用 ...
- mysql密码设置为空怎么办?
由于很多童鞋安装使用MySQL时,安装时没有设置密码,或者像我一样图省事设置密码为空,想为其设置新密码: 1.点击 开始------>运行----在弹出的对话框中输入cmd 如下图: 2.使用 ...
- 在IIS7、IIS7.5中应用程序池最优配置方案
先介绍几个简单的设置 应用程序池 > 高级设置 1.基本设置 队列长度: 65535 启动32位应用程序 否则安装一些32的组建或32位的php都会出错 托管管道模式 Classsic 2.高级 ...
- 003-jdk-数据结构-HashMap、HashTable、ConcurrentHashMap、TreeMap、LinkedHashMap、Set
一.Map概述 Map:“键值”对映射的抽象接口.该映射不包括重复的键,一个键对应一个值. 1.1.HashTable[不常用] 基于“拉链法”实现的散列表. 底层数组+链表实现,无论key还是val ...