题目:http://codeforces.com/contest/1202/problem/B

B. You Are Given a Decimal String...
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Suppose you have a special xx-yy-counter. This counter can store some value as a decimal number; at first, the counter has value 00.

The counter performs the following algorithm: it prints its lowest digit and, after that, adds either xx or yy to its value. So all sequences this counter generates are starting from 00. For example, a 44-22-counter can act as follows:

  1. it prints 00, and adds 44 to its value, so the current value is 44, and the output is 00;
  2. it prints 44, and adds 44 to its value, so the current value is 88, and the output is 0404;
  3. it prints 88, and adds 44 to its value, so the current value is 1212, and the output is 048048;
  4. it prints 22, and adds 22 to its value, so the current value is 1414, and the output is 04820482;
  5. it prints 44, and adds 44 to its value, so the current value is 1818, and the output is 0482404824.

This is only one of the possible outputs; for example, the same counter could generate 02468024680240246802468024 as the output, if we chose to add 22during each step.

You wrote down a printed sequence from one of such xx-yy-counters. But the sequence was corrupted and several elements from the sequence could be erased.

Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string ss — the remaining data of the sequence.

For all 0≤x,y<100≤x,y<10, calculate the minimum number of digits you have to insert in the string ss to make it a possible output of the xx-yy-counter. Note that you can't change the order of digits in string ss or erase any of them; only insertions are allowed.

Input

The first line contains a single string ss (1≤|s|≤2⋅1061≤|s|≤2⋅106, si∈{0−9}si∈{0−9}) — the remaining data you have. It's guaranteed that s1=0s1=0.

Output

Print a 10×1010×10 matrix, where the jj-th integer (00-indexed) on the ii-th line (00-indexed too) is equal to the minimum number of digits you have to insert in the string ss to make it a possible output of the ii-jj-counter, or −1−1 if there is no way to do so.

Example
input

Copy
0840
output

Copy
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
Note

Let's take, for example, 44-33-counter. One of the possible outcomes the counter could print is 0(4)8(1)4(7)00(4)8(1)4(7)0 (lost elements are in the brackets).

One of the possible outcomes a 22-33-counter could print is 0(35)8(1)4(7)00(35)8(1)4(7)0.

The 66-88-counter could print exactly the string 08400840.

题意:

有一个x-y计数器,tmp初始为0,计数器每次会输出tmp的最低位(也就是tmp%10),并会对tmp加上x或y,如此重复形成一个串
现在给你一个串s,询问x-y计数器x从0到9,y从0到9,在s中插入最少多少个数字使的当前串能由当前x-y计数器输出,否则如果没办法插入一些数字使得当前x-y计数器输出当前串则输出-1

思路:

若s串中每一个数字都代表一个状态,则问题就转化为上一个状态能否可达当前状态,如果可达最小代价是多少?所以我们需要知道0到9数字之间的转移代价
现在考虑如何状态如何转移,对于x-y计数器,当前数字是i,则i只能转移到(i+x)%10或(i+y)%10
接着考虑状态的最小代价,一个状态转移到另一个状态,其中可经过其他点,使得总距离缩小,如果不能经过其他来缩小距离则当前的两点距离就是最短距离
所以我们可以用floyd来求出任意两点距离,先考虑从0中转直到从9中转,注意中转点k要写在最外层,每次才能在上一个中转点最短路求出的基础上求这一个中转点的最短路
求出任意两点最短路后我们就可以判断是否可达,如果不可达返回-1,否则就累加答案,注意这里是插入多少点,也就是距离-1,这个-1是因为最后一个点是已经存在了

注意:

注意floyd的中转点k要写在最外层,每次才能在上一个中转点最短路求出的基础上求这一个中转点的最短路

 #include<bits/stdc++.h>
using namespace std;
#define fi s[i-1]-'0'
#define se s[i]-'0'
const int amn=2e6+,inf=0x3f3f3f3f;
char s[amn];
int ans[][],dis[][];
int solve(int x,int y,int len){
memset(dis,inf,sizeof dis); ///x-y计数器必须要加x或加y,所以i==j时也赋值为inf
for(int i=;i<=;i++){
dis[i][(i+x)%]=; ///现在考虑如何状态如何转移,对于x-y计数器,当前数字是i,则i只能转移到(i+x)%10或(i+y)%10
dis[i][(i+y)%]=;
} ///接着考虑状态的最小代价,一个状态转移到另一个状态,其中可经过其他点,使得总距离缩小,如果不能经过其他来缩小距离则当前的两点距离就是最短距离
for(int k=;k<=;k++) ///floyd 的k要写在最外层,代表现在中转的节点k,下面求ij中转k的最短距离
for(int i=;i<=;i++)
for(int j=;j<=;j++)
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
int ans=;
for(int i=;i<len;i++){ ///求出任意两点最短路后我们就可以判断是否可达,如果不可达返回-1,否则就累加答案,注意这里是插入多少点,也就是距离-1,这个-1是因为最后一个点是已经存在了
if(dis[fi][se]==inf)return -;
ans+=dis[fi][se]-;
}
return ans;
}
int main(){
ios::sync_with_stdio();
cin>>s;
int len=strlen(s);
for(int i=;i<=;i++){
for(int j=;j<=;j++){
cout<<solve(i,j,len)<<(j<?' ':'\n');
}
}
}
/**
有一个x-y计数器,tmp初始为0,计数器每次会输出tmp的最低位(也就是tmp%10),并会对tmp加上x或y,如此重复形成一个串
现在给你一个串s,询问x-y计数器x从0到9,y从0到9,在s中插入最少多少个数字使的当前串能由当前x-y计数器输出,否则如果没办法插入一些数字使得当前x-y计数器输出当前串则输出-1
若s串中每一个数字都代表一个状态,则问题就转化为上一个状态能否可达当前状态,如果可达最小代价是多少?所以我们需要知道0到9数字之间的转移代价
现在考虑如何状态如何转移,对于x-y计数器,当前数字是i,则i只能转移到(i+x)%10或(i+y)%10
接着考虑状态的最小代价,一个状态转移到另一个状态,其中可经过其他点,使得总距离缩小,如果不能经过其他来缩小距离则当前的两点距离就是最短距离
所以我们可以用floyd来求出任意两点距离,先考虑从0中转直到从9中转,注意中转点k要写在最外层,每次才能在上一个中转点最短路求出的基础上求这一个中转点的最短路
求出任意两点最短路后我们就可以判断是否可达,如果不可达返回-1,否则就累加答案,注意这里是插入多少点,也就是距离-1,这个-1是因为最后一个点是已经存在了
**/

[最短路,floyd] Codeforces 1202B You Are Given a Decimal String...的更多相关文章

  1. [最短路,floyd] Codeforces 1204C Anna, Svyatoslav and Maps

    题目:http://codeforces.com/contest/1204/problem/C C. Anna, Svyatoslav and Maps time limit per test 2 s ...

  2. ACM/ICPC 之 最短路-Floyd+SPFA(BFS)+DP(ZOJ1232)

    这是一道非常好的题目,融合了很多知识点. ZOJ1232-Adventrue of Super Mario 这一题折磨我挺长时间的,不过最后做出来非常开心啊,哇咔咔咔 题意就不累述了,注释有写,难点在 ...

  3. 模板C++ 03图论算法 2最短路之全源最短路(Floyd)

    3.2最短路之全源最短路(Floyd) 这个算法用于求所有点对的最短距离.比调用n次SPFA的优点在于代码简单,时间复杂度为O(n^3).[无法计算含有负环的图] 依次扫描每一点(k),并以该点作为中 ...

  4. 最短路 - floyd算法

    floyd算法是多源最短路算法 也就是说,floyd可以一次跑出所以点两两之间的最短路 floyd类似动态规划 如下图: 用橙色表示边权,蓝色表示最短路 求最短路的流程是这样的: 先把点1到其他点的最 ...

  5. HDU1869---(最短路+floyd)

    http://acm.hdu.edu.cn/showproblem.php?pid=1869 思路:最短路+floyd 分析:1 题目是要求所有的数据能否满足“六度分离”,那么我们就想到所有点之间的最 ...

  6. 【bzoj2324】[ZJOI2011]营救皮卡丘 最短路-Floyd+有上下界费用流

    原文地址:http://www.cnblogs.com/GXZlegend/p/6832504.html 题目描述 皮卡丘被火箭队用邪恶的计谋抢走了!这三个坏家伙还给小智留下了赤果果的挑衅!为了皮卡丘 ...

  7. 【ACM程序设计】求短路 Floyd算法

    最短路 floyd算法 floyd是一个基于贪心思维和动态规划思维的计算所有点到所有点的最短距离的算法. P57-图-8.Floyd算法_哔哩哔哩_bilibili 对于每个顶点v,和任一顶点对(i, ...

  8. 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

    题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...

  9. poj 3613 经过k条边最短路 floyd+矩阵快速幂

    http://poj.org/problem?id=3613 s->t上经过k条边的最短路 先把1000范围的点离散化到200中,然后使用最短路可以使用floyd,由于求的是经过k条路的最短路, ...

随机推荐

  1. 手机视频APP将关闭 生态梦成空的三星如何自救?

    生态梦成空的三星如何自救?"> 三星如今的处境,只能用"屋漏偏逢连夜雨"来形容.继营收.利润.智能手机销量等大幅下滑之后,裁员也接踵而来,股价的下跌也自然在情理之中 ...

  2. Android APP性能及专项测试(个人整理)

    移动测试. Android测试 .APP测试   Android篇 1. 性能测试 Android性能测试分为两类:1.一类为rom版本(系统)的性能测试2.一类为应用app的性能测试 Android ...

  3. 14、创建/恢复ETH钱包身份

    借助网上的一段描述: 若以银行账户为类比,这 5 个词分别对应内容如下: 地址=银行卡号密码=银行卡密码私钥=银行卡号+银行卡密码助记词=银行卡号+银行卡密码Keystore+密码=银行卡号+银行卡密 ...

  4. e代驾狂野裁员 O2O逐渐恢复理智?

    O2O逐渐恢复理智?" title="e代驾狂野裁员 O2O逐渐恢复理智?">     近段时间以来,O2O行业堪称"哀鸿遍野",十分凄惨.巨头 ...

  5. WiFi产生电磁辐射或让人想去自杀

    随着互联网在生活中的地位越来越重要,WiFi作为一种无线连接方式给了用户极大的便捷,然而有一部分科学家提出WiFi产生的电磁反应会对人的健康受到影响.面对这种说法,我们一直以为是专家在危言耸听,但是如 ...

  6. 一道二叉树题的n步优化——LeetCode98validate binary search tree(草稿)

    树的题目,往往可以用到三种遍历.以及递归,因为其结构上天然地可以往深处递归,且判断条件也往往不复杂(左右子树都是空的). LeetCode 98题讲的是,判断一棵树是不是二叉搜索树. 题目中给的是标准 ...

  7. C# 视频监控系统(提供源码分享)

    去过工厂或者仓库的都知道,在工厂或仓库里面,会有很多不同的流水线,大部分的工厂或仓库,都会在不同流水线的不同工位旁边安装一台电脑,一方面便于工位上的师傅把产品的重要信息录入系统,便于公司系统数据采集分 ...

  8. HTML5 基础知识(1)——基本标签

    ## HTML**概念**:是最基础的网页开发语言(Hyper Text Markup Langage 超文本标记语言) > 1.超文本:超文本是用超链接的方式i,将各种不同空间的文字组织在一起 ...

  9. JDk下载和环境变量Path的配置

    JDK下载与安装 下载地址 打开该网址会显示如下图,点击DOWMLOAD即可: 出现该页面时,点击接受: 选择对应的安装包下载即可(本人用的是Windows64位): 注:如果您无法确定您的windo ...

  10. MySQL记录操作(增删改)

    概览 MySQL数据操作: DML 在MySQL管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括 使用INSERT实现数据的插入 UPDATE实现数据的更新 使用DELETE实现数据 ...