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

2
6
19
0

Sample Output

10
100100100100100100
111111111111111111

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

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

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

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

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
struct node{
int k , temp ;
int last ;
}p[1000000] , q ;
int flag[210] , a[120] , n ;
int bfs()
{
int low = 0 , high = 0 ;
p[high].k = 1 ;
p[high].temp = p[high].k % n ;
flag[p[high].temp] = 1 ;
p[high++].last = -1 ;
while( low < high )
{
q = p[low++] ;
if( q.temp == 0 )
return low-1 ;
if( !flag[ (q.temp*10+1)%n ] )
{
p[high].k = 1 ;
p[high].temp = (q.temp*10+1)%n;
flag[ p[high].temp ] = 1 ;
p[high++].last = low-1 ;
}
if( !flag[ (q.temp*10)%n ] )
{
p[high].k = 0 ;
p[high].temp = (q.temp*10)%n ;
flag[ p[high].temp ] = 1 ;
p[high++].last = low-1 ;
}
}
return -1 ;
}
int main()
{
int i , j ;
while(scanf("%d", &n) && n)
{
memset(flag,0,sizeof(flag));
i = 0 ;
j = bfs();
while( j != -1 )
{
a[i++] = p[j].k ;
j = p[j].last ;
}
for(j = i-1 ; j >= 0 ; j--)
printf("%d", a[j]);
printf("\n");
}
return 0;
}

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 ...