码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1426

时间:2015-04-13 22:27:09      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

题意 : 给定一个n求只包括0与1的数 能被n整除 任意一个答案就可以

#include<iostream>
#include<stdio.h>
#include<cstring>
#include <queue> 
using namespace std; 
 
#define CLR(arr,val) memset(arr,val,sizeof(arr)) 
typedef long long LL; 
int flag[210]; 
LL bfs(int n){ 
    CLR(flag,0); 
    queue<LL> qq; 
    qq.push(1); 
    flag[1%n] = 1; 
    while(!qq.empty()){ 
        LL x = qq.front(); 
        if(x % n == 0) 
            return x; 
        qq.pop(); 
        LL y = x * 10; 
        int yy = y % n; 
        LL z = x * 10 + 1; 
        int zz = z % n; 
        if(!flag[yy]){ //这里为减枝  就是求最短的那个答案
           qq.push(y); 
           flag[yy] = 1; 
        } 
        if(!flag[zz]){ 
           qq.push(z); 
           flag[zz] = 1; 
        } 
    } 
} 
int main(){ 
    //freopen("1.txt","r",stdin); 
    int n; 
    while(scanf("%d",&n) &&n){ 
       LL ans = bfs(n); 
       printf("%lld\n",ans); 
    } 
    return 0; 
}

 

poj 1426

标签:

原文地址:http://www.cnblogs.com/zhangdashuai/p/4423150.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!