Different Digits HDU - 1664

题目链接:Different Digits HDU - 1664

===================================================

Different Digits

Time Limit: 10000/4000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)

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

===================================================

算法:数论+BFS

思路:

  • 感觉这题集出得挺不错,每道题总与上一道题有关联。这一题和上一题一样,也是求N的正整数倍。所以也是余数下手。

  • 但是这题巧妙在于,最优解是,答案需尽量少不同数字构成且最小。

  • 这里牵扯的数论就是:
    在这里插入图片描述
    很好理解:求10进制的余数,(*10+1)MOD,因为只加一个数字的求余存在一个循环,存在0则为答案。余数相同,就互减得到答案,互减将余数减掉就是N的整数倍了。所以一个整数一定存在一个正整数倍的数,且由0和1的数构成。

  • 那么这题剩下就是求一个最优最小解,即不同数字的数量相同求最小。

  • 分两层,第一层只用一个数叠加求,得到答案就是最优。得不到去第二层,循环从不同两个数字组合求出答案。

===================================================

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <map>

using namespace std;

const int inf = 2e9;

int N,ansLen;
bool vis[65536];
string ans,res;

bool gO(int x){
    memset(vis,false,sizeof vis);
    int tmp = 0;res = "";
    while(1){
        tmp = (tmp*10+x) % N;
        if(vis[tmp]) return false;
        vis[tmp] = true;
        res += '0' + x;
        if(!tmp){
            if(ansLen == inf||ans.length()>res.length()){
                ans = res;ansLen = res.length();return true;
            }
            return false;
        }
    }
}

int val[65536],pre[65536],cnt[65536];

void getAns(int x){
    if(x==-1) return ;
    getAns(pre[x]);
    res += '0' + val[x];
}

bool gT(int x,int y){
    memset(vis,false,sizeof vis);
    memset(pre,-1,sizeof pre);
    memset(cnt,0,sizeof cnt);
    queue<int> q;
    int fk,num[2]={x,y};
    if(x) { fk = x%N;vis[fk] = true,val[fk] = x,cnt[fk] = 1;q.push(fk); }
    fk = y%N; if(!vis[fk]) {vis[fk] = true,val[fk] = y,cnt[fk] = 1;q.push(fk);}
    while(!q.empty()){
        int now = q.front();q.pop();
        if(cnt[now]>ansLen) return false;
        for(int i=0;i<2;i++){
            fk = (now*10 + num[i]) % N;
            if(vis[fk]) continue;
            vis[fk] = true,val[fk] = num[i],cnt[fk] = cnt[now] + 1,pre[fk] = now;
            if(!fk){
                res = "";getAns(0);
                if(ansLen==inf||ans.length()>res.length()||(ans.length()==res.length()&&res<ans)){
                    ans = res;ansLen = res.length();
                    return true;
                }
                return false;
            }
            q.push(fk);
        }
    }
    return false;
}

int main()
{
    while(cin>>N&&N){
        int flag = 0; ansLen = inf;ans = "";
        for(int i=1;i<10;i++) if(gO(i)) flag = 1;
        if(flag) {cout<<ans<<endl;continue;}
        for(int i=0;i<10;i++) for(int j=i+1;j<10;j++) gT(i,j);
        cout<<ans<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盐太郎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值