hdu4294 Multiple 數學結論 bfs

來源:互聯網
上載者:User

標籤:class   blog   code   get   2014   string   




#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <map>#define inf 0x3f3f3f3f#define ll __int64#define mod 1000000007using namespace std;int n,k,cnt,num[2],len[50010],pre[50010],v[50010];string s,ans;int bfs(){    queue<int> q;    int now,tmp;    memset(len,0,sizeof len);    for(int i=0;i<cnt;i++)    {        if(!num[i]||len[num[i]%n]) continue;//不能0打頭        tmp=num[i]%n;        len[tmp]=1;//串長度        pre[tmp]=-1;//記錄數字位置順序        v[tmp]=num[i];//記錄該位置數位值        q.push(tmp);    }    while(!q.empty())    {        now=q.front();        q.pop();        if(!now) return 1;        if(ans.size() && len[now]>ans.size()) return 0;        for(int i=0;i<cnt;i++)        {            tmp=(now*k+num[i])%n;            if(!len[tmp])            {                len[tmp]=len[now]+1;//串長度                pre[tmp]=now;//記錄數字位置順序                v[tmp]=num[i];//記錄該位置數位值                q.push(tmp);            }        }    }    return 0;}void get(int a){    if(pre[a]!=-1) get(pre[a]);    s+=(char)(v[a]+'0');}bool cmp(string a,string b){    if(b.size()==0||a.size()<b.size()) return 1;    if(a.size()>b.size()) return 0;    return a<b;}int main(){    int i;    while(~scanf("%d%d",&n,&k))    {        int flag=0;        ans="";        for(i=1;i<k;i++)        {            num[0]=i;cnt=1;            if(bfs())            {                flag=1;                s="";                get(0);                if(cmp(s,ans))                    ans=s;            }        }        if(!flag)        {            for(i=1;i<k;i++)            {                for(int j=0;j<i;j++)                {                    num[0]=j;num[1]=i;cnt=2;                    if(bfs())                    {                        flag=1;                        s="";                        get(0);                        if(cmp(s,ans))                            ans=s;                    }                }            }        }        cout<<ans<<endl;    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.