counting game
There are n people standing in a line, playing a famous game called ''counting ". when the game begins, the leftmost person says ''1 "loudly, then the second person (people are numbered 1 to n from left to right) says ''2 "loudly. this is followed by the 3rd person saying ''3 "and the 4th person says ''4", and so on. when the n -th person (I. e. the rightmost person) said'' n "loudly, the next turn goes to his immediate left person (I. e. the ( n -1) -th person ), who shoshould say '' n + 1 " loudly, then the ( n -2) -th person shoshould say '' n + 2 " loudly. after the leftmost person spoke again, the counting goes right again.
There is a catch, though (otherwise, the game wocould be very boring !) : If a person shoshould say a number who is a multiple of 7, or its decimal representation contains the digit 7, he shoshould clap instead! The following tables shows us the counting processN= 4('X' represents a clap). When the 3rd Person Claps for the 4th time, he's actually counting 35.
person |
1 |
2 |
3 |
4 |
3 |
2 |
1 |
2 |
3 |
action |
1 |
2 |
3 |
4 |
5 |
6 |
x |
8 |
9 |
person |
4 |
3 |
2 |
1 |
2 |
3 |
4 |
3 |
2 |
action |
10 |
11 |
12 |
13 |
x |
15 |
16 |
x |
18 |
person |
1 |
2 |
3 |
4 |
3 |
2 |
1 |
2 |
3 |
action |
19 |
20 |
x |
22 |
23 |
24 |
25 |
26 |
x |
person |
4 |
3 |
2 |
1 |
2 |
3 |
4 |
3 |
2 |
Action |
X |
29 |
30 |
31 |
32 |
33 |
34 |
X |
36 |
GivenN,MAndK, Your task is to find out, whenM-Th Person Claps forK-Th time, what is the actual number being counted.
Input
There will be at most 10 test cases in the input. Each test case contains three IntegersN,MAndK(
2N100,
1MN,
1K100) In a single line. The last test case is followed by a lineN=M=K= 0, Which shoshould not be processed.
Output
For each line, print the actual number being counted, whenM-Th Person Claps forK-Th time. If this can never happen, print'-1'.
Sample Input
4 3 14 3 24 3 34 3 40 0 0
Sample output
17212735
The seventh Hunan Collegiate Programming Contest
Problemsetter:Rujia Liu,Special thanks:Yiming Li & Jane Alam Jan
# Include < Stdio. h >
# Include < String . H >
Bool AA [ 1000000 ];
Bool Solve ( Int N)
{
If (N % 7 = 0 ) Return True ;
Int T = N;
While (T)
{
If (T % 10 = 7 ) Return True ;
T /= 10 ;
}
Return False ;
}
Void Calc ()
{
For ( Int I = 1 ; I < 1000000 ; I ++ )
{
If (Solve (I) AA [I] = True ;
Else AA [I] = False ;
}
}
Int Main ()
{
Int N, m, K;
Int T;
Int CNT;
Calc ();
While (Scanf ( " % D " , & N, & M, & K) ! = EOF)
{
If (N = 0 && M = 0 && K = 0 ) Break ;
T = M;
CNT = 0 ;
While ( 1 )
{
If (AA [T]) CNT ++ ;
If (CNT = K)
{
Printf ( " % D \ n " , T );
Break ;
}
If (N = M)
{
T + = 2 * (M - 1 );
Continue ;
}
If (M = 1 )
{
T + = 2 * (N - M );
Continue ;
}
T + = 2 * (N - M );
If (AA [T]) CNT ++ ;
If (CNT = K)
{
Printf ( " % D \ n " , T );
Break ;
}
T + = 2 * (M - 1 );
}
}
Return 0 ;
}