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:
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
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.
1,问题在于如何才能想起这个floy,
要求只能插数,距离是i或者j,问题最小插入的数的个数=》距离相当于floy初始化里的内容,最小个数相当于最短路,(a直接到b,还是经过k过渡),结合数据范围即可
- #include
- using namespace std;
- #define int long long
- #define vec vector
- const int maxj=2e6+1000,mod=998244353,inf=0x7f7f7f7f7f7f7f;
- int a[20][20];
- string s;
- int b[120][120];
- int ask(int x,int y){//注重学最短路floy
- memset(b,mod,sizeof(b));
- for(int i=0;i<10;++i){
- b[i][(i+x)%10]=b[i][(i+y)%10]=1;
- }//初始化
- for(int k=0;k<10;++k){
- for(int i=0;i<10;++i){
- for(int j=0;j<10;++j){
- b[i][j]=min(b[i][j],b[i][k]+b[k][j]);
- }
- }//floy的板子,
- }int ans=0;
- for(int i=0;i
size()-1;++i){ - int aa=s[i]-'0',bb=s[i+1]-'0';
- if(b[aa][bb]>=mod)return -1;
- ans+=b[aa][bb]-1;//a到b一步到位时,不需插入
- }return ans;
- }
- void solve(){
- //暴力
- cin>>s;
- for(int i=0;i<10;++i){
- for(int j=0;j<10;++j){
- cout<<ask(i,j)<<' ';//暴力即可
- }cout<<'\n';
- }
- }
- signed main(){
- ios::sync_with_stdio(0);//能不用c语言的输入输出,尽量不要用
- cin.tie(0);
- cout.tie(0);
- int t;
- // cin>>t;
- t=1;
- while(t--)solve();
- return 0;
- }