hdu1664 Different Digits (搜索)

来源:互联网 发布:美国网络中立 编辑:程序博客网 时间:2024/06/05 07:11

Different Digits

Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 734    Accepted Submission(s): 173


Problem Description
Given a positive integer n, your task is to find a positive integer m, which is a multiple of n, and that m contains the least number of different digits when represented in decimal. For example, number 1334 contains three different digits 1, 3 and 4.
 

Input
The input consists of no more than 50 test cases. Each test case has only one line, which contains a positive integer n ( 1<=n < 65536). There are no blank lines between cases. A line with a single `0' terminates the input.
 

Output
For each test case, you should output one line, which contains m. If there are several possible results, you should output the smallest one. Do not output blank lines between cases.
 

Sample Input
7 15 16 101 0
 

Sample Output
7555161111
 

Source
2004 Asia Regional Shanghai
 

Recommend
xhd
 

题意:求在使用最少不同数字的前提且结果尽可能小的情况下,求n的倍数。
数论知识,最多使用两个不同数字即可组成任意数字的倍数。然后搜索,此题是POJ 1465: Multiple的升级版,大家可以先把1465过了,
这道题就简单了。
代码:
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<algorithm>//#pragma comment (linker,"/STACK:1024000000000,102400000000")#define INF 0x3f3f3f3f#define MAXN 66666using namespace std;int n,curcnt,anscnt,da[11];bool done[MAXN];string ans,curans;struct node{    int d,yu,pre,cnt;    node(int dd=0,int vv=0,int pp=0,int cc=0)    {        d=dd;yu=vv;pre=pp;cnt=cc;    }}q[MAXN];bool dfs(int k){    int st=0,en=0;    memset(done,0,sizeof done);    q[en++]=node(0,0,-1,0);    while(st<en)    {        node x=q[st++];        if(x.cnt==anscnt)return 0;        for(int i=0;i<k;i++)        {            if(x.yu==0&&da[i]==0)continue;            int curyu=(x.yu*10+da[i])%n;            if(done[curyu])continue;            if(curyu==0)            {                curans="";                curcnt=x.cnt+1;                curans+=da[i]+'0';                int ii=st-1;                while(q[ii].pre!=-1)                {                    curans+=q[ii].d+'0';                    ii=q[ii].pre;                }                reverse(curans.begin(),curans.end());                return 1;            }            done[curyu]=1;            q[en++]=node(da[i],curyu,st-1,x.cnt+1);        }    }    return 0;}int main(){    int i,j;    while(~scanf("%d",&n)&&n)    {        anscnt=70000;        for(da[0]=1;da[0]<=9;da[0]++)            if(dfs(1))            {                if(anscnt>curcnt)                {                    ans=curans;                    anscnt=curcnt;                }            }        if(anscnt==70000)        for(da[0]=0;da[0]<=9;da[0]++)            for(da[1]=da[0]+1;da[1]<=9;da[1]++)                if(dfs(2))                {                    if(anscnt>curcnt)                    {                        ans=curans;                        anscnt=curcnt;                    }                    else if(anscnt==curcnt&&curans<ans)                    {                        ans=curans;                    }                }        cout<<ans<<endl;    }    return 0;}


0 0
原创粉丝点击