POJ 1426 Find The Multiple (广搜)

Find The Multiple

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20235   Accepted: 8203   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

题目大意:给一个数n,求最小的k,使得k是n的整数倍,且k的十进制表示只有0和1.

题目上说这样的k不会超过200位,确实是真的,不过这样太吓人了,其实在1~200里的全部n的结果k都是在64位之内的,它特意说不超过200位,一开始把我吓坏了,这题用广搜怎么可能做呢,太坑爹了这题。

广搜,其实这个全部数据里最大的也不过是n=198时,k=1111111111111111110(不用数了,一共18个1,1个0)

AC代码:

#include <stdio.h>
#include <queue>
using namespace std;
typedef __int64 ll;

void bfs(int n){
	queue<ll> que;
	que.push(1);
	while(!que.empty()){
		ll t=que.front();
		que.pop();
		if(t%n==0){
			printf("%I64d\n",t);
			return ;
		}
		que.push(t*10);
		que.push(t*10+1);
	}
}

int main()
{
	int i,n;
	while(scanf("%d",&n),n)
		bfs(n);
	return 0;
}

下面还有一个代码,写的基本上一致,只不过把bfs改成了__int64的带返回值,然后在main输出结果,W!A!了。。。。我不明白,路过的大神帮忙指点一下可好

#include <stdio.h>
#include <queue>
using namespace std;
typedef __int64 ll;

ll bfs(int n){
	queue<ll> que;
	que.push(1);
	while(!que.empty()){
		ll t=que.front();
		que.pop();
		if(t%n==0)
			return t;
		que.push(t*10);
		que.push(t*10+1);
	}
}

int main()
{
	int i,n;
	while(scanf("%d",&n),n)
		printf("%I64d\n",bfs(n));
	return 0;
}

时间: 2024-10-10 09:48:03

POJ 1426 Find The Multiple (广搜)的相关文章

POJ 1426 Find The Multiple (深度搜素)

对比与其他的变态搜索题目来说,这道题目就系显得特别友好: 之所以贴上这道题目,是因为还能才能从这道题目中学到东西 1.找到自己想要的搜索结果之后,如何终止搜索 2.如何储存最大的数值 AC CODE: #include<iostream> #include<cstdio> #include<stack> using namespace std; unsigned __int64 tar,ans; bool vis; void dfs(unsigned __int64 x

广搜+打表 POJ 1426 Find The Multiple

POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Accepted: 10613   Special Judge Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representati

POJ 1426 Find The Multiple(寻找倍数)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

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

迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要

DFS/BFS(同余模) POJ 1426 Find The Multiple

题目传送门 1 /* 2 题意:找出一个0和1组成的数字能整除n 3 DFS:200的范围内不会爆long long,DFS水过~ 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-2 14:21:51 8 File Name :POJ_1426.cpp 9 ******************************************

[题解] [BFS] POJ 1426 - Find The Multiple

p { margin-bottom: 0.25cm; line-height: 115% } a:link { } VJudge题目:https://cn.vjudge.net/contest/279018#problem/L 即POJ 1426 - Find The Multiple:http://poj.org/problem?id=1426 题目要求: 给出一个不大于200的整数n,要求找到任意一个n的倍数(十进制),并且这个倍数只由1与0组成,最多100位. 输入输出:输入以0结束. 示

poj 3126 prime path 简单广搜

http://poj.org/problem?id=3126 题意:给你两个四位数a,b,从a开始    每次只能改变上一次数的其中一位,问至少需要几步才能得到b 分析:求最小路   典型的广搜   表面上是    40入口的bfs   但是除去有的数不是素数   入口数远小于40 可以写一个  判断一个数是否为素数的函数  , 每次去 调用 判断一个数是否要进队列 也可以 事先打一个素数表  这样会快点 注意:output  :either with a number stating the

[深度优先搜索] POJ 1426 Find The Multiple

Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28550   Accepted: 11828   Special Judge Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains

poj1426--Find The Multiple(广搜,智商题)

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