poj1426 DFS BFS

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

题意

刚看到数据时候把我吓到了,一想是不是要用大数,要用数组存呢?

然后看了一下 讨论 ,顿时感觉自己被骗了..用数据不会超过long long的.

以后要自己先尝试一下,防止出现类似情况


#include<iostream>  //   5080K  547MS
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<queue>

using namespace std;

queue<long long> Q;
int n;
long long  p;
int BFS()
{
	while(!Q.empty())  //否则会MLE
		Q.pop();
	
	Q.push(1);
	while(!Q.empty())
	{
		p=Q.front();
		Q.pop();
		for(int i=0;i<2;i++)
		{
			if(p%n==0)
			{
				printf("%lld\n",p);
				return 1;
			}
			if(i==0)
				Q.push(p*10);
			if(i==1)
				Q.push(p*10+1);
		}
	}
	return 0;
}
int main()
{
	while(~scanf("%d",&n),n)
	{
		BFS();
	}
}
//704K   157Ms
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
bool found;
void DFS( long long sum ,int n,int k)
{   if(k==19)  //防止爆<span id="transmark"></span>
        return ;
    if(found)   //找到了就停止其他递归
        return ;
    if(sum%n==0)
    {
        printf("%lld\n",sum);
        found=true;
        return ;
    }
    DFS(sum*10,n,k+1);
    DFS(sum*10+1,n,k+1);
}
int main()
{
    int n;
    while(~scanf("%d",&n),n)
    {
        found=false;
        DFS(1,n,0);
    }
    return 0;
}

你可能感兴趣的:(span,idtransmarkspa,idtransmarkspan,bspan)