题解:CF1776L Controllers
CF1776L Controllers 题解
分析
先把题目形式化。设 \(n\) 次加减中有 \(x\) 个加,\(y\) 个减,其中 \(a\) 加了 \(u\) 次,减了 \(v\) 次,显然 \(b\) 加了 \(x-u\) 次,减了 \(y-v\) 次。题目则要求 \(u\cdot a-v\cdot a+(x-u)\cdot b-(y-v)\cdot b=0\),转换一下得 \((u-v)\cdot a=-(x-y)\cdot b+(u-v)\cdot b\),把右边的 \((u-v)\cdot b\) 拎到左边,得 \((u-v)\cdot (a-b)=-(x-y)\cdot b\)。
接下来就是要解这个式子,分两种情况:
- 当 \(a-b\) 为 \(0\) 时,\(x-y\) 必为 \(0\),否则无解。
- 当 \(a-b\) 不为 \(0\) 时,显然 \(u-v= \dfrac {-(x-y)\cdot b }{a-b}\)。很明显,\(0 \le u \le x\),\(0 \le v \le y\),通过这两个不等式解得 \(-y \le u-v \le x\),
代码内判断这两种情况即可。
示例代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
namespace Raiden
{
int n;
signed work()
{
cin >> n;
int x = 0, y = 0;
for (int i = 1; i <= n; i++)
{
char c;
cin >> c;
if (c == '+')
{
x++;
}
else
{
y++;
}
}
int T;
cin >> T;
while (T--)
{
int a, b;
cin >> a >> b;
if (a == b)
{
if (x == y)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
continue;
}
int m = x - y;
if (b * m % (b - a))
{
cout << "NO" << endl;
continue;
}
long long k = b * m / (b - a);
if (-y <= k && k <= x)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
return 0;
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
return Raiden::work();
}
题解:CF1776L Controllers的更多相关文章
- ASP.NET Core 中文文档 第四章 MVC(4.1)Controllers, Actions 和 Action Results
原文:Controllers, Actions, and Action Results 作者:Steve Smith 翻译:姚阿勇(Dr.Yao) 校对:许登洋(Seay) Action 和 acti ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- 【转】Controllers and Routers in ASP.NET MVC 3
Controllers and Routers in ASP.NET MVC 3 ambilykk, 3 May 2011 CPOL 4.79 (23 votes) Rate: vote 1vote ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
随机推荐
- Vue 子组件修改父组件传递过来的值
实现效果:通过点击选中的按钮控制左边的树是否进行展示 子组件篇: <el-button v-if="isShowTree&hasTree" type="te ...
- AWS EC2 实例类型命名规则
AWS EC2(Elastic Compute Cloud)实例类型的命名规则反映了实例的性能特征.用途和硬件配置.这些实例类型的名称由几个组件构成,每个组件都提供了关于该实例类型特定方面的信息.理解 ...
- SQL Server使用脚本实现自动备份
因服务器安装的SQL Server版本不支持自动定时备份,需自行实现,大概思路为: 创建备份数据库的脚本 创建批处理脚本执行步骤一中的脚本 创建Windows定时任务执行步骤二中的脚本 1. 创建SQ ...
- Unocss使用
目录: 安装 简单使用 自定义规则 安装 { "dependencies": { "unocss": "^0.55.6", "vu ...
- 解决向github上push报 error: failed to push some refs to 'xxxxx' 问题
解决向github上push报 error: failed to push some refs to 'xxxxx' 问题 1.问题 向github上push 代码时,报 error: failed ...
- Angular Material 18+ 高级教程 – Custom Themes for Material Design 2 (自定义主题 Material 2)
v18 更新重要说明 从 Angular Material v18 开始,默认使用的是 Material 3 Design (简称 M3),本篇教的是旧版本的 Material 2 Design (简 ...
- Facebook – Pixel
介绍 Facebook 的 Pixel 类似于 Google 的 Analytics. 作用是 tracking 流量进入网站后是否转化. 这样就可以持续优化广告效果. 参考 Facebook Doc ...
- JavaScript – Proxy
参考 阮一峰 – Proxy 介绍和使用 Proxy 的作用是代理对象, 消费者不直接使用对象, 而是使用代理对象. 一般上做代理的目的就是想拦截对象访问做一些别的事情. 比如当对象改变以后, 同步 ...
- "放开那代码让我来!"——Cursor帮你写代码的奇妙之旅
让我们开门见山:编程很酷,但也很折磨人.那些长时间盯着屏幕,debug无休止的日子,只有程序员懂得它的酸爽.而就在这个编程焦虑的世界中,Cursor横空出世,带着一系列魔法功能,如同你手中的一根智能魔 ...
- php7新内容总结(随时更新)
一.参数和返回值类型申明 可以申明的有:float,int,bool,string,interfaces,array,callable 一般模式: function sum(int ...$ints) ...