【POJ 2283】 【HDU 1664】 Different Digits

8 篇文章 0 订阅
7 篇文章 0 订阅

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

HINT

Source


题目意思:

给一个n,让你找出含不同数字最少的n的倍数,如果含不同数字相同,则输出最小的。

解题思路:

根据数论里面的知识点:

对于任意的整数 n ,必然存在一个由不多于两个的数来组成的一个倍数。 因为 a aa  aaa……  n+1 个,则由鸽笼原理,必有两个模 n 余数相同,相减即得 n 的倍数m 。而 m 只由 a  0 组成。

对于余数相同的情况,数位少的含不同数字的个数肯定不超过数位多的,所以可以用余数判重。

先扫描一位的情况,然后在考虑两位的情况。

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
#define N 65540
int step[N], res[N], pre[N];
char ans[N], re[N];
int num[2], c, n, l, l1;
bool bfs() {
    memset(step, 0, sizeof(step));
    queue<int> Q;
    int p, q;
    for (int i = 0; i < c; i++) {
        int y = num[i] % n;
        if (num[i] == 0 || step[y])
            continue;
        step[y] = 1;
        pre[y] = -1;
        res[y] = num[i];
        Q.push(y);
    }
    while (!Q.empty()) {
        p = Q.front();
        Q.pop();
        if (p == 0)
            return 1;
        if (l && step[p] > l)
            return 0;
        for (int i = 0; i < c; i++) {
            q = (p * 10 + num[i]) % n;
            if (!step[q]) {
                step[q] = step[q] + 1;
                pre[q] = p;
                res[q] = num[i];
                Q.push(q);
            }
        }
    }
    return 0;
}
void solve(int i) {
    if (pre[i] != -1) {
        solve(pre[i]);
    }
    re[l1++] = res[i] + '0';
}
bool check() {
    if (!l || l1 < l)
        return 1;
    if (l1 > l)
        return 0;
    for (int i = 0; i < l; i++) {
        if (ans[i] > re[i])
            return 1;
        if (ans[i] < re[i])
            return 0;
    }
    return 0;
}
int main() {
    while (scanf("%d", &n) != EOF && n) {
        c = 1;
        l = 0;
        int flag = 0;
        for (int i = 1; i < 10; i++) {
            num[0] = i;
            if (bfs()) {
                flag = 1;
                l1 = 0;
                solve(0);
                if (check()) {
                    l = l1;
                    memcpy(ans, re, sizeof(ans));
                }
            }
        }
        if (!flag) {
            c = 2;
            for (int i = 0; i < 10; i++) {
                for (int j = i + 1; j < 10; j++) {
                    num[0] = i;
                    num[1] = j;
                    if (bfs()) {
                        l1 = 0;
                        solve(0);
                        if (check()) {
                            l = l1;
                            memcpy(ans, re, sizeof(ans));
                        }
                    }
                }
            }
        }
        for (int i = 0; i < l; i++) {
            printf("%c", ans[i]);
        }
        printf("\n");
    }
    return 0;
}
 
/**************************************************************
    Problem: 1290
    User: xrq
    Language: C++
    Result: Accepted
    Time:420 ms
    Memory:3888 kb
****************************************************************/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值