Codeforces 1070A Find a Number(BFS) 2018-2019 ICPC, NEERC, Southern Subregional Contest Problem A

该博客介绍了一道来自Codeforces的编程竞赛题目,要求找到最小正整数n,它能被d整除且数字之和为s。博主通过BFS(广度优先搜索)策略解决此问题,并分享了ACM/ICPC竞赛中的同余定理应用。文章提供了示例输入输出以及解决问题的代码思路。

Description
You are given two positive integers ddd and sss. Find minimal positive integer nnn which is divisible by ddd and has sum of digits equal to sss.
Input
The first line contains two positive integers ddd and sss(1≤d≤500,1≤s≤5000)(1≤d≤500,1≤s≤5000)(1d500,1s5000) separated by space.
Output

Print the required number or -1 if it doesn’t exist.
Sample Input
Input
13 50
Output
699998
Input
61 2
Output
1000000000000000000000000000001
Input
15 50
Output
-1


这道题是看了别人后的代码才写的,看到代码后没想到居然是个BFS(果然还是自己太菜啊)
就是让你求一个数,这个数能被sss整除,且每位数相加=d=d=d

看了别人ac的代码后发现其实很简单,运用同余定理就行了。。。。QAQ我怎么这么菜


思路
先根据位数进行BFS,如果位数和>s位数和>s>s就不入队,剩下的每次入队前都modmodmod ddd就好了(控制数字大小别超intintint。然后当余数等于000(正好被ddd整除了),位数和等于sss的时候就是答案


代码如下

#include <queue>
#include <map>
#include <unordered_map>
#include <queue>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
#define lowbit(a) (a&(-a))
#define _mid(a,b) ((a+b)/2)
#define _mem(a,b) memset(a,0,(b+3)<<2)
#define fori(a) for(int i=0;i<a;i++)
#define forj(a) for(int j=0;j<a;j++)
#define ifor(a) for(int i=1;i<=a;i++)
#define jfor(a) for(int j=1;j<=a;j++)
#define mem(a,b) memset(a,b,sizeof(a))
#define IN freopen("in.txt","r",stdin)
#define OUT freopen("out.txt","w",stdout)
#define IO do{\
    ios::sync_with_stdio(false);\
    cin.tie(0);\
    cout.tie(0);}while(0)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define debug(a) cout <<(a) << endl
using namespace std;


struct node{
    int mod;
    int bit;
    string s;
    node(){};
    node(int m,int b,string ss){mod=m,bit=b,s=ss;}
};

bool v[501][5001];
string bfs(int d,int s){
    queue<node>q;
    q.push(node(0,0,""));
    v[0][0] = true;
    while(!q.empty()){
        node buf = q.front();
        q.pop();
        if(buf.bit <= s){
            if(buf.mod == 0&&buf.bit==s)
                return buf.s;
            fori(10){
                int bmod = (buf.mod*10+i)%d;
                int bbit = buf.bit+i;
                if(!v[bmod][bbit]){
                    v[bmod][bbit] = true;
                    q.push(node(bmod,bbit,buf.s+(char)(i+'0')));
                }
            }
        }
    }
    return "-1";
}
int main() {
    int s,d;
    cin >> d>> s;
    cout << bfs(d,s) << endl;
    return 0;

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值