题目: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. yii框架通过控制台命令创建定时任务

    假设Yii项目路径为 /home/apps 1. 创建文件 /home/apps/web/protected/commands/console.php $yii = '/home/apps/frame ...

  2. C++扬帆远航——18(项目五2,递归式)

    /* * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:qiushi.cpp * 作者:常轩 * 微信公众号:World ...

  3. rsync 增量同步总是多两行数据

    从google云机器rsync日志到本地,并通过logstash格式化后存入elasticsearch,但在实施过程中发现,每次rsync后通过查看elasticsearch,都会将上次已同步的数据再 ...

  4. springboot配置文件读取pom文件信息

    解决的问题 springboot(当然别的也可以)多环境切换需要该配置文件,打包时不够方便. 解决: 配置文件能读取pom文件中的配置,根据命令选择不同配置注入springboot的配置文件中 pom ...

  5. leetcode 1365. How Many Numbers Are Smaller Than the Current Number

    Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. T ...

  6. 7-49 求前n项的阶乘之和 (15 分)

    从键盘输入一个整数n,求前n项的阶乘之和,1+2!+3!+...+n!的和 输入格式: 输入一个大于1的整数.例如:输入20. 输出格式: 输出一个整数.例如:2561327494111820313. ...

  7. 仿segmentfault-table横向滚动

    问题描述 自己的博客在用移动端访问时,如果table的列数足够多会显示不全,如下图红圈所示 正常情况如图 解决过程 使用chrome发现segmentfault的解决方法是在table上套一个tabl ...

  8. 【前端】这可能是你看过最全的css居中解决方案了~

    1.水平居中:行内元素解决方案 适用元素:文字,链接,及其其它inline或者inline-*类型元素(inline-block,inline-table,inline-flex) html部分代码: ...

  9. 【OO第三次课下讨论】农场主的饲料分配问题

    需求分析与项目设计   本思考题的设计需求是力图找到一个简单且可行的饲料分配方案,由于不涉及到饲料价格或者是营养均衡之类的优化问题,因此在假设总的饲料量必能满足所有动物的热量需求的前提下,我们只需要采 ...

  10. Vue2.0 【第二季】第3节 Vue.set全局操作

    目录 Vue2.0 [第二季]第3节 Vue.set全局操作 第3节:Vue.set全局操作 一.引用构造器外部数据 二.在外部改变数据的三种方法: 三.为什么要有Vue.set的存在? Vue2.0 ...