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; }