日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/1 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforces 1070A Find a Number(BFS) 2018-2019 ICPC, NEERC, Southern Subregional Contest Problem A 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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;}

轉載于:https://www.cnblogs.com/bestsort/p/10588825.html

總結

以上是生活随笔為你收集整理的Codeforces 1070A Find a Number(BFS) 2018-2019 ICPC, NEERC, Southern Subregional Contest Problem A的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。