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. 【优化算法】变邻域搜索算法解决0-1背包问题(Knapsack Problem)代码实例 已

    01 前言 经过小编这几天冒着挂科的风险,日日修炼,终于赶在考试周中又给大家更新了一篇干货文章.关于用变邻域搜索解决0-1背包问题的代码.怎样,大家有没有很感动? 02 什么是0-1背包问题? 0-1 ...

  2. 【原创】go语言学习(十二)struct介绍1

    目录: struct声明和定义 struct的内存布局以及构造函数 匿名字段和struct嵌套 struct与tag应用 struct声明和定义 1.Go中面向对象是通过struct来实现的, str ...

  3. linux 查看内存条详情命令

    dmidecode |more

  4. Python数据结构学习

    列表 Python中列表是可变的,这是它区别于字符串和元组的最重要的特点,一句话概括即:列表可以修改,而字符串和元组不能. 以下是 Python 中列表的方法: 方法 描述 list.append(x ...

  5. PL/SQL配置和连接远端数据库

    1. 安装与配置 (1) 安装 因为是免安装的绿色版,所以解压到目录就可以了,保证目录中没有空格.下划线和中文字符. 还有一点,PL/SQL需要和Oracle的版本一致,都是32位或者都是64位,否则 ...

  6. Hibernate---进度1

    关联映射:http://www.cnblogs.com/huxi/archive/2009/12/15/1624988.html 关联映射,hibernate查询方式:http://www.cnblo ...

  7. Git git rm和git rm --cached

    git rm 和 git rm --cached 的区别 git rm file git commit -m "xxx" git push origin master 删除本地及仓 ...

  8. java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the server

    错误信息 java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents ...

  9. OpenJudge计算概论-骑车与走路

    /*============================================================ 骑车与走路 总时间限制: 1000ms 内存限制: 65536kB 描述 ...

  10. 等待 Redis 应答

    https://zhuanlan.zhihu.com/p/58608323 mq消息合并:由于mq请求发出到响应的时间,即往返时间, RTT(Round Time Trip),每次提交都要消耗RTT, ...