[CF1202B] You Are Given a Decimal String(最短路)
Description
今天突然想来发一篇博客防死
[Portal][https://vjudge.net/problem/2650668/origin]
定义被x-y生成器生成的序列为, 一开始有一个数字S = 0, 每次输出S % 10, 然后把这个数字加上x或y.
现在给你一个串, 对于$0\leq x, y \leq 9 $要你计算至少要在串中插入几个数位才能将其变成正确的x - y生成器生成的串
字符串长度\(\leq 2e6\)
Solution
这种数字之间跳来跳去的直接考虑最短路.
从\(i\)向\((i + x) \mod 10\), 连一条边权为1的单向边.
从\(i\)向\((i + y) \mod 10\), 连一条边权为1的单向边.
\(dis[i][i] = inf\)
直接跑floyd即可, 那么答案就是数字之间的距离-1的和.
Code
#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = (a), i##_end_ = (b); i <= i##_end_; ++i)
#define drep(i, a, b) for(int i = (a), i##_end_ = (b); i >= i##_end_; --i)
#define clar(a, b) memset((a), (b), sizeof(a))
#define debug(...) fprintf(stderr, __VA_ARGS__)
typedef long long LL;
typedef long double LD;
const double pi = acos(-1);
const int BUF_SIZE = (int)1e6 + 10;
template <typename T> inline bool chkmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;}
template <typename T> inline bool chkmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;}
LL read() {
char ch = getchar();
LL x = 0, flag = 1;
for (;!isdigit(ch); ch = getchar()) if (ch == '-') flag *= -1;
for (;isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
return x * flag;
}
const int Maxn = 2e6 + 9;
char s[Maxn];
void Init() {
scanf("%s", s + 1);
}
int dis[10][10];
int calc(int x, int y) {
clar(dis, 0x3f);
rep (i, 0, 9) dis[i][(i + x) % 10] = 1, dis[i][(i + y) % 10] = 1;
rep (k, 0, 9)
rep (i, 0, 9)
rep (j, 0, 9) dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
int ans = 0;
rep (i, 1, strlen(s + 1) - 1) {
if (dis[s[i] - '0'][s[i + 1] - '0'] == 0x3f3f3f3f) return -1;
ans += max(dis[s[i] - '0'][s[i + 1] - '0'] - 1, 0);
}
return ans;
}
void Solve() {
rep (i, 0, 9) {
rep (j, 0, 9) printf("%d ", calc(i, j));
puts("");
}
}
int main() {
// freopen("bosky.in", "r", stdin);
// freopen("bosky.out", "w", stdout);
Init();
Solve();
#ifdef Qrsikno
debug("\nRunning time: %.3lf(s)\n", clock() * 1.0 / CLOCKS_PER_SEC);
#endif
return 0;
}
[CF1202B] You Are Given a Decimal String(最短路)的更多相关文章
- You Are Given a Decimal String...
B. You Are Given a Decimal String... 这个题需要求出从某一个尾数 n 变为 m 所需要的 x 和 y 的最小个数(i+j) 那么就需要预处理出一个数组来存放这个值. ...
- [最短路,floyd] Codeforces 1202B You Are Given a Decimal String...
题目:http://codeforces.com/contest/1202/problem/B B. You Are Given a Decimal String... time limit per ...
- You Are Given a Decimal String... CodeForces - 1202B [简单dp][补题]
补一下codeforces前天教育场的题.当时只A了一道题. 大致题意: 定义一个x - y - counter :是一个加法计数器.初始值为0,之后可以任意选择+x或者+y而我们由每次累加结果的最后 ...
- (模拟)关于进制的瞎搞---You Are Given a Decimal String...(Educational Codeforces Round 70 (Rated for Div. 2))
题目链接:https://codeforc.es/contest/1202/problem/B 题意: 给你一串数,问你插入最少多少数可以使x-y型机器(每次+x或+y的机器,机器每次只取最低位--% ...
- JS转换Decimal带千分号的字符串显示
var numberChars = "0123456789"; /* Convert to decimal string */ function toDecimalString(v ...
- String类的一些常用操作方法
package com.liveyc.framework.util; import java.io.UnsupportedEncodingException; import java.net.URLD ...
- ESQL 查询数据报 参数类型“Edm.Decimal”和“Edm.Double”不兼容
ESQL 查询数据报 参数类型“Edm.Decimal”和“Edm.Double”不兼容 System.Data.Entity.Core.Objects.ObjectQuery<TEntity& ...
- 背后的故事之 - 快乐的Lambda表达式(一)
快乐的Lambda表达式(二) 自从Lambda随.NET Framework3.5出现在.NET开发者眼前以来,它已经给我们带来了太多的欣喜.它优雅,对开发者更友好,能提高开发效率,天啊!它还有可能 ...
- Dev统计选中行、如需其他数据源可留言
Clipboard.Clear(); Dictionary<string, decimal> dtary = new Dictionary<string, decimal>() ...
随机推荐
- Codeforces Round #460 (Div. 2) B Perfect Number(二分+数位dp)
题目传送门 B. Perfect Number time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- BZOJ 1683.City skyline 城市地平线
传送门 从左到右扫一遍,考虑什么时候会和之前形成同一幢房子从而不用统计 显然是当前的高度和之前某个点高度相同,并且它们之间没有更矮的建筑 考虑用一个单调栈维护一个单调上升的房子轮廓,然后对于扫到的每一 ...
- vue.js(8)--v-for的使用
v-for遍历数组.对象数组.对象.迭代次数 <!DOCTYPE html> <html lang="en"> <head> <meta ...
- JavaScript深入之类数组对象与arguments(转载)
类数组对象 所谓的类数组对象: 拥有一个 length 属性和若干索引属性的对象 举个例子: var array = ['name', 'age', 'sex']; var arrayLike = { ...
- html标签的target属性应用
1. 定义和用法 target 属性规定在何处打开页面上的所有链接. <head> <base target="_blank" /> </head&g ...
- EXCUTE JAVAScript点击事件
# Execute Javascript document.getElementsByClassName('chooseFile')[${index}].arguments[0].click(); # ...
- Linux的运行级别和设置开机启动服务的方式
Linux的运行级别 什么是运行级别呢?简单点来说,运行级别就是操作系统当前正在运行的功能级别.级别是从0到6,具有不同的功能.这些级别定义在/ect/inittab文件中.这个文件是init程序寻找 ...
- vue 限制input[type=number]的输入位数策略整理
https://blog.csdn.net/weistin/article/details/79664261 vue type="number 设置maxlength 是无效的 我们可以 ...
- Nginx1.6.0+MySQL5.6.19+PHP5.5.14(centos)
一.配置防火墙,开启80端口.3306端口 CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop fi ...
- golang-练习1
题目: 输入字符串,返回最大的单词. 实例:run#¥@!time 返回:time package main import ( "fmt" "strings" ...