poj 1426 BFS

Find The Multiple

Time Limit: 1000MS
Memory Limit:10000K

Total Submissions: 10604
Accepted: 4373
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
   1: #include <iostream>
   2: //#include <queue>
   3: //#include <cstring>
   4: #include <cstdio>
   5: using namespace std;
   6: long long num[9999999];
   7:  
   8: int main()
   9: {
  10:     int n,i,j;
  11:     while(scanf("%d",&n) && n!=0)
  12:     {
  13:         i=0;
  14:         j=1;
  15:         num[0] = 1;
  16:         while(1)
  17:         {
  18:             if(num[i]%n == 0)
  19:                 {
  20:                     printf("%I64d\n",num[i]);
  21:                     break;
  22:                 }
  23:             else
  24:             {
  25:                 num[j++] = num[i]*10;
  26:                 num[j++] = num[i]*10+1;
  27:                 i++;
  28:             }
  29:         }
  30:     }
  31:     //system("pause");
  32:     return 0;//
  33: }
  34:  
  35:  
这道题目的BFS比较简单,主要需要注意的是long long的利用,队列都不需要用上。在计算时如果直接利用取模运算应该可以进一步降低运行时间,利用二叉树的性质进行回溯即可
posted on 2012-02-13 09:01  bovine  阅读(152)  评论(0编辑  收藏  举报