2nd: The charm of numbers--looking for a qualifying integer

Source: Internet
Author: User

Find an integer problem description that matches the criteria

Arbitrarily given a positive integer n, find a minimum positive integer M (m>1), so that the decimal representation of N*m only contains 1 and 0.

To solve this problem first of all consider for arbitrary N, whether such m must exist. It can be proved that M is a certain existence, and not unique.
Simple proof: Because

This is an infinite sequence, but each of the values in the sequence ranges between [0, N-1]. So there must be a link in the middle of this infinite sequence. That is to say that there are s,t are positive integers, and s<t, there is. So the cyclic section length is T-S. So 10^s = 10^t. So there are:
So

For example, take n=3, because 10 of any nonnegative square modulo 3 is 1, so the cycle period is 1.:

Analysis and solution of "solution one"

Given N, which starts with M 2, enumerates the values of m until it encounters a m that makes the decimal representation of N*m only 1 and 0.

The code is as follows:

1  PackageChapter2shuzizhimei.findminint;2 /**3 * Find an Integer that matches the criteria4 * "Solution One"5  * @authorDELL6  *7  */8  Public classFindMinInt1 {9     //determines whether the decimal representation of integers is only 0 and 1Ten      Public Static BooleanHasonlyoneandzero (Longx) { One          while(x!=0){ A             if(x%10>=2) -                 return false; -X/= 10; the         } -         return true; -     } -     //find the smallest positive integer that meets the criteria +      Public Static LongFindmin (LongN) { -          for(inti=2;; i++){ +             if(Hasonlyoneandzero (i*N)) A                 returni; at         } -     } -      -      Public Static voidMain (string[] args) { -         Longn = 3; -System.out.println ("makes the decimal representation of the product with" +n+ "only 1 and 0 of the smallest positive integer is:" +findmin (n)); in  -     } to  +}

The results of the program run as follows:

The decimal representation of the product with 3 is the smallest positive integer with only 1 and 0:37
"Solution Two"

The remainder sequence of 10 of the quadratic sequence modulo n is obtained and the follow-up process is found. Then search for this remainder sequence, and the purpose of the search is to find some numbers in this remainder sequence so that they are multiples of N. For example, n=13, this sequence is 1,10,9,12,3,4 and then constantly looping. Obviously there are 1+12=13, and 1 is 10 of 0, 12 is 10 3, so this number is 1000+1=1001,m is 1001/13=77.

"Solution three"

Because the value of N*m is 1,10,11,100,101,110,111,...... So directly in this space search, this is the improvement of method one. Search this sequence until you find a number that can be divisible by n, which is n*m, and then the M is calculated. For example n=3, the search tree is as follows:

The remainder of modulo 3 is represented within the brackets. The number that is searched outside the parentheses. Zuozi represents 0, and the right subtree represents 1. In the second layer (the root is the No. 0 layer) encountered 111, it modulo 3 is 0. So n*m=111, m=111/3=37.

The specific implementation code is as follows:

1  PackageChapter2shuzizhimei.findminint;2 3 Importjava.util.LinkedList;4 ImportJava.util.Queue;5 6 /**7 * Find an Integer that matches the criteria8 * "Solution three"9  * @authorDELLTen  * One  */ A  Public classFindMinInt3 { -      -     //find the smallest positive integer that meets the criteria the      Public Static LongFindmin (LongN) { -queue<long> queue =NewLinkedlist<long>(); -Queue.add ((Long) 1); -         LongTemp//to store the first element of the removed team +          while(true){ -temp =Queue.poll (); +             if(temp%n==0) A                 returntemp/N; atQueue.add (temp*10); -Queue.add (temp*10+1); -         } -     } -      -      Public Static voidMain (string[] args) { in         Longn = 3; -System.out.println ("makes the decimal representation of the product with" +n+ "only 1 and 0 of the smallest positive integer is:" +findmin (n)); to  +     } -  the}

The results of the program run as follows:

The decimal representation of the product with 3 is the smallest positive integer with only 1 and 0:37
"Solution Four"

Improvements to method three. The search space of method Three is categorized by the modulo N remainder, so that the search time and space are reduced from the original point of order to O (n). The principle of improvement: assuming that a K-bit decimal number consisting of 0 and 1 is currently being searched, this k-bit decimal number has a total of 2^k. Assuming that there are two numbers x, Y, and they are modulo n congruence, then x and Y will be expanded by four numbers when searching for k+1 decimal numbers consisting of 0 and 1:10X, 10x+1, 10Y, 10y+1. Because the X and Y congruence (congruence can be regarded as equal), so 10X and 10Y with the same, 10x+1 and 10y+1 with the remainder. That is, the subtree expanded by Y and the subtree produced by the x extension produce exactly the same remainder, and if X is smaller than y, then Y is definitely not the smallest number that satisfies the requirement, so y this subtrees tree can be cut off. Thus, the number of 2k is sorted by the modulo n remainder, and only the smallest number is retained for expansion in each class. Originally in this layer need to search 2k number, now only need to search O (N) number. For example, when n=9, the No. 0 layer is 1 (1),

As shown, the 2nd layer of 110, the third layer of 1010, 1110 because the same layer has and it is the same remainder and a smaller number is cut off. If you search by method Three, the third layer should have 8 nodes, but now there are only 4 nodes.

Just add the results of the 10k%n to the non-empty elements in the remainder information array, and then go to modulo n to see if the new remainder will appear.

The specific code is as follows:

1  PackageChapter2shuzizhimei.findminint;2 3 Importjava.util.LinkedList;4 ImportJava.util.Queue;5 6 /**7 * Find an Integer that matches the criteria8 * "Solution four"9  * @authorDELLTen  * One  */ A  Public classFindMinInt4 { -      -     //find the smallest positive integer that meets the criteria the      Public Static LongFindmin (LongN) { -         Long[] A =New Long[(int* M];//A[i] Record the value after the modulo n remainder i -          for(inti=0;i<n;i++) {//initializing an array -A[i]=-1; +         } -         intFactor = 1; +A[1] = 1; A         inti; at          for(i=0;; i++){ -Factor *= 10; -             intCurrent = (int) (Factor%n); -             if(current = = 0){ -A[current] = factor;//save a number with a remainder of 0 -                 returnfactor/N; in             } -             if(A[current]==-1){ toA[current] =factor; +             } -             //Adds the current remainder to the remainder of the remainder array mod n the             intK//Store the remainder of the preceding *             //Add the current remainder to the previous remainder and then modulo n, if the value of the new remainder is saved, and if the value is the same as before, it will not be updated because the smallest $              for(k=1;k<n;k++){Panax Notoginseng                 if(A[k]==-1) -                     Continue; the                 intNewyu = (int) ((current+k)%n); +                  //The newly generated remainder does not exist, add factor and K, get new number, only with the number of less than factor want to add, do not add to the back, and factor the number must be less than factor A                 if(a[newyu]==-1&&a[k]<factor) { theA[newyu] = factor +A[k]; +                     if(newyu==0) -                         returna[newyu]/N; $                 } $             } -         } -     } the      -      Public Static voidMain (string[] args) {Wuyi         Longn = 3; theSystem.out.println ("makes the decimal representation of the product with" +n+ "only 1 and 0 of the smallest positive integer is:" +findmin (n)); -  Wu     } -  About}

The results of the program run as follows:

The decimal representation of the product with 3 is the smallest positive integer with only 1 and 0:37

Reference Links:The beauty of programming--looking for a qualifying integerprogramming Beauty Find an Integer that matches the criteriaprogramming Beauty Find an Integer that matches a condition

2nd: The charm of numbers--looking for a qualifying integer

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.