题意 : 给定一个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 ",ans); } return 0; }