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 |
利用stl队列+宽搜79ms过了,但是要剪枝,不然就TLE。//1426 Find The Multiple #include<iostream> #include<fstream> #include<cstdio> #include<cstring> #include<string> #include<vector> #include<queue> #include<algorithm> #include<cmath> #include<set> #include<map> using namespace std; //ifstream fin("input.in"); //#define cin fin int n; int vis[220]; int getmod(string &str, int m) { int len = str.length(); int ans = 0; for(int i=0;i<len;i++) ans = (ans*10+str[i]-'0')%m; return ans; } void bfs() { memset(vis,0,sizeof(vis)); if(n==1) {printf("1\n");return;} queue<string> q; string u = "1"; q.push(u); while(!q.empty()) { u = q.front(); q.pop(); string v; int m; v = u+"0"; m = getmod(v,n); if(!m) { cout<<v<<"\n"; return; } if(!vis[m]) { vis[m] = 1; q.push(v); } v = u+"1"; m = getmod(v,n); if(!m) { cout<<v<<"\n"; return; } if(!vis[m]) { vis[m] = 1; q.push(v); } } } int main() { //freopen("input.in","r",stdin); while(~scanf("%d",&n)) { if(n==0) break; bfs(); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator