POJ-1426 Find The Multiple 解题报告

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.

Inpu。

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


       题目链接:http://poj.org/problem?id=1426

       解法类型:BFS或DFS

       解题思路:这题目很有意思。刚开始看到no more than 100 decimal digits,没细想,就以为最大的可能位数会达到100位,然后memory要求1000KB,就以为用BFS不现实。果断用了DFS做,结果一提交,居然TE,于是就测试了一下位数,发现最大的数就是199的,才19位而已啊!这下被坑了,于是就用了DFS+打表,以为绝对会过,结果WA,这是最让我郁闷的地方。最后,既然最大的位数才19位而已,那就直接BFS就水吧。。。水就水吧,结果还是TE ~= =!在绝望的时候用G++交,这一交就AC!时间才188MS啊,用C++就超时,神马情况啊。而且用BFS+打表C++居然也超时。一度怀疑此题的SPJ有问题。。。

       算法实现:

//STATUS:G++_AC_141MS_2728KB
#include<stdio.h>
#include<queue>
int BFS();
__int64 a,sav[201];
using namespace std;
int main()
{
//	freopen("in.txt","r",stdin);
	for(a=1;a<=200;a++){
			BFS();
	}
	while(scanf("%I64d",&a)&&a)
	{
		printf("%I64d,",sav[a]);
	}
	return 0;
}

int BFS()
{
	__int64 x;
	queue<__int64>q;
	q.push(1);
	while(!q.empty())
	{
		x=q.front();
		q.pop();
		if(x%a==0){sav[a]=x;return 1;}
		q.push(x*10+1);
		q.push(x*10);
	}
	return 0;
}

       再贴上非常暴力的DFS代码(虽然WA,但我觉得是SPJ的问题,算法很容易实现):

//STATUS:C++_WA_?_?
#include<stdio.h>
#include<memory.h>
int DFS(int cur);
int is_01(int cur);
const int MAXN=110;
int ok[MAXN],tot[210],a,b[MAXN],d[2]={1,0},bt[210][MAXN];
int main()
{
//	freopen("in.txt","r",stdin);
	int i,j,max;
	for(a=1;a<=200;a++){
		ok[a]=0;
		memset(b,0,sizeof(b));
		b[0]=1;

		DFS(1);

		for(j=0;j<19;j++)
			bt[a][j]=b[j];
	}

	while(scanf("%d",&a)&&a)
	{
		max=tot[a];
		printf("0\n");
	}
	return 0;
}

int DFS(int cur)
{
	if(is_01(cur)){ok[a]=1;tot[a]=cur;return 1;}
	if(cur<19){
    	for(int i=0;i<2;i++)
		{
	     	b[cur]=d[i];
	    	if(DFS(cur+1))return 1;
	    	b[cur]=0;
		}
	}
	return 0;
}

int is_01(int cur)
{
	int i,t;
	for(i=0,t=0;i<cur;i++){
		t=t*10+b[i];
		t%=a;
	}
	if(t)return 0;
	else return 1;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值