Multiples of N of 51nod 1109 01 (BFS) __ Search

Source: Internet
Author: User
Description

Given a natural number N, find a M, so that M > 0 and M are multiples of N, and M's 10 binary representation contains only 0 or 1, and the smallest m.

For example: N = 4,m = 100.

Input

Enter 1 number N. (1 <= N <= 10^6)

Output

Outputs the smallest M that meets the criteria.

Sample Input

4

Sample Output

100

train of Thought

The enhanced version of the POJ 1426 is mainly the increase in data.

As the range of data expands, the solution space we are searching for will also rise exponentially, so here's a good pruning to consider:

we know that 13%3= ((1x10)%3+3)%3 13\%3= ((1 \times) \%3+3) \%3

that is, the two-digit prefix modulo the result of one number is equal, the final result is equal by the suffix of the only decision.

Therefore, when we consider the search, there is the same prefix modulo the condition of pruning.

The string type in ps:c++ is easy to use, but only the last set of data cannot pass (TLE), and the last set of data is 738169.

PPS: simply using arrays to store MTE, we need to consider dynamically allocating memory.

PPPS: There is a feeling of violence, of course, there are more excellent solution ~ (go out and turn right a big God Blog

AC Code

 #include <bits/stdc++.h> using namespace std; typedef __int64 LL; const int MAXN = 1E6+10; I
NT N,tot;
BOOL VIS[MAXN];

queue<char*>sk;
    int Multget (char* s,int len) {int ans=0;
    for (int i=0; i<len; i++) ans= (ans*10+s[i]-' 0 ')%n;
return ans;
    } void push (const char* S,int Len,char c) {char *ne = (char*) malloc (len+2);
    strcpy (ne,s);
    Ne[len]=c;
    ne[len+1]=0;
Sk.push (NE);
    } void BFs () {push ("", 0, ' 1 ');
        while (!sk.empty ()) {char* s=sk.front ();
        Sk.pop ();
        int len = strlen (s);
        int num = Multget (S,len);
            if (num==0) {puts (s);
        Return
            } if (!vis[num]) {push (S,len, ' 0 ');
            Push (S,len, ' 1 ');
        Vis[num]=true;
    Free (s);
    int main () {cin>>n;
    BFS ();
return 0; }

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.