hdu 1664 Different Digits (bfs+取余判重+数论知识)

Different Digits

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


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
  
  
7 555 16 1111
 

Source

ps:一个数论中的结论: 对于任意的整数n,必然存在一个由不多于两个的数来组成的一个倍数。因为a,aa,aaa……取n+1个,则必有两个模n余数相同,相减即得n的倍数m。而m只由a、0组成。
有了这个结论之后就好下手了,所以数论还是挺重要的。

解题思路:
先用一个数来搜答案,9种情况,已经搜到答案就输出,没有则再用两个数来搜答案,36种情况。当然,要注意答案必须是不同的数的个数最少,有不同答案的话再输出最小的。

思维误区:
搜索从小到大同时进行。错在这样搜不能简单的取余判重。例子17,如果从小到大进行+取余判重,搜到的答案就为17,而答案应为1111111111111111。因为不同的数在进行搜索时,取余判重相互影响,像这样111根本就不会进栈,因为vis[6]已经进栈了。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#define maxn 65540
using namespace std;

int n,m,mcnt,tcnt,h;
int a[5];
bool vis[maxn];  // 取余判重
string ans,tans;
struct Node
{
    int d,val,pre; // val-有效值
    int cnt;
} cur,now,q[maxn];

void getans(int k)// 递归得到ans 避免在结构体中添加string 每次操作string很拖时间
{
    char c;
    if(k==-1) return ;
    else
    {
        getans(q[k].pre);
        c=q[k].d+'0';
        tans+=c;
    }
}
bool bfs(int k)
{
    int i,j,nval,ncnt,tval;
    int head=0,tail=-1;
    memset(vis,0,sizeof(vis));
    for(i=1; i<=k; i++)
    {
        if(a[i])
        {
            cur.cnt=0;
            cur.d=a[i];
            cur.pre=-1;
            cur.val=a[i]%n;
            vis[cur.val]=1;
            q[++tail]=cur;
        }
    }
    while(head<=tail)
    {
        now=q[head];
        nval=now.val;
        ncnt=now.cnt;
        if(ncnt>mcnt) break ;  // 剪枝
        if(!nval)
        {
            h=head;
            tcnt=ncnt;
            return true ;
        }
        for(i=1; i<=k; i++)
        {
            tval=(nval*10+a[i])%n;
            if(!vis[tval])
            {
                vis[tval]=1;
                cur.cnt=ncnt+1;
                cur.d=a[i];
                cur.pre=head;
                cur.val=tval;
                q[++tail]=cur;
            }
        }
        head++;
    }
    return false;
}
int main()
{
    int i,j,flag;
    while(scanf("%d",&n),n)
    {
        flag=0;
        ans="xx";
        mcnt=1000000000;
        for(i=1; i<=9; i++)    // 先搜一个数能否组成n的倍数
        {
            a[1]=i;
            if(bfs(1))
            {
                tans="";
                getans(h);
                if(mcnt>tcnt||mcnt==tcnt&&ans>tans) // 注意怎么比较ans的大小
                {
                    ans=tans;
                    mcnt=tcnt;
                }
            }
        }
        if(ans!="xx")
        {
            flag=1;
            cout<<ans<<endl;
        }
        if(flag) continue ;
        for(i=0; i<=9; i++)  // 再搜两个数
        {
            a[1]=i;
            for(j=i+1; j<=9; j++)
            {
                a[2]=j;
                if(bfs(2))
                {
                    tans="";
                    getans(h);
                    if(mcnt>tcnt||mcnt==tcnt&&ans>tans)
                    {
                        ans=tans;
                        mcnt=tcnt;
                    }
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
/*
17
343
818
234
*/



 
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值