【推理】UVa 10771 - Barbarian tribes

  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):

  1. 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).

  2. Content of the circle: G1 G3 K7 K5 K6

    Starting to count at K5.
    First sacrifice: K6.
    Second sacrifice: G3 (replaced by K8).

  3. Content of the circle: G1 K8 K7 K5

    Starting to count at K7.
    First sacrifice: K5.
    Second sacrifice: K8 (replaced by G9).

  4. Content of the circle: G1 G9 K7

    Starting to count at K7.
    First sacrifice: G1.
    Second sacrifice: K7 (replaced by K10).

  5. Content of the circle: G9 K10

    Starting to count at G9.
    First sacrifice: K10.
    Second sacrifice: G9 (replaced by K11).

  6. 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: 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.

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

开始以为是约瑟夫环,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 }
时间: 2024-10-10 02:51:14

【推理】UVa 10771 - Barbarian tribes的相关文章

uva 1500 - Alice and Bob(推理)

题目连接:uva 1500 - Alice and Bob 题目大意:在黑板上又一个序列,每次操作可以选择一个数减1,或者是合并两个数,一个数被减至1则自动消除,不能操作者输. 解题思路:结论,对于大于1的数可以看成是一个整数s,为消除他们的总操作步数,包括减1以及合并,c为列中1的个数,如果s>2的话,c或者是s为奇数则为必胜,否则必败.若s≤2的话(s=2或者s=0)是,判断c是否为3的倍数,是的话必败,不是的话必胜. 证明:s>2时,s和c均为偶数是为必败态. s为奇数,c为偶数:先手操

UVA 1372 - Log Jumping(推理)

题目链接:1372 - Log Jumping 题意:给定一些n个木板的起始位置和长度k,相重叠的木板可以互相跳跃,求能构成环的最大数量. 思路:先按起始位置排序,然后每次多一个木板就去判断他和前一个和前前一个能不能互相跳跃,如果可以的话就可以多加上这个木板. 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #define max(a,b) ((a)

UVA 11246 - K-Multiple Free set(数论推理)

UVA 11246 - K-Multiple Free set 题目链接 题意:一个{1..n}的集合,求一个子集合,使得元素个数最多,并且不存在有两个元素x1 * k = x2,求出最多的元素个数是多少 思路:推理一下, 一开始n个 先要删除k倍的,删除为{k, 2k, 3k, 4k, 5k, 6k...},会删掉多余的k^2,因此在加回k^2倍的数 然后现在集合中会出现情况的只有k^2的倍数,因此对k^2倍的数字看成一个新集合反复做这个操作即可,因此最后答案为n - n / k + n /

uva 11892 - ENimEN(推理)

题目链接:uva 11892 - ENimEN 题目大意:给定n堆石子的个数,两人轮流选择石子堆取石子,直到不能取为失败,附加条件,如果前一次操作,即队手的操作,没有将选中石子堆中的石子取完,那么当前操作者必须在该堆中取石子. 解题思路:只要有一个石子堆的个数大于2,那么先手就获得必胜态,可控.对于全是1的情况判断奇偶性. #include <cstdio> #include <cstring> #include <algorithm> using namespace

uva 1561 - Cycle Game(推理)

option=com_onlinejudge&Itemid=8&page=show_problem&problem=4336" style="">题目链接:uva 1561 - Cycle Game 题目大意:给出一个环,每次从起点開始,能够选择一个权值非0的边移动,移动后减掉权值至少1点. 不能移动的为失败. 解题思路: 1:有0的情况,假设有方向离权值为0的边的步数为奇数,则为必胜.否则必败. 2:无0的情况,奇数边必胜: 3:有1的情况.同

UVA - 11892 ENimEN (推理)

Description  ENimEN  In deterministic games no chance is involved, meaning that the final result can be predicted from the initial arrangement assuming players play optimal. These games are so boring. piloop and poopi are professional gamers. They pl

UVA 11024 - Circular Lock(数论+推理)

UVA 11024 - Circular Lock 题目链接 题意:给定一个矩阵,每次能在一行或者一列都加1,问能否构造出满足每个位置%P都等于0的矩阵,P的得到方法为矩阵p所有数字的gcd 思路:推公式啊,一共4个加值的方法,分别为A,B,C,D A + C 加到A位置上a + k1 p,a为原位置差多少为p的倍数 同理 A + D 加到A位置上b + k2 p B + C 加到A位置上c + k3 p B + D 加到A位置上d + k4 p 这样一来消掉就得到a - b - c + d +

【推理,贪心】UVa 1319 - Maximum

看到了大神的代码.理解了好久...真是差距. 题意:给出m, p, a, b,然后xi满足已下两个公式, 求 xp1 + xp2 +...+ xpm 的最大值. 1.-1/sqrt(a) <= xi <= sqrt(a); (a>0) 2.x1+x2+...+xm = b*sqrt(a); 注意:p为偶数. 解题思路:因为p为偶数,所以sqrt(a)和-1/sqrt(a)的p次方都为正数且sqrt(a) > 1/sqrt(a).所以贪心思想时尽量先取sqrt(a);当已经取的xi的

【置换,推理】UVa 1315 - Creaz tea party

Dsecription n participants of «crazy tea party» sit around the table. Each minute one pair of neighbors can change their places. Find the minimum time (in minutes) required for all participants to sit in reverse order (so that left neighbors would be