【原】 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]
相关文章推荐
- POJ 1426 Find The Multiple (bfs搜索)
- poj 1426 Find The Multiple (bfs 搜索)
- poj 1426 Find The Multiple(搜索BFS的思想+ 同余模定理+二叉树+01哈夫曼编码)
- POJ1426 Find The Multiple 解题报告
- POJ 1426 Find The Multiple(简单搜索bfs)
- POJ - 1426 Find The Multiple(15.10.10 搜索专题)bfs
- POJ1426-Find The Multiple-深度优先搜索BFS
- poj 1426 Find The Multiple (bfs 搜索)
- POJ-1426 Find The Multiple 解题报告
- Find The Multiple poj 1426 bfs
- POJ-1426(Find The Multiple)--简单搜索
- [kuangbin带你飞]专题1 简单搜索 E - Find The Multiple POJ - 1426
- POJ 1426 Find The Multiple(BFS 同余模定理)
- POJ1426,Find The Multiple,bfs...注意类型 5兆内存过了...
- poj1426 Find The Multiple 简单搜索
- [kuangbin带你飞]专题一 简单搜索E - Find The Multiple(POJ 1426)
- POJ 1426 Find the Multiple 思路,线性同余,搜索 难度:2
- poj 1426 Find The Multiple(bfs)
- poj-1426 Find The Multiple BFS打表
- POJ-1426--Find The Multiple---BFS广搜