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了,那个 ...
随机推荐
- 【Codevs1227】方格取数2(费用流)
题意:给出一个n*n的矩阵,每一格有一个非负整数Aij,(Aij <= 1000) 现在从(1,1)出发,可以往右或者往下走,最后到达(n,n),每达到一格,把该格子的数取出来,该格子的数就变成 ...
- 集合-Vector
Vector中的操作是线程安全的. public Vector(int initialCapacity, int capacityIncrement) { super(); if (initialCa ...
- bash变量类型详解
本地变量:作用于当前shell,对当前shell之外的其他shell进程和当前shell子进程均无效. 本地变量赋值为 name='value' value可以是字符串或者是变量,引用变量使用${na ...
- P2007 魔方
洛谷——P2007 魔方 题目背景 常神牛从来没接触过魔方,所以他要借助计算机来玩.即使是这样,他还是很菜. 题目描述 常神牛家的魔方都是3*3*3的三阶魔方,大家都见过. (更正:3 4以图为准.) ...
- Chrome 浏览器安装Vue插件方法 (十分详细)
博主最近在研究Vue,无奈新手想安装Chrome的Vue插件,整理下安装流程: 1.首先去github下载vue.zip文件插件(还有npm安装方法这里就不介绍了自行百度)下载地址:https://g ...
- 10.Java web—JavaBean
定义一个类,然后在jsp页面通过<jsp:useBean>标签调用 重点是类属性名要起得规则,一般是setXXX getXXXX 新建一个类UserInfo public class U ...
- 【转】Spring框架深入理解
参考这篇文章: http://www.ibm.com/developerworks/cn/java/j-lo-spring-principle/ Spring内部分为Beans, Context 和 ...
- OC - 读歌词
类的头文件: #import <Foundation/Foundation.h> //FILE_PATH是文件名称. #define FILE_PATH @"/Users/qia ...
- [转]Linux上程序执行的入口--Main
main()函数,想必大家都不陌生了,从刚开始写程序的时候,大家便开始写main(),我们都知道main是程序的入口.那main作为一个函数,又是谁调用的它,它是怎么被调用的,返回给谁,返回的又是什么 ...
- insserv: warning: script 'lampp' missing LSB tags and overrides
https://ubuntuforums.org/showthread.php?t=2327011 1.方法一,编辑rc.loacl脚本 Ubuntu开机之后会执行/etc/rc.local文件中的脚 ...