您的位置:首页 > 其它

UVA 10771 - Barbarian tribes(推理)

2014-01-08 18:23 369 查看


Barbarian tribes 
In a lost land two primitive tribes coexist: Gareds and Kekas. Every summer solstice they meet and compete to decide which tribe will be the favorite of the gods for the rest of the year, following an old ritual:

First, a local guru chooses three numbers at random: nm and k.
Afterwards, n Gared maids (in the positions 1, 2,..., n) and m Keka
maids (in the positions n + 1, n + 2,..., n + m) are placed in a circle looking inwards. Then the guru begins to count 1,
2,..., k starting at the first Gared maid. When the k-th maid is reached, she is immediately sacrificed to the gods. The guru then counts again 1,
2,..., k starting at the maid following the one just sacrificed. Again, the k-th maid reached this way is sacrificed. After every two sacrifices,
the second sacrificed maid is replaced by a new maid. In order to decide the tribe of the new maid, the guru looks at the heads of the two maids just killed (nothing else remains of them). If both heads are of the same tribe, the guru calls a Gared maid. If
the heads are from different tribes, the guru calls a Keka maid. The process then begins again (counting and sacrificing twice and replacing once) starting to count at the maid following the new maid just added to the circle. Since the number of maids reduces
by one after every step (of two sacrifices and one replacement), after n + m - 1 steps only one maid remains.

According to the tradition, the tribe of the last maid will be the favorite of the gods. (What the guru does to the last maid is something you don't want to know.) Anyway, write a program such that, given nm andk,
writes the name of the fortunate tribe.

For example, this is what happens for n = m = 3 and k =
2 (a ``G'' denotes a Gared maid and a ``K'' denotes a Keka maid; the subindexes mark the order the maids enter the circle):
Initial content of the circle: G1 G2 G3 K4 K5 K6 

Starting to count at G1. First sacrifice: G2. Second sacrifice: K4 (replaced
by K7).
Content of the circle: G1 G3 K7 K5 K6 

Starting to count at K5. First sacrifice: K6. Second sacrifice: G3 (replaced
by K8).
Content of the circle: G1 K8 K7 K5 

Starting to count at K7. First sacrifice: K5. Second sacrifice: K8 (replaced
by G9).
Content of the circle: G1 G9 K7 

Starting to count at K7. First sacrifice: G1. Second sacrifice: K7 (replaced
by K10).
Content of the circle: G9 K10 

Starting to count at G9. First sacrifice: K10. Second sacrifice: G9 (replaced
by K11).
Final content of the circle: K11 

Input 

Input consists of zero ore more test cases. Each test case consists of a line with three positive integers:nm and k.
You can assume 1

n + m

2000 and 1

k

1000.
A test case with n = m = k = 0 ends the input and must not be processed.

Output 

For every test case, print either "Gared" or "Keka" as convenient.

Sample Input 

3 3 2
4 2 2
0 1 7
0 0 0


Sample Output 

Keka
Gared
Keka

题意:n个G,m个K,站成一圈1-n为G,n-m为K,然后现在有个k,每次走k步杀死一个人,每杀死两个人,进行一个操作,如果杀死的两个人不同,则在第二个被杀的人位置用K顶替,如果相同用G顶替。求最终剩下的是G还是K

思路:不错的一题,很容易把人引入误区以为是约瑟夫环类的变形(我也是悲剧了想错了想破头了- -,后来看了下讨论版恍然大悟。。),在每杀两个人这个地方进行考虑。无非3种情况:杀两G,多一G,杀两K,多一G,杀一G一K,多一K。那么注意,K的人数只会以减2的方式减少。所以K一开始是奇数的话是永远减少不完的,最终肯定剩下K。如果一开始是偶数,最终减少完的必然是K,剩下G。

代码:

#include <stdio.h>
#include <string.h>

const char Name[2][10] = {"Gared", "Keka"};
int n, m, k;

int main() {
while (scanf("%d%d%d", &n, &m, &k) && n || m || k) {
printf("%s\n", Name[m % 2]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: