Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

利用stl队列+宽搜79ms过了,但是要剪枝,不然就TLE。

Posted by xym_sun at 2013-01-26 00:00:21 on Problem 1426
//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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator