E

Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 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 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.

Sample Input

2
6
19
0

Sample Output

10
100100100100100100
111111111111111111

在VOJ上BFS要超时,看别人用dp做的不超时
BFS:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
//#include <unordered_set>
//#include <unordered_map>
//#include <xfunctional>
#define ll  long long
#define PII  pair<int, int>
using namespace std;
int dir[5][2] = { {0,1} ,{0,-1}, {1,0}, {-1,0} ,{0,0} };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 1e9 + 7;
const int maxn = 16;
//if(x<0 || x>=r || y<0 || y>=c)
int n;
int main()
{
    while (cin >> n && n != 0)
    {
        queue<ll> que;
        que.push(1);
        while (!que.empty())
        {
            ll tmp=que.front();
            que.pop();
            if (tmp%n==0)
            {
                cout << tmp << endl;
                break;
            }
            que.push(tmp * 10);
            que.push(tmp * 10 + 1);
        }
    }
    return 0;
}

DP:

#include<iostream>
#include<vector>
using namespace std;
const int maxn=5e6;
int n;
int dp[maxn];
vector<int> ans;
int main()
{
    while(cin>>n && n)
    {
        dp[1]=1%n;
        int now;
        for(int i=1;;i++)
        {
            if(!(dp[i*2]=dp[i]*10%n)) {now=i*2;break;}
            if(!(dp[i*2+1]=(dp[i]*10+1)%n)) {now=i*2+1;break;}
        }

        ans.clear();
        while(now)
        {
            ans.push_back(now%2);
            now/=2;
        }
        for(int i=ans.size()-1;i>=0;i--) cout<<ans[i]; cout<<endl;
    }
}
原文地址:https://www.cnblogs.com/dealer/p/12556953.html