Poj1426 -- find the multiple (broad search, IQ questions)

Source: Internet
Author: User
Find the multiple
Time limit:1000 ms   Memory limit:10000 K
Total submissions:18527   Accepted:7490   Special Judge

Description

Given a positive integer N, write a program to find out a nonzero Multiple m of N whose decimal representation contains only the digits 0 and 1. you may assume that N is not greater than 200 and there is a corresponding M containing no more than 100 decimal digits.

Input

The input file may contain in multiple test cases. Each line contains a value of N (1 <=n <= 200). A line containing a zero terminates the input.

Output

For each value of N in the input print a line containing the corresponding value of M. the decimal representation of M must not contain more than 100 digits. if there are multiple solutions for a given value of N, any one of them is acceptable.

Sample Input

26190

Sample output

10100100100100100100111111111111111111

If you see this in the wide search question, you cannot think of wide search ,,,,,

Each digit can only be 0 or 1, so the multiple of N is searched from the first digit until it is found.

The first digit must be 1, and the remainder is saved as temp. If the next digit is 1, (temp * 10 + 1) % N returns a new remainder. If the first digit is 0, then (temp * 10) % N gets the remainder, so that the search is wide and the size is 2 ^ 100

Pruning method: a maximum of 200 remainder values can be obtained for each request. It is good to see each remainder if it appears once.

 

 

#include <cstdio>#include <cstring>#include <algorithm>using namespace std ;struct node{    int k , temp ;    int last ;}p[1000000] , q ;int flag[210] , a[120] , n ;int bfs(){    int low = 0 , high = 0 ;    p[high].k = 1 ;    p[high].temp = p[high].k % n ;    flag[p[high].temp] = 1 ;    p[high++].last = -1 ;    while( low < high )    {        q = p[low++] ;        if( q.temp == 0 )            return low-1 ;        if( !flag[ (q.temp*10+1)%n ] )        {            p[high].k = 1 ;            p[high].temp = (q.temp*10+1)%n;            flag[ p[high].temp ] = 1 ;            p[high++].last = low-1 ;        }        if( !flag[ (q.temp*10)%n ] )        {            p[high].k = 0 ;            p[high].temp = (q.temp*10)%n ;            flag[ p[high].temp ] = 1 ;            p[high++].last = low-1 ;        }    }    return -1 ;}int main(){    int i , j ;    while(scanf("%d", &n) && n)    {        memset(flag,0,sizeof(flag));        i = 0 ;        j = bfs();        while( j != -1 )        {            a[i++] = p[j].k ;            j = p[j].last ;        }        for(j = i-1 ; j >= 0 ; j--)            printf("%d", a[j]);        printf("\n");    }    return 0;}

Poj1426 -- find the multiple (broad search, IQ questions)

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.