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. bzoj1779 [Usaco2010 Hol]Cowwar 奶牛战争(网络流)

    1779: [Usaco2010 Hol]Cowwar 奶牛战争 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 302  Solved: 131[Sub ...

  2. sessionStorage 使用方法

    作为html5中Web Storage的一种存储方式,localStorage和sessionStorage一样都是用来存储客户端临时信息的对象. W3c上给的介绍是这两者区别在于前者用于持久化的本地 ...

  3. JS变量连续赋值

    下面就是这个经典案例: var a = {n: 1}; var b = a; a.x = a = {n: 2}; console.log(a);console.log(b); console.log( ...

  4. Introduction to Sound Programming with ALSA

    ALSA stands for the Advanced Linux Sound Architecture. It consists of a set of kernel drivers, an ap ...

  5. [USACO06FEB]摊位预订Stall Reservations(贪心)

    [USACO06FEB]摊位预订Stall Reservations 题目描述 Oh those picky N (1 <= N <= 50,000) cows! They are so ...

  6. mongodb使用简介

    mongodb简介 在使用nodejs时候,需要存储一些简单json数据的情况下,很多人会推荐使用mongodb.mongodb是一个文档型数据库,在 sql 中,数据层级是:数据库(db) -> ...

  7. 【LeetCode】图论 graph(共20题)

    [133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 /* // Definition for a Node. class ...

  8. html5 jquery音乐播放器,play()和pause()不起作用

    今天在自己写的页面上加上背景音乐,当我点击图片时可以切换 播放/暂停 用jquery写的,方法总是提示没有pause这个方法! 检查了半天最后发现 你使用的是jquery选择器所以返回的是jquery ...

  9. Nginx配置参数详解参考示例

    user nobody; worker_processes 2; events{ worker_connections 1024; } http{ #设置默认类型为二进制流 default_type ...

  10. Oracle12c RAC数据导出至Oracle11g

    一.Oracle12c导出数据 1.连接数据库 sqlplus / as sysdba 2.查看pdbs show pdbs; 3.切换pdb alter session set container= ...