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. 数据结构实验之图论七:驴友计划【迪杰斯特拉算法】(SDUT 3363)

    分析:可以求简单的任意两点间最短距离的稍微变形,一个板子题.  #include <iostream> #include <bits/stdc++.h> using names ...

  2. Python中匹配IP的正则表达式

    下面是IPv4的IP正则匹配表达式 import re #简单的匹配给定的字符串是否是ip地址,下面的例子它不是IPv4的地址,但是它满足正则表达式 if re.match(r"^(?:[0 ...

  3. js 将网络图片格式转为base64 canvas 跨域

    function getBase64Image(img) { var canvas = document.createElement("canvas"); canvas.width ...

  4. 2019CSP-S初赛知识点汇总

    0x00 基本算法 0x01 位运算 0x02 前缀和与差分 0x03 二分 0x04 倍增 0x05 排序 0x06 离散化 0x07 高精度 0x10 数据结构 0x11 栈和队列 0x12 链表 ...

  5. JAVA基础知识|java虚拟机(JVM)

    一.JVM简介 java语言是跨平台的,兼容各种操作系统.实现跨平台的基石就是虚拟机(JVM),虚拟机不是跨平台的,所以不同的操作系统需要安装不同的jdk版本(jre=jvm+类库:jdk=jre+开 ...

  6. LeetCode31 Next Permutation and LeetCode60 Permutation Sequence

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  7. 认识wsgi

    WSGI是什么? WSGI,全称 Web Server Gateway Interface,或者 Python Web Server Gateway Interface ,是为 Python 语言定义 ...

  8. 2.linux的增删改查

    一.增删改查       1.建立文件和目录         mkdir /tmp/xueying       2.cd 进入的路径         绝对路径:以根目录为其实目录的路径         ...

  9. WebSocketSharp中WebSocket类

    websocket-sharp.clone, Version=1.0.2.39869 WebSocket由方法调用事件改为实例化委托调用,两种构造 1.构造函数 第一种 // // 摘要: // In ...

  10. export命令import命令

    export命令import命令 export { name1, name2, …, nameN }; export { variable1 as name1, variable2 as name2, ...