Super Number UVA - 10624

问题

https://vjudge.net/problem/UVA-10624

分析

注意整数类型溢出,要使用LL,同时30位数取余数要分成两部分

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn=30+5;
int n,m,kase=0,T;
char ch[maxn];

inline int cal(int d){
    LL s=0,t;
    for(int i=1;i<=d;++i){
        s=10*s+ch[i]-'0';
        if(i==17) s=s%d;
    }
    t=s%d;
    if(t==0) return 0;
    return d-t;
}

inline bool dfs(int d){
    if(d==m+1) return true;
    int i=(d==1)?1:0;
    if(d<n) {
        for(;i<10;++i) {
            ch[d]='0'+i;
            if (dfs(d + 1)) return true;
        }
    }else{
//        for(;i<10;++i){
//            ch[d]='0'+i;
//            int t=cal(d);
//            if(t==0 && dfs(d+1)){
//                return true;
//            }
//        }
        for(;i<10;){
            ch[d]='0'+i;
            int t=cal(d);
            if(t==0){
                if(dfs(d+1)) return true;
            }else if(i+t<10){
                ch[d]='0'+i+t;
                if(dfs(d+1))  return true;
            }else break;
            i+=t+1;
        }
    }
    return false;
}

int main(void){
    scanf("%d",&T);
    ch[0]='0';
    while(kase<T){
        scanf("%d%d",&n,&m);
        bool flag=dfs(1);
        printf("Case %d: ",++kase);
        if(flag) {
            for(int i=1;i<=m;++i){
                putchar(ch[i]);
            }
            putchar('\n');
        }else printf("-1\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值