POJ 1426 Find The Multiple

来源:互联网 发布:php 开源 报表系统 编辑:程序博客网 时间:2024/04/29 20:06
/*这道题要解决的一个问题就是当数字太大的时候int存下了,不能直接进行模运算可以用这个公式:(a*b)%n=(a%n * b%n)%n(a+b)%n=(a%n + b%n)%n*/#include<stdio.h>#include<string>#include<queue>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(dsi));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";//因为n的取值范围有限,直接事先计算出来else bfs(i);}while(scanf("%d",&n) && n) {cout<<node[n].ans<<endl;}return 0;}

0 0