POJ - 1426 Find The Multiple

给你一个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;  
}  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值