标签:一个队列 href end 运算 more net ota mon and
| Time Limit: 1000MS | Memory Limit: 10000K | |||
| Total Submissions: 18012 | Accepted: 7297 | Special Judge | ||
Description
Input
Output
Sample Input
2 6 19 0
Sample Output
10 100100100100100100 111111111111111111
Source
#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;
void bfs(int n)
{
queue<__int64>q;//这里后面的数据可能会有大的,所以用个64位的就够了
q.push(1);
while(!q.empty())
{
__int64 x;
x=q.front();
q.pop();
if(x%n==0)//能够整除就直接输出
{
printf("%I64d\n",x);
return ;
}
q.push(x*10);//把x的10的倍数入队。
q.push(x*10+1);
}
}
int main()
{
int n;
while(scanf("%d",&n)&&n)
{
bfs(n);
}
return 0;
}#include <cstdio>
#include <cstring>
long long q[2000000];
void bfs(int n)
{
int front=0;
int rear=0;
q[front]=1;
rear++;
long long temp;
while(rear>front)
{
temp=q[front];
if(temp%n==0)
{
break;
}
temp*=10;
q[rear]=temp;
rear++;
q[rear]=temp+1;
rear++;
front++;
}
printf("%lld\n",temp);
}
int main()
{
int n;
while(scanf("%d",&n)&&n)
{
bfs(n);
}
return 0;
}
(a*b)%n = (a%n *b%n)%n
(a+b)%n = (a%n +b%n)%n
再贴一段pl大牛关于此题的分析思路。以学习:
要m整除n,那么能够用对n的余数来表示当前的状态。
搜到一个余数为0的状态就能够了。
直接bfs出去。
假设当前的余数是r。添在当前答案后面的数为,k
那么新的余数。也就是新的状态为:
( r * 10 + k ) % n 。
这样最多200个状态,判重一下。能够非常快出解。
大牛的代码:
#include<iostream>
using namespace std;
int a[524300],i,n;
int main(){
while(cin>>n){
if (!n) break;
i=1; a[1]=1%n;
while(a[i]){i++; a[i]=(a[i/2]*10+i%2)%n;}
n=0; while(i){a[n++]=i%2;i>>=1;}
while(n--) cout<<a[n]; cout<<endl;
}
return 0;
}这原来也是bfs,直接用一个满二叉树实现(左儿子是0,右儿子是1);
还是要多学习大神们的代码~
poj 1426 Find The Multiple (bfs 搜索)
标签:一个队列 href end 运算 more net ota mon and
原文地址:http://www.cnblogs.com/clnchanpin/p/6784553.html