待解决poj1426 同余模

来源:互联网 发布:学cnc编程 编辑:程序博客网 时间:2024/05/23 14:28
先贴一个没有技术含量的
#include<iostream>#include<stdio.h>#include<queue>using namespace std;long long q[10000000];long long a[204];long long bfs(int n){   int front=0,tail=0;   q[tail++]=1;   while(1)   {       long long tmp=q[front++];       if(tmp*10%n==0)       return tmp*10;       if((tmp*10+1)%n==0)       return tmp*10+1;       q[tail++]=tmp*10%n;       q[tail++]=(tmp*10+1)%n;   }   return -1;}int main(){    for(int i=0;i<=200;i++)    a[i]=bfs(i);    int n;    while(cin>>n&&n){    cout<<a[n]<<endl;}    return 0;}
#include <iostream>  #include <queue>  #include <string>  using namespace std;  string ans[210];  bool mark[210];  struct node  {      string ans;      int mod;  };  string BFS(int n)  {      memset(mark,0,sizeof(mark));      queue<node>q;      node s;      s.ans="1";       s.mod=1;      q.push(s);      mark[1]=true;      while(!q.empty())      {          node now=q.front(),temp=now;          q.pop();          int c=(now.mod*10+1)%n;          int d=(now.mod*10)%n;          if(d==0)          {              temp.ans+="0";              return temp.ans;          }           if(c==0)          {              temp.ans+="1";              return temp.ans;          }           if(!mark[d])          {              mark[d]=1;              temp.ans+="0";              temp.mod=d;              q.push(temp);          }           if(!mark[c])          {              mark[c]=1;              now.ans+="1";              now.mod=c;              q.push(now);          }      }  }  int main()  {      int n;      for(int i=1;i<=200;i++)      {          if(i&1)              ans[i]=BFS(i);          else              ans[i]=ans[(i>>1)]+"0";      }      while (scanf("%d",&n)&&n)      {          cout<<ans[n]<<endl;      }  } 

先码着 过两天看 貌似内心有点抵触 就是看不会 orz
0 0
原创粉丝点击