首页 > 代码库 > poj 1426 Find The Multiple(bfs)

poj 1426 Find The Multiple(bfs)

Find The Multiple
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 18757 Accepted: 7592 Special Judge

Description

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

Source

Dhaka 2002

[Submit]   [Go Back]   [Status]   [Discuss]

技术分享Home Page   技术分享Go Back  技术分享To top



题意:
给你一个n,求一个只由1和0组成的十进制数对n取摸为0。

CODE:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>

#define N 100010
#define Mod 10000007
#define lson l,mid,idx<<1
#define rson mid+1,r,idx<<1|1
#define lc idx<<1
#define rc idx<<1|1
const double EPS = 1e-11;
const double PI = acos ( -1.0 );
const double E = 2.718281828;
typedef long long ll;

const int INF = 1000010;

using namespace std;

int n;
struct node
{
    string s;
    int mod;//保留前一个的摸值
};

bool vis[222];

queue<node>que;

void bfs()
{
    while(que.size())
        que.pop();
    memset(vis,0,sizeof vis);
    node a,t;
    a.mod=1;
    a.s="1";
    que.push(a);
    vis[1]=1;
    while(que.size())
    {
        a=que.front();
        que.pop();
        if(a.mod==0)
        {
            cout<<a.s<<endl;
            return;
        }
        for(int i=0; i<2; i++)
        {
            t.mod=a.mod*10+i;
            t.mod%=n;
            if(!vis[t.mod])
            {
                if(i)
                    t.s=a.s+"1";
                else
                    t.s=a.s+"0";
                que.push(t);
                vis[t.mod]=1;
            }
        }
    }
}

int main()
{
    while(cin>>n&&n)
    {
        bfs();
    }
    return 0;
}


poj 1426 Find The Multiple(bfs)