标签:
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: n, m 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 n, m and k, 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):
Input consists of zero ore more test cases. Each test case consists of a line with three positive integers: n, m and k. You can assume 1n + m2000 and 1k1000. A test case with n = m = k = 0 ends the input and must not be processed.
For every test case, print either "Gared" or "Keka" as convenient.
3 3 2 4 2 2 0 1 7 0 0 0
Keka Gared Keka
开始以为是约瑟夫环,TLE了...郁闷半天,后来看了题解恍然大悟。自己还是得加强下思维转换。。。
题目大意:给出n,m和k,有n个G,m个K,站成一个圈,现在有个杀手每次走k步,杀掉当前位置的人,每次杀两个人之后如果这两个人都是G或都是K,就用G补上,否则就用K补上。问说最后剩一个谁。
解题思路:在每杀两个人这个地方进行考虑。无非3种情况:杀两G,多一G,杀两K,多一G,杀一G一K,多一K。
注意,杀一G一K,多一K时,K的数目不变,所以K的人数只会以减2的方式减少。如果K一开始是奇数的话是永远减少不完的,最终肯定剩下K。如果一开始是偶数,最终减少完的必然是K,剩下G。
代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 using namespace std; 6 int main() 7 { 8 int n, m, k; 9 while(scanf("%d%d%d", &n, &m, &k)) 10 { 11 if(!n && !m && !k) break; 12 if(m%2) printf("Keka\n"); 13 else printf("Gared\n"); 14 } 15 return 0; 16 }
【推理】UVa 10771 - Barbarian tribes
标签:
原文地址:http://www.cnblogs.com/LLGemini/p/4305117.html