Title Address:
http://acm.hdu.edu.cn/showproblem.php?pid=1393
Topic Description:
Weird Clock Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 2639 accepted Submission (s): 945
Problem Description a weird clock marked from 0 to the has only a minute hand. It won ' t move until a special coin are thrown into its box. There are different kinds of coins as your options. However Once you do your choice, you cannot use the any other kind. There are infinite number of coins of each kind, all marked with a number D (1 <= d <= 1000), meaning so this C Oin'll make the minute hand the "D times clockwise the" times. For example, if it is, and d = 2. Then the minute hand'll move clockwise the minutes and would be pointing to 15.
Now, are given the initial time S (1 <= s <=) and the coin ' s type D. Write a program to find the minimum num ber of D-coins needed to turn the minute hand back to 0.
Input There are several tests. Each test occupies a line containing two positive integers s and d.
The input is finished by a line containing 0 0.
Output for each test print in a single line the minimum number of coins needed. If It is impossible to turn the hand back to 0, output "impossible".
Sample Input
30 1 0 0
Sample Output
1
The following:
Judge the shortest turn a few d times can be just 60 minutes.
The puzzle :
Simulation, and with the idea of indexing, repeats that not 60 of the clock will never reach 60.
Code:
#include <stdio.h>
#include <string.h>
bool visited[61] = {false};
int s=0,d=0;
int main ()
{while
(scanf ("%d%d", &s,&d)!=eof&& (s+d) >0)
{
memset (visited,false , sizeof (visited));
Visited[60]=true;
Visited[s]=true;
int cnt=0;
while (s!=)
{
s=s+s*d;
cnt++;
while (s>60) s-=60;
if (Visited[s]) break;
else visited[s] = true;
}
if (s==60) printf ("%d\n", CNT);
else printf ("impossible\n");
}
return (0);
}