Misha and Vanya have played several table tennis sets. Each set consists of several serves, each serve is won by one of the players, he receives one point and the loser receives nothing. Once one of the players scores exactly k points, the score is reset and a new set begins.
Across all the sets Misha scored a points in total, and Vanya scored b points. Given this information, determine the maximum number of sets they could have played, or that the situation is impossible.
Note that the game consisted of several complete sets.
The first line contains three space-separated integers k, a and b (1 ≤ k ≤ 109, 0 ≤ a, b ≤ 109, a + b > 0).
If the situation is impossible, print a single number -1. Otherwise, print the maximum possible number of sets.
11 11 5
1
11 2 3
-1
Note that the rules of the game in this problem differ from the real table tennis game, for example, the rule of "balance" (the winning player has to be at least two points ahead to win a set) has no power within the present problem.
题目大意:
一场乒乓球比赛的规则是:谁先打到了K分,谁就赢了,并且此时双方比分清0.进行下一盘比赛。
已知两个人打比赛:A一共得到了a分,B一共得到了b分.问最多可能有多少场比赛。
思路:
1、问最多的比赛场次,我们肯定是贪心的去想,让每一场比赛尽量以K:0或者是0:K结束.
那么最多可能进行的比赛场数就是:a/k+b/k.
2、对于可能出现的情况:
我们此时求出a,b分别%k的余数,如果对应a%k!=0,我们就要在b某一场胜利的时候,将比分修改为a%k:k.那么此时如果a%k!=0&&B没有赢过.那么这就是一个不可能的情况。
同理,对于b%k!=0&&A没赢过的时候,也是不可能的情况。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
int k,a,b;
while(~scanf("%d%d%d",&k,&a,&b))
{
int tmpa=a/k;
int tmpb=b/k;
a=a%k;
b=b%k;
if(a>0&&tmpb==0)printf("-1\n");
else if(b>0&&tmpa==0)printf("-1\n");
else printf("%d\n",tmpa+tmpb);
}
}