题目: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. Nginx+PHP配置错误,日志:[error] 24324#0: *31 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream

    一.问题现象 1.安装nginx.php.php-fpm后,浏览器访问php报错,“File not found”: 二.问题排查 1.检查nginx.php-fpm服务是否正常启动,均正常启动: 2 ...

  2. Ubuntu 16.04 PXE+kickstart部署系统

    #PXE+TFTP+Kickstart 自动部署服务器系统系统Ubuntu16.04apt-get install isc-dhcp-servervim /etc/default/isc-dhcp-s ...

  3. 虚拟机+server03系统+sql的安装

    教程: 首先安装虚拟机 然后安装server系统 最后完成sql的安装 https://download.pchome.net/system/sysenhance/detail-4673.html 虚 ...

  4. 网站提权之MSF骚操作

    当我们在进行web渗透测试的时候,拿到了webshell,但是在执行net user.whoami.类似的命令会发现怎么好像用不了,没有回显,权限不够,这可怎么办呐? 测试环境: 内网,没钱买服务器, ...

  5. k8s环境部署.net core web项目(docker本地仓库)

    在之前的文档中,我们部署了.net core web在k8s环境下,达成了集群管理项目的目的.但是,由于是本地部署,需要在所有的node节点都拉取好镜像,这是非常麻烦的,为了只维护一份代码,同步更新. ...

  6. python版md-to-html编辑器

    用PyQt5封装python-markdown,支持自定义样式. 详情:https://blog.phyer.cn/article/4523

  7. 从零开始打造 Mock 平台 - 核心篇

    前言 最近一直在捣鼓毕设,准备做的是一个基于前后端开发的Mock平台,前期花了很多时间完成了功能模块的交互.现在进度推到如何设计核心功能,也就是Mock数据的解析. 根据之前的需求设定加上一些思考,用 ...

  8. 把.net Core 项目迁移到VS2019 for MAC

    VS2019 for MAC已经发布很长时间了,本以为项目移过去很麻烦,一直没有动作,最近呆家里快发霉了,决定研究研究,没想到一句代码都不需要动,直接完功,这下可以生产了.同学们可以放心整了. 本次平 ...

  9. Python——五分钟带你弄懂迭代器与生成器,夯实代码能力

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是周一Python专题,给大家带来的是Python当中生成器和迭代器的使用. 我当初第一次学到迭代器和生成器的时候,并没有太在意,只是觉 ...

  10. 复盘MySQL分页查询优化方案

    一.前言 MySQL分页查询作为Java面试的一道高频面试题,这里有必要实践一下,毕竟实践出真知. 很多同学在做测试时苦于没有海量数据,官方其实是有一套测试库的. 二.模拟数据 这里模拟数据分2种情况 ...