hdu 1664 Different Digits

本文介绍了一道算法题目,要求找到一个正整数n的倍数m,使得m用最少的不同数字表示。通过广度优先搜索算法实现,考虑了只使用一种数字和两种数字的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Different Digits

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


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


题意:告诉一个数n,求一个他的倍数m,使得m由最少的不同数字组成。

思路:对于任意一个n,都可以找到一个他的倍数,并且这个倍数只有最多2种不同的数字组成。所以,可以分成1种和2种讨论下。


#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;

struct Node
{
    int mod;
    string ans;
}f,t;

int n;
string ans,tmp;
int num[2];
int vis[70000];

int bfs1()
{
    int flag=0;
    for(int i=1;i<=9;i++)
    {
        f.mod=i%n;
        f.ans="";
        f.ans+=i+'0';

        while(f.ans.size()<n && f.mod!=0)
        {
            f.ans+=i+'0';
            f.mod=(f.mod*10+i)%n;
        }
        if(f.mod==0)
        {
            if(flag==0) {tmp=f.ans;flag=1;}
            else if(f.ans.size()<tmp.size()) tmp=f.ans;
            else if(f.ans.size()==tmp.size() && f.ans<tmp) tmp=f.ans;
        }
    }
    if(flag) {ans=tmp;return 1;}
    else return 0;
}

void bfs2()
{
    memset(vis,0,sizeof vis);
    queue<Node>q;

    for(int i=0;i<2;i++)
    {
        if(num[i]==0) continue;
        f.mod=num[i]%n;
        f.ans="";
        f.ans+=num[i]+'0';
        vis[f.mod]=1;
        q.push(f);
    }

    int flag=0;

    while(!q.empty())
    {
        f=q.front();
        q.pop();

        if(flag)
        {
            if(f.mod==0)
            {
                if(tmp.size()>f.ans.size()) {tmp=f.ans;continue;}
                else if(tmp.size()==f.ans.size() && tmp>f.ans) {tmp=f.ans;continue;}
            }

        }
        else if(f.mod==0)
        {
            flag=1;
            tmp=f.ans;
            continue;
        }

        for(int i=0;i<2;i++)
        {
            t=f;
            t.ans+=num[i]+'0';
            t.mod=(f.mod*10+num[i])%n;
            if(vis[t.mod]) continue;
            vis[t.mod]=1;
            q.push(t);
        }

    }

    if(ans=="") ans=tmp;
    else if(ans.size()>tmp.size()) ans=tmp;
    else if(ans.size()==tmp.size() && ans>tmp) ans=tmp;

}

int main()
{
    while(scanf("%d",&n),n)
    {
        ans="";

        if(bfs1());
        else
        {
            for(int i=0;i<9;i++)
                for(int j=i+1;j<=9;j++)
                {
                    num[0]=i;num[1]=j;
                    bfs2();
                }
        }
        cout<<ans<<endl;
    }
    return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值