Poj 1426 find the multiple (the idea of searching BFS + same modulus theorem + binary tree + 01 Harman coding)

Source: Internet
Author: User
Tags modulus

Http://poj.org/problem? Id = 1426

Find the multiple
Time limit: 1000 ms   Memory limit: 10000 K
Total submissions: 14519   Accepted: 5893   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

Ideas:

The result here is only 01, so the first thought is to use the binary tree structure,

And because the result is a 01 sequence, can we * 10. Obtain K for each bit. ConvertThe operation of mod 2 obtains each bit of K (0 or 1) What about it?

The answer is yes.

First, we use Same modulus Theorem Optimize the method for getting the remainder

(A * B) % N = (a % N * B % N) % N

(A + B) % N = (a % N + B % N) % N

 

Code1:

 1 # Include <iostream> 2 # Include <stdio. h> 3 # Include < String . H> 4 # Include <queue> 5   Using   Namespace STD;  6   7   Int MoD [ 600000  ];  8   Int Res [ 200  ];  9   10   Int  Main ()  11   {  12      Int  N;  13       While (~ Scanf ( "  % D  " , & N )&& N)  14   {  15 MoD [ 1 ] = 1 % N; //  Initialization. The maximum digit of N must be 1.  16          Int  I;  17   18           For (I = 2 ; Mod [I- 1 ]; I ++ )  19   {  20               //  Using the same modulus theorem, the remainder mod [I/2] in the previous step gets the remainder mod [I] in the next step.  21               //  MoD [I/2] * 10 + I % 2 simulates the BFS double-entry search 22 MoD [I] = (mod [I/ 2 ] * 10 + I % 2 ) % N;  23   }  24           Int Id = 0  ;  25 I -- ;  26           While (I)//  The same as the Harman encoding, the root node's 01 Encoding  27   {  28 Res [ID ++] = I % 2  ;  29 I/= 2  ;  30   }  31           For (I = ID- 1 ; I> = 0 ; I --) //  Inverted output  32   {  33 Printf ( "  % D  "  , Res [I]);  34   }  35 Putchar ( 10  );  36   } 37       Return   0  ;  38 }

It seems that I think too much (the code below is actually AC)

Code 2:

 1 # Include <iostream> 2 # Include <stdio. h> 3 # Include < String . H> 4 # Include <queue> 5   Using   Namespace STD;  6   7 _ Int64 mod [ 600000  ];  8   9   Int  Main ()  10   {  11       Int  N;  12       While (~ Scanf ( " % D  " , & N )&& N)  13   {  14           Int  I;  15           For (I = 1 ; I ++ )  16   {  17 MoD [I] = mod [I/ 2 ] *10 + I % 2  ;  18               If (Mod [I] % N = 0  )  19   {  20                   Break  ;  21   }  22   }  23 Printf ( "  % I64d \ n  "  , MOD [I]);  24   }  25       Return   0  ;  26 }

 

I cannot figure out why BFS-Based Direct Writing times out. So many optimizations can be achieved through array simulation ?????

Attached timeout BFS Code (Code 3) "the following code poj times out ":

 1 # Include <iostream> 2 # Include <stdio. h>3 # Include < String . H> 4 # Include <queue> 5   Using   Namespace  STD;  6   7 _ Int64 BFS ( Int  M)  8   {  9 Queue <__ int64> Q; 10   _ Int64 temp;  11 Q. Push ( 1  );  12       While (! Q. Empty ())  13   {  14 Temp = Q. Front ();  15   Q. Pop ();  16           If (TEMP % m =0  )  17               Return  Temp;  18 Q. Push (temp * 10 + 1  );  19 Q. Push (temp * 10  );  20   }  21       Return  0  ;  22   }  23   24   Int  Main ()  25   {  26       Int  N;  27       While (~ Scanf ( "  % D  " , & N )&& N)  28   {  29 Printf ( "  % I64d \ n  "  , BFs (n ));  30   }  31       Return   0  ;  32 }

 

 

 

 

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.