HDU1664 Different Digits(bfs +数论剪枝)

Different Digits
Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1129 Accepted Submission(s): 290

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
2004 Asia Regional Shanghai

Recommend
xhd | We have carefully selected several similar problems for you: 1667 1668 1666 1661 1662

感觉是ZOJ1136 的高级版http://blog.csdn.net/u013167299/article/details/47170483


#include<bits/stdc++.h>
#define cl(a,b) memset(a,b,sizeof(a));
#define LL long long
#define out(x) cout<<x<<endl;
using namespace std;
const int maxn=65540;
const int inf=9999999;
int n;
int a[2];
int used[maxn];
struct node{
    int pre,d,mod,id;//pre是该节点的前驱节点,d是当前加在末尾的数,mod是到目前为止组合的数对n的余数,id是编号,便于查找,
    node(){}
    node(int a,int b,int c,int tt){
        pre=a;d=b;mod=c;id=tt;
    }

}q[maxn];//直接模拟队列,便于记录路径
string ans,tmpans;

bool bfs(int m){
    int sum=0;
    int st=0,en=0;
    cl(used,0);
    for(int i=0;i<m;i++){
        q[en++]=node(-1,a[i],a[i]%n,sum++);
        if(a[i])used[a[i]%n]=1;
    }
    int len=0;
    while(st<en){
        node s=q[st++];
        if(s.mod==0&&(s.id>0||s.id==0&&s.d!=0)){
            tmpans="";
            tmpans+=char(s.d+'0');
            int t=s.pre;
            while(t!=-1){//路径转化成字符串
                tmpans+=q[t].d+'0';
                t=q[t].pre;
            }
            reverse(tmpans.begin(),tmpans.end());
            //out(tmpans)
            return true;
        }
        for(int i=0;i<m;i++){
            node tmp=s;
            if(len==0&&tmp.d==0&&tmp.mod==0)continue;
            if(used[(tmp.mod*10+a[i])%n]++)continue;
            len++;
            q[en++]=node(s.id,a[i],(s.mod*10+a[i])%n,sum++);
        }
    }
    return false;
}

int main(){
    while(~scanf("%d",&n)&&n){
        ans="zcc";
        for(int i=1;i<10;i++){
            a[0]=i;
            if(bfs(1)){
                if(ans=="zcc")ans=tmpans;
                else if(tmpans.size()<ans.size()){
                    ans=tmpans;
                }
                else if(ans.size()==tmpans.size()){
                    ans=min(ans,tmpans);
                }
            }
        }
        if(ans=="zcc")for(int i=0;i<10;i++){
            a[0]=i;
            for(int j=i+1;j<10;j++){
                a[1]=j;
                if(bfs(2)&&tmpans!="0"){
                    if(ans=="zcc")ans=tmpans;
                    else if(tmpans.size()<ans.size()){
                        ans=tmpans;
                    }
                    else if(ans.size()==tmpans.size()){
                        ans=min(ans,tmpans);
                    }
                }
            }
        }
        out(ans)
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值