您的位置:首页 > 其它

【原】 POJ 1426 Find The Multiple BFS搜索 解题报告

2010-11-05 16:37 447 查看
 

http://poj.org/problem?id=1426

方法:

找到n的倍数m,m中只能有0或1
0,1可构造出二叉树进行BFS搜索
该题的重点就在于如何构造出BFS进行搜索。
所有0,1组成的数可以构造出一颗二叉树从而进行BFS。每层的数字长度相同,每个节点的左右孩子为其末尾分别添加0,1
BFS搜索一颗二叉树。二叉树的节点按层序遍历依次为(1),(10,11),(100,101,110,111)....
即每个节点vertex的邻接点为10*vertex和10*vertex+1               1
             / \
            /   \
           /     \
         10       11
         / \      /\
        /  \    / \
      100 101 110 111

 

注意:
1、使用STL的queue会TLE,改用自己手写的才126MS左右,差别很大
2、自己手写的queue的capacity是测试出来的,即使得200以内的答案正确即可
3、虽然题目中说m可能有200位那么长,似乎要用到bignum,但是经测试可知__int64足够了。

 

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

 

[code]       [code] void run1426_2()


{


int n ;


__int64 m ;


const int capacity = 9999999 ;


__int64 *myQueue = new __int64[capacity] ;


int front,rear ;


 


//for( n=1 ;n<=200; ++n )


while( scanf("%d",&n) && n!=0 )


    {


rear = 0 ;


front = 1 ;


 


myQueue[++rear] = 1 ;


while( true )


   {


m = myQueue[front] ;


if(++front == capacity)


front = 0 ;


 


if( m%n==0 )


break ;


 


m = 10*m ;


if( m%n==0 )


break ;


if(++rear == capacity)


        rear = 0 ;


myQueue[rear]=m ;


 


m += 1 ;


if( m%n==0 )


break ;


if(++rear == capacity)


        rear = 0 ;


myQueue[rear]=m ;


}


//printf("%d\t%d\n",n,m) ;


printf("%I64d\n",m) ;


}


delete []myQueue ;


}

[/code]
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: