Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
此题有多组解,所以样例没过的同志,也要试试。/*bfs*/ #include <cstdio> #include <cstring> using namespace std; int n; bool flag; struct multi { long long sum; int w; }q[1000005]; void bfs() { int l=1,r=1; struct multi tmp; q[1].sum=1;q[1].w=1; while(l<=r) { for(int i=0;i<=1;i++) { tmp=q[l]; tmp.sum=tmp.sum*10+i; tmp.w++; if(tmp.sum%n==0) { printf("%I64d\n",tmp.sum); flag=true; return ; } r++; q[r]=tmp; } l++; } } int main() { while(scanf("%d",&n) && n) { flag=false; bfs(); } return 0; } /*dfs*/ #include <cstdio> using namespace std; int n; bool flag; long long ans; void js(int w,long long sum) { if(sum%n==0) { flag=true; ans=sum; return ; } if(w>18||flag)return ; for(int i=0;i<=1;i++)js(w+1,sum*10+i); } int main() { while(scanf("%d",&n) && n) { ans=0; flag=false; js(1,1); printf("%I64d\n",ans); } return 0; } //代码奉上 Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator