UVA10624 Super Number【DFS】

Don’t you think 162456723 very special? Look at the picture below if you are unable to find its speciality. (a|b means ‘b is divisible by a’)
在这里插入图片描述
Figure: Super Numbers
    Given n, m (0 < n < m < 30), you are to find a m-digit positive integer X such that for every i (n ≤ i ≤ m), the first i digits of X is a multiple of i. If more than one such X exists, you should output the lexicographically smallest one. Note that the first digit of X should not be 0.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 15), the number of test cases followed. For each case, two integers n and m are separated by a single space.
Output
For each test case, print the case number and X. If no such number, print ‘-1’.
Sample Input
2
1 10
3 29
Sample Output
Case 1: 1020005640
Case 2: -1

问题链接UVA10624 Super Number
问题简述:(略)
问题分析
    给定两个数n和m,如果长度为m的数满足对于每个i(n<=i<=m),数字的前i位都能被i整除,那么这个数就是超级数,求字典序中最小的符合要求的超级数。
    用DFS暴力搜索一下。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10624 Super Number */

#include <bits/stdc++.h>

using namespace std;

const int N = 30;
int t, n, m, a[N + 1];
bool flag;

int judge(int pos)
{
    int sum = 0;
    for(int i = 0; i < pos; i++)
        sum = (sum * 10 + a[i]) % pos;
    return sum;
}

void dfs(int pos)
{
    if(pos == m)
        flag = true;
    else {
        for(int i = 0; i <= 9; i++)
            if(pos != 0 || i != 0) {
                a[pos] = i;
                if(!judge(pos + 1) || pos + 1 < n) {
                    dfs(pos + 1);
                    if(flag) return;
                }
            }
    }
}

int main()
{
    scanf("%d", &t);
    for(int k = 1; k <= t; k++) {
        memset(a, -1, sizeof(a));

        scanf("%d%d", &n, &m);

        flag = false;
        dfs(0);

        printf("Case %d: ", k);
        if(flag) {
            for(int i = 0; i < m; i++)
                printf("%d", a[i]);
            printf("\n");
        } else
            printf("-1\n");
    }

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值