codeforces 451C. Predict Outcome of the Game 解题报告
题目链接:http://codeforces.com/problemset/problem/451/C
题目意思:有3支球队(假设编号为1、2、3),总共要打 n 场比赛,已知已经错过这n场比赛中的 k 场,但从 k 场比赛中可以获取一些信息:设w1表示 k 场比赛中编号为1的球队赢了w1场比赛(w2、w3 类似),绝对值 |w1-w2| = d1, |w2-w3| = d2,问能否通过设置n-k 的 赛果来使得n场比赛都打完后,编号为1的球队的胜利次数 == 编号为2的球队的胜利次数 == 编号为3的球队的胜利次数。注意:每场比赛的赛果只有胜和输之分,没有平局。
初时看到这道题目,完全没有思路,当看了Tutorial 之后(不过没仔细看他的solution),就大概知道怎么做了,贡献了两个wa......
首先对于n,如果不能被 3 整除,那就肯定怎么分配剩下的 n-k 场赛果都于事无补的了,因为最终 编号为1的球队的胜利次数 == 编号为2的球队的胜利次数 == 编号为3的球队的胜利次数 == n / 3 嘛
其次,要得出一条隐含的方程(关键所在): w1 + w2 + w3 = k! (1)
然后结合 |w1-w2| = d1 (2), |w2-w3| = d2 (3) 来得出w1、 w2、 w3的值。(绝对值可以通过w1,w2,w3的大小关系来去掉,有四种情况)
(1)w1 >= w2,w2 >= w3 (2)w1 >= w2,w2 < w3
——> w1 = (2d1 + d2 + k) / 3 ——> w1 = (2d1 - d2 + k) / 3
w2 = (-d1 + d2 + k) / 3 w2 = (-d1 - d2 + k) / 3
w3 = (-d1 - 2d2 + k) / 3 w3 = (-d1 + 2d2 + k) / 3
(3)w1 < w2,w2 >= w3 (4)w1 < w2,w2 < w3
——> w1 = (-2d1 + d2 + k) / 3 ——> w1 = (-2d1 - d2 + k) / 3
w2 = ( d1 + d2 + k ) / 3 w2 = ( d1 - d2 + k ) / 3
w3 = ( d1 - 2d2 + k) / 3 w3 = ( d1 + 2d2 + k) / 3
可以知道,x1与x2的大小关系只会影响 d1 的符号,x2与x3的大小关系只会影响 d2 的符号。
那么可以枚举d1与d2的符号(当然不可能是0啦),算出相应的x1, x2, x3 的大小,看看每个xi (1 <= i <= 3 ) 是否满足 0 <= xi <= n/3 (前提是n%3 == 0!下界0一定要加上,因为算出来有可能是负数啊),还有一点就是,由于涉及除法,4 / 3 == 1 这些情况要考虑到,一个可以避免的方法是,计算的时候,分子要判断是否能被3整除,不能就continue 咯。
总的来说,这题是纯数学题!
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; __int64 n, k, d1, d2; int main()
{
int t;
while (scanf("%d", &t) != EOF)
{
while (t--)
{
scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2);
if (n % )
printf("no\n");
else
{
int flag = ;
for (int s1 = -; s1 <= && !flag; s1++) // 控制d1的正负号
{
for (int s2 = -; s2 <= && !flag; s2++) // 控制d2的正负号
{
if (s1 == || s2 == )
continue;
__int64 w1, w2, w3;
__int64 f1, f2, f3; // w1, w2, w3的分子
f1 = (*(s1)*d1 + (s2)*d2 + k);
if (f1 % )
continue;
w1 = f1 / ;
f2 = ((-)*(s1)*d1 + (s2)*d2 + k);
if (f2 % )
continue;
w2 = f2 / ;
f3 = ((-)*(s1)*d1 + *(-)*(s2)*d2 + k);
if (f3 % )
continue;
w3 = f3 / ; if (w1 + w2 + w3 == k && w1 >= && w1 <= n/ && w2 >= && w2 <= n/ && w3 >= && w3 <= n/)
{
flag = ;
break;
}
}
}
printf("%s\n", flag ? "yes" : "no");
}
}
}
return ;
}
codeforces 451C. Predict Outcome of the Game 解题报告的更多相关文章
- CodeForces 451C Predict Outcome of the Game
Predict Outcome of the Game Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d &a ...
- codeforces 258div2C Predict Outcome of the Game
题目链接:http://codeforces.com/contest/451/problem/C 解题报告:三个球队之间一共有n场比赛,现在已经进行了k场,不知道每个球队的胜场是多少,如三个球队的胜场 ...
- codeforces 814B.An express train to reveries 解题报告
题目链接:http://codeforces.com/problemset/problem/814/B 题目意思:分别给定一个长度为 n 的不相同序列 a 和 b.这两个序列至少有 i 个位置(1 ≤ ...
- codeforces 558B. Amr and The Large Array 解题报告
题目链接:http://codeforces.com/problemset/problem/558/B 题目意思:给出一个序列,然后找出出现次数最多,但区间占用长度最短的区间左右值. 由于是边读入边比 ...
- codeforces 515B. Drazil and His Happy Friends 解题报告
题目链接:http://codeforces.com/problemset/problem/515/B 题目意思:有 n 个 boy 和 m 个 girl,有 b 个 boy 和 g 个 girl ( ...
- codeforces 514B. Han Solo and Lazer Gun 解题报告
题目链接:http://codeforces.com/problemset/problem/514/B 题目意思:给出双头枪的位置(x0, y0),以及 n 个突击队成员的坐标.双头枪射击一次,可以把 ...
- codeforces 471C.MUH and House of Cards 解题报告
题目链接:http://codeforces.com/problemset/problem/471/C 题目意思:有 n 张卡,问能做成多少种不同楼层(floor)的 house,注意这 n 张卡都要 ...
- codeforces C. Vasily the Bear and Sequence 解题报告
题目链接:http://codeforces.com/problemset/problem/336/C 题目意思:给出一个递增的正整数序列 a1, a2, ..., an,要求从中选出一堆数b1, b ...
- codeforces A. Vasily the Bear and Triangle 解题报告
题目链接:http://codeforces.com/problemset/problem/336/A 好简单的一条数学题,是8月9日的.比赛中没有做出来,今天看,从pupil变成Newbie了,那个 ...
随机推荐
- hadoop+yarn+hbase+storm+kafka+spark+zookeeper)高可用集群详细配置
配置 hadoop+yarn+hbase+storm+kafka+spark+zookeeper 高可用集群,同时安装相关组建:JDK,MySQL,Hive,Flume 文章目录 环境介绍 节点介绍 ...
- ORACLE 内部原理
http://www.ohsdba.cn/index.php?m=Article&a=index&id=46 内部原理 2016-05-04• 如何使用BBED 2016-04-16• ...
- 【spring data jpa】报错如下:Caused by: javax.persistence.EntityNotFoundException: Unable to find com.rollong.chinatower.server.persistence.entity.staff.Department with id 0
报错如下: org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.rollong.chi ...
- BUPT复试专题—统计字母(2008)
题目描述 给定一个只有小写英文字母组成的字符串,串长为n.请你编写程序求出这个字符串中出现次数最多的字母. 输入 输入的第一行为t(0 < t < 10),表示有t组测试用例.对于每组测试 ...
- python—networkx:依据图的权重绘图
首先输入边和边的权重,随后画出节点位置.依据权重大小划分实边和虚边 #coding:utf-8 #!/usr/bin/env python """ An example ...
- 第04章-VTK基础(3)
[译者:这个系列教程是以Kitware公司出版的<VTK User's Guide -11th edition>一书作的中文翻译(出版时间2010年,ISBN: 978-1-930934- ...
- Linux C++的多线程编程(转)
1. 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的Unix也支持线程的概念,但是在一个进程(proces ...
- candy——动态规划
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- Yii Criteria常用方法(select,join,where,日期,)
$criteria = new CDbCriteria; //select $criteria->select = '*';//默认* $criteria->select = 'id,n ...
- CloudStack系统虚拟机启动但不在Host表中
有网友问到这个问题,CloudStack中,系统虚拟机正常启动,Running状态. 可是在host表中没有对应项,上传下载模板等功能也不正常. 原因:系统虚拟机启动之后,会通过管理网段主动连接man ...