"The beauty of Programming" Find an integer that matches the criteria

Source: Internet
Author: User

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

After looking at the topic requirements, we first think of the value of the small to large enumeration M, and then calculate the n*m, and finally determine whether their product contains only 1 and 0. The general idea can be implemented with the following pseudo-code:

1  for 2;; m++)2{3     Product = N *4     if  ( Hasonlyoneandzero (product))5         return6 }

But the problem soon arises, when should the cycle be terminated? Will this cycle stop? Even if it can be terminated, this cycle may still take too much time, such as N = 99 o'clock, m = 1122334455667789,n * m = 111111111111111111.

Analysis and Solution

The direct approach to the topic is clearly not a satisfactory approach. Is there any other way? The answer is yes.

method One: Post-conversion enumeration

You can make a conversion to the problem: the decimal representation of a minimum positive integer x,x is only 0 and 1, and x%n=0.

We can find that only 0 and 1 of the number X are like this: 1,10,11,100,101,110,111,1000,1001,1010,1011,1100,1101,1110,1111,10000 ...

For the x loop, check to see if it can be divisible by n, and if so, find the smallest x, then we can figure out the m by X.

It is not difficult to find that if the final X has K-bit, then our algorithm time complexity is at least O (2K), can we improve it?

method Two: class dynamic programming

Introduce variable j=x%n, use array bigint[j] to represent the smallest positive integer X except the N remainder is J, then bigint[0] is what we ask for.

For any integer x=10i+k, we have j=x%n= (10i+k)%n= (10i%n+k%n)%N.

So, if BIGINT[J] hasn't been calculated yet, we can do it in the following ways:

Bigint[j]=10i+bigint[k%n]

If BIGINT[J] already exists, we will not update its value.

For convenience, we use a variable-length array to represent BIGINT[J], which is represented in the following code using Vector<int>.

1001 is expressed as 100+103, which is bigint[j]={0, 3}.

The reference code looks like this:

1#include <iostream>2#include <vector>3 using namespacestd;4 5typedefLong LongLL;6 7 Const intMAXN =1000007; 8vector<int>BIGINT[MAXN];9 TenLL Tenpow (intN) One { ALL ret =1;  -      for(inti =0; I < n; i++) -RET *=Ten;  the     returnret; - } -  -LL Biginttoll (vector<int>bigint) + { -     intLen =bigint.size (); +LL ret =0;  A      for(inti =0; i < Len; i++) atRET + =Tenpow (Bigint[i]); -     returnret; - } -  - voidSolveintN) - { in      for(inti =0; i < N; i++) - bigint[i].clear (); to      +bigint[1].push_back (0);  -      the      for(inti =1, j =Ten% N;; i++, J = (J *Ten) %N) *     { $         BOOLFlag =false; Panax Notoginseng         if(bigint[j].size () = =0) -         { theFlag =true;  + Bigint[j].push_back (i); A         } the          +          for(intK =1; K < N; k++) -         { $             intR = (k + j)%N; $             if((Bigint[k].size () >0)  -&& (i > Bigint[k][bigint[k].size ()-1]) -&& (bigint[r].size () = =0)) the             { -Flag =true; WuyiBIGINT[R] =Bigint[k]; the Bigint[r].push_back (i); -             } Wu         } -         if(bigint[0].size () >0) About         { $              Break; -         } -     } - } A  + intMainintargcChar*argv[]) the { -     intN; $      while(Cin >>N) the     { the solve (N); the          the         if(bigint[0].size () = =0) -         { incout <<"M not exist"<<Endl; the              Break; the         } About         Else  the         { thecout << N <<" * "<< Biginttoll (bigint[0]) /N the<<" = "<< Biginttoll (bigint[0]) <<Endl; +         } -     } the}
View CodeScaling Issues

1. For any n, there must be M, so that the product of the N*m 10 system is only 0 and 1?

Conclusion: for arbitrary N, there must be M, which makes the n*m of the product of the decimal means only 0 and 1.

Prove:

100modN, 101modN, 102modN, ..., 10imodN, ... is an infinite sequence;

Moreover, this sequence can only be valued at 0, 1, ..., N-1

Therefore, the sequence must be a cyclic sequence, with a period of T, then there is

10smodN = 10S+TMODN = ... = 10s+ (N-1) TMODN

So: (10s + 10s+t + ... + 10s+ (N-1) t) MODN = 0

So, presence M makes m * N = 10s + 10s+t + ... + 10s+ (N-1) T Moreover, such m is not the only one.

2. How to find the N and M to meet the requirements of the topic, make n*m<216, and n+m maximum?

Analysis:

First, we found n*m < 216 = 32768,

Then the maximum x is 11111, followed by 11110

At first I think that 1*11111 is the N and M, the maximum n+m=11112 that satisfies the conditions,

In fact, this is not correct, because when n=1, the smallest m=10 can satisfy the n*m result of the decimal representation of only 0 and 1, and when taking n=11111, the smallest m=10, not 1, because the title of M is greater than 1, therefore, this is not satisfied.

We found that when n is larger, m is more hours, the value of the n+m that satisfies the condition can be greater.

The smallest m=2, followed by 3

When m=2, the largest n=5555, satisfies the n*m=11110,n+m=5557

When M=3, the largest n=3700, satisfies the n*m=11100,n+m=3703

Obviously, when M is larger, the maximum value of n is smaller, and the resulting and smaller.

In conclusion, when n=5555,m=2, meet n*m=11110, at this time the N+m maximum is 5557.

"The beauty of Programming" Find an integer that matches the criteria

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.