【搜索】 HDU 1664 Different Digits

//对于任意的整数n,必然存在一个由不多于两个的数来组成的一个倍数。

。。。所以最多有2个数字

先判断1个数字的情况没有再判断2个数字的情况

在s串上每次添加1个数判断是否被n整除

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <math.h>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define cler(arr, val)    memset(arr, val, sizeof(arr))
#define IN     freopen ("in.txt" , "r" , stdin);
#define OUT  freopen ("out.txt" , "w" , stdout);
typedef long long  LL;
const int MAXN = 66666;//点数的最大值
const int MAXM = 20006;//边数的最大值
const int INF = 1101521204;
const int mod = 10000007;
int num[MAXN],ans,n;
int pre[MAXN],re[MAXN],out[MAXN];
string s;
void print(int x)
{
    if(x!=-1)
        print(pre[x]);
    else return ;
    s+=(out[x]+'0');
}
string bfs(int x,int y)
{
    s="";
    int tol=0;
    cler(num,0);
    if(x!=0)
    {
        pre[tol]=-1;
        out[tol]=x;
        num[x%n]=1;
        re[tol++]=x%n;
    }
    if(!num[y%n])
    {
        pre[tol]=-1;
        out[tol]=y;
        num[y%n]=1;
        re[tol++]=y%n;
    }
    for(int i=0; i<tol; i++)
    {
        if(re[i]==0)
        {
            print(i);
            return s;
        }
        else
        {
            int temp=re[i]*10+x;
            if(!num[temp%n])
            {
                pre[tol]=i;
                out[tol]=x;
                num[temp%n]=1;
                re[tol++]=temp%n;
            }
            temp=re[i]*10+y;
            if(!num[temp%n])
            {
                pre[tol]=i;
                out[tol]=y;
                num[temp%n]=1;
                re[tol++]=temp%n;
            }
        }
    }
    return s;
}
bool cmp(string temp,string ans)
{
    if(temp.size()!=ans.size())
    {
        return temp.size()<ans.size();
    }
    return temp<ans;
}
int main()
{
    //IN;
    while(scanf("%d",&n),n)
    {
        string ans="",temp;
        for(int i=1; i<=9; i++)
        {
            temp=bfs(i,i);
            //cout<<temp<<endl;
            if(temp.size()==0) continue;
            if(ans.size()==0||cmp(temp,ans))
                ans=temp;
        }
       //     cout<<ans<<endl;
        if(ans.size()==0)
        {
            for(int i=0; i<=9; i++)
            {
                for(int j=i+1; j<=9; j++)
                {
                    temp=bfs(i,j);
                 //   cout<<temp<<endl;
                    if(temp.size()==0) continue;
                    if(ans.size()==0||cmp(temp,ans))
                        ans=temp;
                   // cout<<ans<<i<<j<<endl;
                }
            }
        }
        cout<<ans<<endl;
    }
}


posted @ 2014-09-16 20:34  kewowlo  阅读(142)  评论(0编辑  收藏  举报