BFS POJ1426 Find the multiple

Source: Internet
Author: User
Tags stdin time limit

Find The multiple
time limit:1000ms Memory limit:10000k
Total submissions:22471 accepted:9236 Special Jud GE
Description
Given A positive integer n, write a program to find out a nonzero multiple m of n whose decimal r Epresentation contains only the digits 0 and 1. You may assume this n is not greater than and there are a corresponding m containing no more than decimal digits.
Input
The input file may contain 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 digits. If There is multiple solutions for a given value of N, any one of the them is acceptable.
Sample Input
2
6

0
Sample Output
Ten
100100100100100100
111111111111111111
Source
Dhaka 2002

Test instructions: gives a positive integer n not greater than 200, which is a positive integer m consisting only of numbers 0 and 1, which divides n into M.
Proof 1: Any positive integer n, there must be a number of only 0 and 1 m, so that n is divisible by M.
in the number of 1,11,111,1111 ... 111 ... 111 (n 1):
(1) There is a number a, which makes a mod n = 0. So 10*a mod n = 0, and 10*a only consists of numbers 0 and 1.
(2) There is no number divisible by n, but there are always two number p,q, which have the same remainder as n (see Proof 2). Set P=r1*n+s,q=r2*n+s. Then, the b=p-q= (R1-R2) *n is a multiple of N and consists only of 0 and 1. The
is completed.
Prove 2:1,11,111,1111, ..., 111 ... One (n 1) is divided by N to obtain n remainder, and the remainder of the maximum only N-1 a different remainder (1 to N-1, because all numbers can not be divisible by n, so there is no 0 in the remainder), by the pigeon nest principle, there must be two remainder of the same.
Idea: Simple BFS problem, plain search, starting from 1, 10,11,101,100,110,111 ... Use the queue. Each time you search for the first number a of the team, a*10 and a*10+1 are queued until you find the answer.

5044K 407MS
#include <cstdio>
#include <queue>
#include <cstring>
#include < Iostream>
using namespace std;
int main ()
{
    freopen ("Input.txt", "R", stdin);
    Freopen ("Output.txt", "w", stdout);
    int n;
    Long long Tmp_front;
    while (scanf ("%d", &n)!=eof&&n) {
        queue<long long> q;
        Q.push (1);
        while (!q.empty ()) {
            tmp_front=q.front ();
            Q.pop ();
            if (tmp_front%n==0) {
                printf ("%lld\n", Tmp_front);
                break;
            }
            Q.push (tmp_front*10);
            Q.push (tmp_front*10+1);
        }
    }
    return 0;
}

Here is the method of the great God, which is 10 times times more efficient than a simple search method,//http://m.blog.csdn.net/blog/lyy289065406/6647917
Solving methods: Bfs+ and Comodule theorem
First of all, simple, non-pruning search methods:
I take n=6 as an example.
First decimal number, first digit (highest bit) must not be 0, that is, the highest bit must be 1

Set 6 "10 binary" to K, then must have k%6 = 0
Now we're going to use BFS to find K-values.
1, first search the highest bit of K, the highest bit must be 1, then k=1, but 1%6 = 1! = 0
So k=1 is not asking for the remainder of the storage 1
2, search the next one, the next one may be 0, that is k*10+0, at this time k=10, then k%6=4
May be 1, that is k*10+1, at this time k=11, then k%6=5
Because the remainder is not 0, that is, k=10 and k=11 are not asked
3, continue to search for the third place, there are four possibilities:
For k=10, the next one may be 0, or k*10+0, at this time k=100, then k%6=4
The next one may be 1, that is k*10+1, at this time k=101, then k%6=5
For k=11, the next one may be 0, or k*10+0, at this time k=110, then k%6=2
The next one may be 1, that is k*10+1, at this time k=111, then k%6=3
Since the remainder is not 0, that is, the k=100,k=101,k=110,k=111 is not a request
4, continue to search the fourth place, at this time there are eight possibilities:
For k=100, the next one may be 0, or k*10+0, at this time k=1000, then k%6=4
The next one may be 1, that is k*10+1, at this time k=1001, then k%6=5
For K=101, the next one may be 0, or k*10+0, at this time k=1010, then k%6=2
The next one may be 1, that is k*10+1, at this time k=1011, then k%6=3
For k=110, the next one may be 0, or k*10+0, at this time k=1100, then k%6=2
The next one may be 1, that is k*10+1, at this time k=1101, then k%6=3
For k=111, the next one may be 0, or k*10+0, at this time k=1110, then k%6=0
The next one may be 1, that is k*10+1, at this time k=1111, then k%6=1
When we find k=1110, k%6=0, or 1110, is the multiple we ask for.
From the above deduction is not difficult to find, with BFS is to search the current digit (except the highest bit fixed to 1), because each bit only 0 or 12 choice, in other words is a double inlet BFS
The difficulty lies in the processing after the search: the processing of the remainder, the processing of large numbers, the relationship between the remainder and the desired multiples.
Let's talk about how to deal with the problem of large numbers and pruning:
Let's start with a brief look at the naïve search method:

n=6 1%6=1 (k=1) {(1*10+0)%6=4 (k=10) {(10*10+0)%6=4 (k=100) {(100*10+0)%6=4
        (k=1000) (100*10+1)%6=5 (k=1001)} (10*10+1)%6=5 (k=101) {(101*10+0)%6=2 (k=1010) (101*10+1)%6=3 (k=101  1)}} (1*10+1)%6=5 (k=11) {(11*10+0)%6=2 (k=110) {(110*10+0)%6=2 (k=1100) (110*10+1)%6=3
    (k=1101)} (11*10+1)%6=3 (k=111) {(111*10+0)%6=0 (k=1110) has solution (111*10+1)%6=1 (k=1111) because there is a solution in front, the remainder is not stored}}}< /pre>

From the above can be seen the remainder of the order of storage (stored by layer):
The remainder with array mod[], where mod[0] is not used, starting from mod[1]
then the remainder of the mod is: 1 4 5 4 5 2 3 4 5 2 3 2 3 0 Total 14
That means we get the remainder 0 before, do 14 steps *10 operation, then when the n value is large enough, it is very easy to have K for large number of cases (in fact, I have done statistics, 200 of the N, there are 18 n corresponding K value for the large number
then we use int to store k is not very wise.)
in order to handle all cases, we naturally think that we should use int[] to store every bit of K.
and since K is a 01 sequence, can the *10 get K each bit of the problem into the modulo 2 operation to get every bit of K (0 or 1) it?
The answer is Yes
First we use the same comodule theorem to make an optimization of the way the remainder is obtained
(a*b)%n = (a%n *b%n)%n
(a+b)%n = (a%n +b%n)%n
Take one of the above clauses as an example
previous step (11*10+0)%6=2 namely k=110, k%6=2
Current step (110*10+1)%6=2
by the same comodule theorem (110*10+1)%6 = ((110*10)%6+1%6)%6 = ((110%6 * 10%6 )%6 +1)%6
is not difficult to find the underscore part 110%6 equals (11*10+0)%6 = 2
So the current step (110*10+1)%6 can be converted to (2*10+1)%6=2

Obviously, this kind of processing k=110 equivalent to k=2
that is, the remainder of the previous operation to replace the K value of the current step
and N in the range of 200, the residual value can not exceed 3 digits, which solves the problem of large numbers
By this approach, we only need to be in the BFS The hand stores a remainder array mod[], can be obtained through the mod[i-1] mod[i], until the end of the mod[i]==0, greatly reducing the computational time
has been mentioned, n=6, the balance operation was carried out 14 times, corresponding to, BFS *10 operation also carried out 14 times.
Let i=14, through observation found that i%2 happens to be a multiple of 6 of the lowest digit number
I/2 again i%2, exactly is a multiple of the second-low number of 6 ...
Loop This operation until i=0, you can get 6 01 times times (a 01 queue), the reverse output is required
to complete the *10 operation to%2 operation of the transition

2748K 47MS #include <iostream> #include <cstdio> using namespace std;  int mod[524286]; Save the remainder of each mod n//since the remainder sequence of 198 is the longest//after repeated two-point verification, 436905 is able to store a minimum of 198-odd sequence of space// But POJ must have crossed the test again.
    524286 is the minimum lower limit of AC, otherwise the re int main (int i) {freopen ("Input.txt", "R", stdin) is not guaranteed;
    Freopen ("Output.txt", "w", stdout);
    int n;  while (cin>>n&&n) {mod[1]=1%n; Initialization, the highest bit of n multiples must be 1 for (i=2;mod[i-1]!=0;i++)//using the same comodule theorem, the remainder of the previous step MOD[I/2] get the remainder of the next Mod[i] mod[i]= (mod[i/2]*10
                     +i%2)%n; The mod[i/2]*10+i%2 simulates the two-entry search for BFS//When I is even, +0, which is the current bit number 0.
        is an odd number, then +1, which takes the current digit to 1//for (i=1;mod[i]!=0;i++) cout<<mod[i]<< "";cout<<endl;
        i--;
        int pm=0;   while (i) {mod[pm++]=i%2;
        The *10 operation is converted to%2 operation, and each digit of the inverse multiplier is i/=2;  } while (PM) cout<<mod[--pm];
    Reverse Output cout<<endl;
} return 0;
 }

Note: The second program opened 500,000 of memory, 2700k of memory, the first program did not open the array, but the memory reached 5000k. This is because the queue of the first program is running with a maximum of 5000k of data, and after all it is a long long, 4 bytes more than int, while the second program opens 500,000 of the memory, but the actual runtime may not use as much memory, the array opens to 1,000,001 like 2700k.

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.