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. 0502 php简单了解

    准备工作: 安装好wamp,配置站点:apache2.4.9\conf\httpd.conf 注意事项: 1.必须有分号 2.不要有无意义空行,会以空格形式输出. 3.变量,关键字(if,for..) ...

  2. 辨析 singleton 和 prototype

    <bean id="person1" class="com.bean.life.Person"> <property name="n ...

  3. Phoenix与Squirrel 是什么?

    不多说,直接上干货! 前言 Phoenix是HBase的开源SQL引擎. squirrel是windows上Phoneix可视化工具.  Phoenix的官网 http://phoenix.apach ...

  4. Javascrpt核心概念(2)--操作符

    学习操作符这个章节最好回忆一下C里的惯例,因为ECMAScript的标准很多继承自C的语法 一元操作符 只能操作一个值得操作符 递增和递减操作符 var age = 29; ++age; //30 v ...

  5. 解决:惠普HP LaserJet Pro M126a MFP 驱动 安装失败,及其它同类打印机失败问题

    注意:如果在 Windows XP 系统下安装出错,请先安装WindowsXP KB971276-v3补丁后再安装装驱动. 下载地址:http://www.dyjqd.com/soft/KB97127 ...

  6. IIS日志分析:SC-Status语义

    在网站属性-网站-日志(属性) 中进行设定该站点IIS日志常规属性和扩展属性,扩展属性设置IIS日志包含字段显示. HTTP协议状态(sc-status)码的含义  IIS中 100 Continue ...

  7. 极客学院免费VIP

    [手快福利]用我的链接注册极客学院,你我都能免费得30天VIP!6500+编程开发视频教程随便学,还能下载资料和源码 http://e.jikexueyuan.com/invite/index.htm ...

  8. Data mapping-数据映射

    数据映射:根据数据的结构信息建立数据间的映射操作机制. 数据映射的要素: 一.数据 1.源数据: 2.目标数据: 3.数据间关系: 4.数据的元数据(结构信息). 5.元素类型的对应关系. 二.元数据 ...

  9. APICloud 获取缓存以及清除缓存(常用第三方方法)

    一.app中经常会有缓存的清除这个操作,具体如下 1.获取缓存大小 apiready = function() { api.getCacheSize(function(ret, err) { //si ...

  10. C#第十二节课

    数组 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Th ...