Hoj 1006 Weird CLOCK_ data structure

Source: Internet
Author: User

Topic Description:

A weird clock marked from the 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 (0 <= 1000), meaning that this coin would Make is the minute hand move D-clockwise the current time. 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 (0 <= 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

1
0 0


Sample Output

1

Ideas for solving problems: First in the study of other people's problem-solving report learned to judge whether the end of the data 0 0 o'clock simple method; This problem can not use the exhaustive method has been enumerated, so that can not judge impossible, will only fall into the dead cycle, so the simple way is to use an array of minutes to point to the number When the point is that the loop can not point to 0 is impossible; there is a more ingenious way to use 60 of the factor decomposition, 60=2*2*3*5, each time now = ((d + 1) * Today)% 60, if you can, then up to 2 times can reach 0, Or you'll never get there.

Law II:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main ()
{
    int now,d,i,j,n = 0;
    int num[60] = {0};
    int answer[60] = {0};

    while ((scanf ("%d", &now,&d) = = 2) && (now | | d))
    {
        if (now = = 0)
        {
            Answer[n] = 0;
            Continue;
        }
        if (d = = 0)
        {
            Answer[n] =-1;
            Continue;
        }
        for (i = 1;;; i++)
        {
            J = ((i * d) *now+now)%60;
            if (j = = 0)
            {
                Answer[n] = i;
                break;
            if (Num[j])
            {
                Answer[n] =-1;
                break;
            else num[j] = 1;
            }}
        n++;
    }
    for (i = 0;i < n;i++)
    {
        if (answer[i] = = 1) printf ("impossible!\n");
        else printf ("%d\n", Answer[i]);

    return 0;
}


Law II:

#include <stdio.h>

int main ()
{
    int d, TMP;
    int num;

    while (scanf ("%d%d", &tmp, &d) = = 2 && (tmp | | d))
    {
        if (tmp = = 0)
        {
            printf ("0\n");
            Continue;
        }
        if (d = = 0)
        {
            printf ("impossible\n");
            Continue;
        }

        for (num = 1; num <= 2; num++)
        {
            tmp = ((d + 1) * tmp)%;
            if (TMP = = 0) break
                ;

        if (TMP = = 0)
            printf ("%d\n", num);
        else
            printf ("impossible\n");
    }

    return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.