The beauty of programming Reading Notes (13): "finding qualified integers"

Source: Internet
Author: User
Tags bitset

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:

  1. # Include <stdio. h>
  2. IntHasonlyoneandzero (unsignedIntN ){
  3. While(N ){
  4. If(N % 10> = 2)Return0;
  5. N/= 10;
  6. }
  7. Return1;
  8. }
  9. IntMain (){
  10. IntN, m;
  11. While(Scanf ("% d", & N )! = EOF ){
  12. For(M = 1; m ++ ){
  13. If(Hasonlyoneandzero (N * m )){
  14. Printf ("n = % d, M = % d, n * m = % d/N", n, m, n * m );
  15. Break;
  16. }
  17. }
  18. }
  19. Return0;
  20. }

Source code of method 3:

  1. // Solution 3 (1): breadth-first search
  2. # DEFINE _ crt_secure_no_warnings 1
  3. # Include <cstdio>
  4. # Include <queue>
  5. Using NamespaceSTD;
  6. IntMain (){
  7. IntN;
  8. While(Scanf ("% d", & N )! = EOF ){
  9. Queue <Int> Q;
  10. Q. Push (1 );
  11. While(! Q. Empty ()){
  12. IntT = Q. Front ();
  13. Q. Pop ();
  14. If(T % N = 0 ){
  15. Printf ("n = % d, M = % d, n * m = % d/N", N, T/N, t );
  16. Break;
  17. }
  18. Q. Push (T * 10 );
  19. Q. Push (T * 10 + 1 );
  20. }
  21. }
  22. Return0;
  23. }

Method 4 source code:

  1. // Solution 4: divide the search space into the breadth of the category, so that the space is occupied by O (n) instead
  2. // Exponential level. The classification principle is to classify according to the remainder of model N.
  3. # DEFINE _ crt_secure_no_warnings 1
  4. # Include <cstdio>
  5. # Include <bitset>
  6. # Include <vector>
  7. # Include <queue>
  8. Using NamespaceSTD;
  9. StructQnode {
  10. IntV, R; // v is value, R is Remainder
  11. Qnode (IntVV,IntRr): V (VV), R (RR ){}
  12. Qnode (): V (0), R (0 ){}
  13. };
  14. IntMain (){
  15. IntN;
  16. While(Scanf ("% d", & N )! = EOF ){
  17. // Bitset <n> Bn;
  18. Queue <qnode> q;
  19. Q. Push (qnode (1, 1 ));
  20. While(! Q. Empty ()){
  21. // Bn. Reset ();
  22. Vector <Bool> Bn (n,False);
  23. IntS = Q. Size ();
  24. While(S --){
  25. Qnode T = Q. Front ();
  26. If(T. R = 0 ){
  27. Printf ("n = % d, M = % d, n * m = % d/N", N, T. V/N, T. V );
  28. GotoOK;
  29. }
  30. Q. Pop ();
  31. If(! BN [T. R * 10% N]) {
  32. BN [T. R * 10% N] =True;
  33. Q. Push (qnode (T. V * 10, T. R * 10% n ));
  34. }
  35. If(! BN [(T. R * 10 + 1) % N]) {
  36. BN [(T. R * 10 + 1) % N] =True;
  37. Q. Push (qnode (T. V * 10 + 1, (T. R * 10 + 1) % N ));
  38. }
  39. }
  40. }
  41. OK :;
  42. }
  43. Return0;
  44. }

Link: http://blog.csdn.net/jcwKyl/archive/2009/02/02/3859155.aspx

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.