[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>() ...
随机推荐
- C#设计模式:抽象工厂(Abstract Factory)
一,抽象工厂模式 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- Dubbo源码学习总结系列一 总体认识
本文写作时,dubbo最高版本是V2.6.0. 写这篇文章主要想回答以下4个问题: 一.dubbo是什么?完成了哪些主要需求? 二.dubbo适用于什么场景? 三.dubbo的总体架构是什么样的? ...
- nginx下TP3.2访问页面总是404
这是我测试onethink用的配置 可以参考一下 server { listen 80; server_name onethink.dev www.onethink.dev; root /vagran ...
- 【LeetCode】线段树 segment-tree(共9题)+ 树状数组 binary-indexed-tree(共5题)
第一部分---线段树:https://leetcode.com/tag/segment-tree/ [218]The Skyline Problem [307]Range Sum Query - Mu ...
- Sass--混合宏的不足
混合宏在实际编码中给我们带来很多方便之处,特别是对于复用重复代码块.但其最大的不足之处是会生成冗余的代码块.比如在不同的地方调用一个相同的混合宏时.如: @mixin border-radius{ - ...
- Echarts 折线类型
1:Echarts2.0 这种皮肤下柱状图转折线图默认是平滑曲线 默认皮肤为硬折线: 如果需要在2.0的macarons皮肤下使用硬折线需显式设置以下属性: series: [ { ...
- centos6.5 相关命令
挂载U盘 1.进入mnt目录: #cd /mnt 2.新建一个USB目录: #mkdir usb 3.查看U盘的目录: #fdisk –l 4.挂载: #mount –t vfat /dev/sdb1 ...
- "sorted()"中的"Key Functions"
Pythonsorted()函数中可以加入key=<FUNCTION>参数.作用是每个元素在排序之前,先作为key=<FUNCTION>中FUNCTION的参数,用FUNCTI ...
- javaScript的关键字与保留字
JavaScript 关键字: break case catch continue default delete do else finally for function if in instance ...
- C#版简易RSS阅读器
C#版简易RSS阅读器.由VB版修改完成,感谢aowind的技术支持! 源代码: using System; using System.Drawing; using System.Collection ...