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(最短路)的更多相关文章

  1. You Are Given a Decimal String...

    B. You Are Given a Decimal String... 这个题需要求出从某一个尾数 n 变为 m 所需要的 x 和 y 的最小个数(i+j) 那么就需要预处理出一个数组来存放这个值. ...

  2. [最短路,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 ...

  3. You Are Given a Decimal String... CodeForces - 1202B [简单dp][补题]

    补一下codeforces前天教育场的题.当时只A了一道题. 大致题意: 定义一个x - y - counter :是一个加法计数器.初始值为0,之后可以任意选择+x或者+y而我们由每次累加结果的最后 ...

  4. (模拟)关于进制的瞎搞---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的机器,机器每次只取最低位--% ...

  5. JS转换Decimal带千分号的字符串显示

    var numberChars = "0123456789"; /* Convert to decimal string */ function toDecimalString(v ...

  6. String类的一些常用操作方法

    package com.liveyc.framework.util; import java.io.UnsupportedEncodingException; import java.net.URLD ...

  7. ESQL 查询数据报 参数类型“Edm.Decimal”和“Edm.Double”不兼容

    ESQL 查询数据报 参数类型“Edm.Decimal”和“Edm.Double”不兼容 System.Data.Entity.Core.Objects.ObjectQuery<TEntity& ...

  8. 背后的故事之 - 快乐的Lambda表达式(一)

    快乐的Lambda表达式(二) 自从Lambda随.NET Framework3.5出现在.NET开发者眼前以来,它已经给我们带来了太多的欣喜.它优雅,对开发者更友好,能提高开发效率,天啊!它还有可能 ...

  9. Dev统计选中行、如需其他数据源可留言

    Clipboard.Clear(); Dictionary<string, decimal> dtary = new Dictionary<string, decimal>() ...

随机推荐

  1. A AFei Loves Magic

    链接:https://ac.nowcoder.com/acm/contest/338/A来源:牛客网 题目描述 AFei is a trainee magician who likes to stud ...

  2. 联想笔记本 thinkpad BIOS 超级密码 Supervisor Password 清除 破解 亲测有效 转载地址https://blog.csdn.net/ot512csdn/article/details/72571674

    联想笔记本 thinkpad BIOS 超级密码 Supervisor Password 清除 破解 亲测有效 转载地址https://blog.csdn.net/ot512csdn/article/ ...

  3. Go 数组(1)

    1.一旦声明,数组里存储的数据类型和数组长度就都不能改变了.如果需要存储更多的元素, 就需要先创建一个更长的数组,再把原来数组里的值复制到新数组里. 例如: ]int 2.使用数组字面量声明数组 // ...

  4. canvas 点击图片播放视频

    canvas.js window.onload=function() { var canvas = document.getElementById('canvas'); var ctx= canvas ...

  5. grep正则表达式(二)

    任意字符(The Any Character) dot or period character: "." grep -h '.zip' dirlist*.txt ".&q ...

  6. 后端技术杂谈3:Lucene基础原理与实践

    本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下 ...

  7. Yii2.0 for update 行级锁

    当我们遇到存在高并发并且对于数据的准确性有要求的场景,需要了解和使用for update 需要注意的点: 1.InnoDB默认是行级别的锁,当有明确指定的主键时候,是行级锁.否则是表级别 2.for ...

  8. 高并发大流量专题---5、CDN加速

    高并发大流量专题---5.CDN加速 一.总结 一句话总结: CDN就是多整几台节点服务器,选距离用户最近的服务器来给用户服务,实现的话可以用阿里云.腾讯云他们提供的功能,简单方便,妈妈再也不用担心我 ...

  9. vector代替数组

    vector代替数组 1.声明一个int向量以替代一维的数组:vector <int> a;(等于声明了一个int数组a[],大小没有指定,可以动态的向里面添加删除). 2.用vector ...

  10. soj#532 set p3175

    传送门 分析 代码 #include<bits/stdc++.h> using namespace std; ; <<],Ans; int n,m,N; inline int ...