Codeforces Find a Number 简单Bfs

A. Find a Number

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given two positive integers dd and ss. Find minimal positive integer nn which is divisible by dd and has sum of digits equal to ss.

Input

The first line contains two positive integers dd and ss (1≤d≤500,1≤s≤50001≤d≤500,1≤s≤5000) separated by space.

Output

Print the required number or -1 if it doesn't exist.

Examples

input

Copy

13 50

output

Copy

699998

input

Copy

61 2

output

Copy

1000000000000000000000000000001

input

Copy

15 50

output

Copy

-1

题目的意思就不多说了,就是求一个数n对d取余等于0,其各个位上的数字和等于s

简单的Bfs就过了。。。

题目链接:http://codeforces.com/problemset/problem/1070/A

 

#include <bits/stdc++.h>
using namespace std ;
const int Maxn = 5e3 + 10 ;

struct Node{
    int len ;
    int mod ;
    int sum ;
    char s[600] ;
    Node(){
        len = -1 ;
        mod = sum = 0 ;
        memset(s, 0, sizeof(s)) ;
    }
};

int Vis[510][Maxn] ;

int main (){
    int d, s ;
    cin >> d >> s ;
    queue < Node > que ;
    Node New ;
    que.push(New) ;
    while (!que.empty()){
        New = que.front() ;
        que.pop() ;
        for (int i = 0; i <= 9; i++){
            Node p = New ;
            p.len++ ;
            p.s[p.len] = i + '0' ;
            p.mod = (p.mod * 10 + i) % d ;
            p.sum += i ;
            if (Vis[p.mod][p.sum] || p.sum > s) continue ;
            Vis[p.mod][p.sum] = 1 ;
            if (p.mod == 0 && p.sum == s){
                puts(p.s) ;
                return 0 ;
            }
            que.push(p) ;
        }
    }
    puts("-1") ;
    return 0 ;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值