Question: given a positive integer N, evaluate a minimum positive integer m (M> 1) so that the decimal representation of N * M contains only 1 and 0.
To solve this problem, first consider whether such M must exist for any n. It can be proved that M exists and is not unique.
Simple Proof: Because
This is an infinite series, but each value in the series is between [0, N-1. Therefore, the infinite series must have cyclic knots in the middle. That is, if there is S, T is a positive integer, and S <t, there is. The cycle length is t-s. So 10 ^ s = 10 ^ t. Therefore:
, So
For example, if n = 3 is used, because any non-negative modulo 3 of 10 is 1, the cycle of the cyclic section is 1. There are:
Given N, evaluate M:
Method 1Given N, let m start from 2, enumerate m values until a m is met so that the decimal representation of N * m is only 1 and 0.
Method 2: Obtain the remainder sequence of the nth power sequence modulo n and find the cyclic section. Then search for the remainder sequence. The search aims to find some numbers in the remainder sequence so that their sum is a multiple of N. For example, n = 13. The sequence is 1, 10, 9, 12, 3, 4, and then repeats continuously. Obviously, 1 + 12 = 13, while 1 is the 0 power of 10, 12 is the 3 power of 10, so this number is 1000 + 1 = 1001, M is 1001/13 = 77.
Method 3: Because the value of N * m is 11,100,101,110,111,... so search in this space directly, this is an improvement to method 1. Search for this sequence until you find a number that can be divisible by N, which is N * m, and then calculate M. For example, if n = 3, the search tree is as follows:
The remainder of Modulo 3 in brackets. It indicates the number of searched items. The left subtree represents 0, and the right subtree represents 1. when the second layer is found (the root layer is layer 0th), it encounters 111, and the remainder of Modulo 3 is 0. so N * m = 111, M = 111/3 = 37.
Method 4: Improvement on method 3. The search space in method 3 is classified by model N remainder, so that the search time and space are reduced from the original exponential level to O (n ). Principle of improvement: Assuming that a K-digit decimal number consisting of 0 and 1 is being searched, the K-digit decimal number is 2 ^ K. Assume that there are two numbers x and y, and their modulo n is the same remainder, then when you search for k + 1 decimal numbers composed of 0 and 1, X and Y are extended to four numbers: 10x, 10x + 1, 10y, 10y + 1. Because X and Y are the same (they can be considered equal), 10 X and 10 y are the same, and 10 x + 1 and 10 y + 1 are the same. That is to say, the subtree extended by Y and the subtree generated by X extension produce the same remainder. If X is smaller than Y, Y is definitely not the smallest number that meets the requirements, so y can be cut off. In this way, the number of 2 ^ K is classified by the remainder of the modulo n, and only the smallest number of each type is reserved for expansion. Originally, we needed to search for the number of 2 ^ K at this layer. Now we only need to search for the number of O (n. For example, when n = 9, layer 0th is 1 (1 ),
As shown in, 2nd and 110 of Layer 3 are all cut out because the same layer has the same number and smaller number. If you search by method 3, the third layer should have eight nodes, but now there are only four nodes.
Source code of Method 1:
- # Include <stdio. h>
- IntHasonlyoneandzero (unsignedIntN ){
- While(N ){
- If(N % 10> = 2)Return0;
- N/= 10;
- }
- Return1;
- }
- IntMain (){
- IntN, m;
- While(Scanf ("% d", & N )! = EOF ){
- For(M = 1; m ++ ){
- If(Hasonlyoneandzero (N * m )){
- Printf ("n = % d, M = % d, n * m = % d/N", n, m, n * m );
- Break;
- }
- }
- }
- Return0;
- }
Source code of method 3:
- // Solution 3 (1): breadth-first search
- # DEFINE _ crt_secure_no_warnings 1
- # Include <cstdio>
- # Include <queue>
- Using NamespaceSTD;
- IntMain (){
- IntN;
- While(Scanf ("% d", & N )! = EOF ){
- Queue <Int> Q;
- Q. Push (1 );
- While(! Q. Empty ()){
- IntT = Q. Front ();
- Q. Pop ();
- If(T % N = 0 ){
- Printf ("n = % d, M = % d, n * m = % d/N", N, T/N, t );
- Break;
- }
- Q. Push (T * 10 );
- Q. Push (T * 10 + 1 );
- }
- }
- Return0;
- }
Method 4 source code:
- // Solution 4: divide the search space into the breadth of the category, so that the space is occupied by O (n) instead
- // Exponential level. The classification principle is to classify according to the remainder of model N.
- # DEFINE _ crt_secure_no_warnings 1
- # Include <cstdio>
- # Include <bitset>
- # Include <vector>
- # Include <queue>
- Using NamespaceSTD;
- StructQnode {
- IntV, R; // v is value, R is Remainder
- Qnode (IntVV,IntRr): V (VV), R (RR ){}
- Qnode (): V (0), R (0 ){}
- };
- IntMain (){
- IntN;
- While(Scanf ("% d", & N )! = EOF ){
- // Bitset <n> Bn;
- Queue <qnode> q;
- Q. Push (qnode (1, 1 ));
- While(! Q. Empty ()){
- // Bn. Reset ();
- Vector <Bool> Bn (n,False);
- IntS = Q. Size ();
- While(S --){
- Qnode T = Q. Front ();
- If(T. R = 0 ){
- Printf ("n = % d, M = % d, n * m = % d/N", N, T. V/N, T. V );
- GotoOK;
- }
- Q. Pop ();
- If(! BN [T. R * 10% N]) {
- BN [T. R * 10% N] =True;
- Q. Push (qnode (T. V * 10, T. R * 10% n ));
- }
- If(! BN [(T. R * 10 + 1) % N]) {
- BN [(T. R * 10 + 1) % N] =True;
- Q. Push (qnode (T. V * 10 + 1, (T. R * 10 + 1) % N ));
- }
- }
- }
- OK :;
- }
- Return0;
- }
Link: http://blog.csdn.net/jcwKyl/archive/2009/02/02/3859155.aspx