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:
it prints 00, and adds 44 to its value, so the current value is 44, and the output is 00;
it prints 44, and adds 44 to its value, so the current value is 88, and the output is 0404;
it prints 88, and adds 44 to its value, so the current value is 1212, and the output is 048048;
it prints 22, and adds 22 to its value, so the current value is 1414, and the output is 04820482;
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 22 during 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
0840
output
-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.
因为0<x,y<10,所以枚举x和y,对于每对x和y用Floyd求出数字之间的最短路,每次只能加x或y,路径长度为1
代码:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll d[20][20],c[12][12],inf=99999999999999999;
void uu(ll x,ll y)
{
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if((i+x)%10==j || (i+y)%10==j)
{c[i][j]=1;}
else
{c[i][j]=inf;}
}
}
for(int k=0;k<10;k++)
{
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if(c[i][j]>c[i][k]+c[k][j])
{c[i][j]=c[i][k]+c[k][j];}
}
}
}
}
int main()
{
//uu(0,1);
char s[2000005];
gets(s);
ll n=strlen(s);
ll val,op;
for(int i=0;i<10;i++)
{
for(int j=i;j<10;j++)
{
uu(i,j);
val=0;
for(int k=1;k<n;k++)
{
op=s[k]-'0';
if(c[val][op]==inf)
{d[i][j]=-1;break;}
else
{d[i][j]+=(c[val][op]-1);}
val=op;
}
}
}
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if(j>=i)
{printf("%lld ",d[i][j]);}
else
{printf("%lld ",d[j][i]);}
}
printf("\n");
}
return 0;
}