Find The Multiple
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 18527   Accepted: 7490   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

  1. 2
  2. 6
  3. 19
  4. 0

Sample Output

  1. 10
  2. 100100100100100100
  3. 111111111111111111

在广搜的题中看到这一个,表示根本想不到广搜,,,,,

每一位仅仅能是0或1,那么求n的倍数。从第一位開始搜。一直找到为止。

第一位一定是1,然后存余数temp,假设下一位是1。那么(temp*10+1)%n得到新的余数。假设是0,那么(temp*10)%n得到余数。这样进行广搜。大小是2^100

剪枝的方法:对于每个求的余数,最多有200个,每个仅仅要出现过一次就好了,出现多的减掉

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std ;
  5. struct node{
  6. int k , temp ;
  7. int last ;
  8. }p[1000000] , q ;
  9. int flag[210] , a[120] , n ;
  10. int bfs()
  11. {
  12. int low = 0 , high = 0 ;
  13. p[high].k = 1 ;
  14. p[high].temp = p[high].k % n ;
  15. flag[p[high].temp] = 1 ;
  16. p[high++].last = -1 ;
  17. while( low < high )
  18. {
  19. q = p[low++] ;
  20. if( q.temp == 0 )
  21. return low-1 ;
  22. if( !flag[ (q.temp*10+1)%n ] )
  23. {
  24. p[high].k = 1 ;
  25. p[high].temp = (q.temp*10+1)%n;
  26. flag[ p[high].temp ] = 1 ;
  27. p[high++].last = low-1 ;
  28. }
  29. if( !flag[ (q.temp*10)%n ] )
  30. {
  31. p[high].k = 0 ;
  32. p[high].temp = (q.temp*10)%n ;
  33. flag[ p[high].temp ] = 1 ;
  34. p[high++].last = low-1 ;
  35. }
  36. }
  37. return -1 ;
  38. }
  39. int main()
  40. {
  41. int i , j ;
  42. while(scanf("%d", &n) && n)
  43. {
  44. memset(flag,0,sizeof(flag));
  45. i = 0 ;
  46. j = bfs();
  47. while( j != -1 )
  48. {
  49. a[i++] = p[j].k ;
  50. j = p[j].last ;
  51. }
  52. for(j = i-1 ; j >= 0 ; j--)
  53. printf("%d", a[j]);
  54. printf("\n");
  55. }
  56. return 0;
  57. }

poj1426--Find The Multiple(广搜,智商题)的更多相关文章

  1. POJ3984 BFS广搜--入门题

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20816   Accepted: 12193 Descriptio ...

  2. POJ1426:Find The Multiple(算是bfs水题吧,投机取巧过的)

    http://poj.org/problem?id=1426 Description Given a positive integer n, write a program to find out a ...

  3. poj 1184 广搜进阶题

    起初的想法果然就是一个6000000的状态的表示. 但是后面觉得还是太过于幼稚了. 可以看看网上的解释,其实就是先转换位置,然后再改变数字的大小. #include<iostream> # ...

  4. hdu 1180(广搜好题)

    诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Subm ...

  5. hdu 2612:Find a way(经典BFS广搜题)

    Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. poj 3984:迷宫问题(广搜,入门题)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description ...

  7. hdu 1253:胜利大逃亡(基础广搜BFS)

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  8. Eight_pku_1077(广搜).java

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21718   Accepted: 9611   Special ...

  9. HDU 2267 How Many People Can Survive(广搜,简单)

    题目 //一道简单的广搜水题 #include<queue> #include<stdio.h> #include<string.h> #include<al ...

随机推荐

  1. jorgchart,帮助你生成组织结构图的

    下载地址: http://yunpan.cn/c6pfenkmmFV2q  访问密码 8e29 演示链接: http://www.gbtags.com/gb/share/546.htm jstree. ...

  2. Cracking the Coding Interview 8.7

    Given a infinite number of quarters(25cents), dimens(10cents), nickels(5cents) and pennies(1cent), w ...

  3. POJ 3083 BFS+DFS 40行

    题意:给你一个迷宫. 先输出当左转优先的时候走的路程长度,再输出当右转优先时走的路程长度,最后输出从起点到终点的最短路程长度. 嗯嗯 奴哥活跃气氛的题.随便写了写.. 此题 知道了思路以后就是水题了. ...

  4. Laravel5.1 学习笔记1, 目录结构和命名空间(待修)

    自从用 Laravel4做了个小网站,使用了数据库ORM Eloquent, 就放下了一段时间,想不到这个与Asp.net MVC 有着异曲同工之妙的框架已经出了下个版本,而且还有不小的改动,因此不得 ...

  5. Java导入excel并保存到数据库

    首先建立好excel表格,并对应excel表格创建数据库表. 前台jsp页面:其中包含js <%@ page language="java" import="jav ...

  6. SQL Server存储过程作业(二)

    阶段1:练习——统计某类型客房的入住客人人数 需求说明 使用存储过程统计在指定类型的客房入住客人的总人数 提示: 存储过程的输入参数是指定的客房类型名称 USE Hotel GO --阶段1:查询入住 ...

  7. 推荐系统:MovivLens20M数据集解析

    MovieLens 是历史最悠久的推荐系统.它由美国 Minnesota 大学计算机科学与工程学院的 GroupLens 项目组创办,是一个非商业性质的.以研究为目的的实验性站点.MovieLens ...

  8. VTK嵌入MFC同步显示

    使用VTK嵌入MFC,实现四视图更新,机制和细节参考原文. 原文链接:http://blog.csdn.net/www_doling_net/article/details/8939115 原文代码: ...

  9. 国外AI界牛人主页 及资源链接

    感觉 好博客要收集,还是贴在自己空间里难忘!!! 原文链接:http://blog.csdn.net/hitwengqi/article/details/7907366 http://people.c ...

  10. Jsp页面中常用的EL表达式

    首先引入标签 <%@ page language="java" contentType="text/html; charset=utf-8"  pageE ...