给你一个n(n<=200),输出是n的倍数并且全部由十进制的01构成的所有情况中的一种。
这道题的难点在于对一个公式的理解,假设一串数字是abcde, 让它对p取余,结果和(((((a%p)*10+b)%p)*10+d)%10)*10+e)%p等价,所以我们枚举01序列进行取余操作,就能得到结果,不用担心数据太大。
#include <iostream>
#include <string>
#include <queue>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef struct{
int mod;
string ans;
}Node;
Node node[210],temp,now;
bool dis[210];
void bfs(int i){
int x,y;
queue <Node>q;
memset(dis,false,sizeof(dis));
dis[1]=true;
temp.ans="1";
temp.mod=1;
q.push(temp);
while(!q.empty()){
now=temp=q.front();
q.pop();
x=(temp.mod*10+1)%i;
y=(temp.mod*10)%i;
if(!x){
node[i].ans=temp.ans+"1";
return ;
}
if(!y){
node[i].ans=temp.ans+"0";
return ;
}
if(!dis[x]){
temp.ans=temp.ans+"1";
temp.mod=x;
q.push(temp);
dis[x]=true;
}
if(!dis[y]){
now.ans=now.ans+"0";
now.mod=y;
q.push(now);
dis[y]=true;
}
}
}
int main(){
int i,n;
node[1].ans="1";
for(i=2;i<=200;i++){
if(i%2==0) node[i].ans=node[i/2].ans+"0";
else bfs(i);
}
while(scanf("%d",&n) && n){
cout<<node[n].ans<<endl;
}
return 0;
}