Find a Number

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

数据范围:略。


题解

因为$d$和$s$比较小可以搜。

这就很$gay$了...我根本没想到可以宽搜。

想到搜索就非常简单了,从小到大枚举就好了。

代码

#include <bits/stdc++.h>

#define inf 0x3f3f3f3f 

#define D 510 

#define S 5010 

using namespace std;

int d, s;

bool vis[D][S];

queue <pair<int, int> >q;

pair<pair<int, int>, int> p[D][S];

void bfs() {
vis[0][0] = true;
q.push(make_pair(0, 0));
while (!q.empty()) {
int x = q.front().first, y = q.front().second;
q.pop();
for (int i = 0; i < 10; i ++ ) {
int x_ = (x * 10 + i) % d, y_ = y + i;
if (y_ <= s && !vis[x_][y_]) {
vis[x_][y_] = true;
p[x_][y_] = make_pair(make_pair(x, y), i);
q.push(make_pair(x_, y_));
}
}
}
} void output(int x, int y) {
if (!x && !y) {
return;
}
output(p[x][y].first.first, p[x][y].first.second);
putchar('0' + p[x][y].second);
} int main() {
cin >> d >> s ;
bfs();
if (!vis[0][s]) {
puts("-1");
}
else {
output(0, s);
}
return 0;
}

[CF1070A]Find a Number_bfs的更多相关文章

  1. dp转图论——cf1070A好题

    dp的状态转移很像一张有向图:每个状态为一个点,每中转移方案是一条有向边 本题要求是求出最小的数,那我们用状态[i,j]表示模i,数位和为j,那么从每个点出发的十条有向边代表[0,9]十个数 从每个状 ...

  2. yd的拔钉子之路之 POI 2017

    写在前面的一些话 如果我NOIP没退役,这大概会写成一个系列吧,所以这算是系列的开始,要写一些奇怪的东西? 首先解释下什么叫“拔钉子”,其实就是在钉子上做题嘛......至于钉子具体是个什么东西就当面 ...

随机推荐

  1. [TJOI2013]松鼠聚会 曼哈顿距离

    [TJOI2013]松鼠聚会 luogu P3964 首先容易得到两点间距离是\(max(|x_1-x_2|, |y_1-y_2|)\)(即切比雪夫距离) 然后有个套路:原\((x,y)\)求曼哈顿距 ...

  2. linux下安装python3.6.6

    1.到python的官网去下载python3.6.3安装包,必须是Linux版本的 2.在/usr/tmp下下载python安装包 wget https://www.python.org/ftp/py ...

  3. 安装less/sass

    安装sass npm i node-sass 安装wepy-compiler-sass插件 npm install wepy-compiler-sass --save-dev 在我的项目中使用才有用.

  4. 10月清北学堂培训 Day 7

    今天是黄致焕老师的讲授~ 历年真题选讲 NOIP 2012 开车旅行 小 A 和小 B 决定外出旅行,他们将想去的城市从 1 到 n 编号,且编号较小的城市在编号较大的城市的西边.记城市 i 的海拔高 ...

  5. 不用中间变量交换a和b的值?

    a = b = a = a+b b = a-b a = a-b print(a,b) a = b = a = a^b b = b^a a = a^b print(a,b) a = b = a,b = ...

  6. C++中的平方、开方、绝对值怎么计算

    #include <math.h> //平方 pow() ,);// 4的平方=16 //开方 ,0.5);// 4的平方根=2 );// 4的平方根=2 //整数绝对值 int c = ...

  7. P3975 (后缀自动机sort)

    题目链接: https://www.luogu.org/problem/P3975 题意: 求出所有字串的第k大子串 有两种,第一种对于出现在不同位置的相同子串算作一个子串 第二种,对于不同位置的子串 ...

  8. onPageScroll的使用

    1. 2.

  9. HttpClient学习(二)—— MinimalHttpClient源码解析

    依赖关系 方法 class MinimalHttpClient extends CloseableHttpClient { //连接管理器 private final HttpClientConnec ...

  10. GitHub回退到某个commit版本

    首先查看commit日志 git log 复制你想回退到的commit版本的commit_id,也就是图中圈出来的一大串字符. 将本地回退 git reset --hard commit_id 将远程 ...