1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "message.h"

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <exception>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <regex>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <tuple>
#include <typeinfo>
#include <vector>

#include <unordered_set>
#include <unordered_map>
#include <bitset>

using namespace std;
#define rep(i,b) for(auto i=(0);i<(b);++i)
#define fo(i,a,b) for(auto i=(a);i<=(b);++i)
#define ford(i,a,b) for(auto i=(a);i>=(b);--i)
#define fore(a,b) for(auto &a:(b))
#define v vector
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define clr(x,a) memset(x,a,sizeof(x))

template <typename T, size_t N> struct ma : array<T,N> { T& operator[](size_t n) {
#ifdef DEBUG
           assert(n < N);
#endif
return (*static_cast<array<T,N>*>(this))[n]; } } ; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<string> vs; typedef ostringstream oss; typedef istringstream iss;
template<class T> string to_str(const T &a) { oss os; os << a; return os.str(); }
template<> string to_str<ld>(const ld& a) { oss os; os.precision(10); os.setf(ios::fixed); os << a; return os.str(); }
template<class T> T from_str(const string &s) { iss is; T val; is >> val; return val; }

const int cond = (ll)0;
#define db(x...) { if (id == 0 && cond > 0) { rep (xxx, 1) cerr << __LINE__ << " " << #x << " " << x << endl; cerr.flush(); } }
//template <class C, class=typename C::iterator> struct _cprint { using type = void;}; template<> struct _cprint<string> {};
//template <class C, class=typename _cprint<C>::type> ostream& operator<<(ostream &o, const C& v){ for(auto x:v) o<<x<<" "; return o; }


int nodes = 0;
int id = 0;
ll size = 0;
const int sz = (int)1e5 + 8077;
char aa[sz], bb[sz];
typedef array<int, 2> pii;
pii dp[2][sz];
pii le_hist[sz];
pii ri_hist[sz];

pii operator + (const pii &le, const pii &ri) {
    return {{ le[0] + ri[0], le[1] + ri[1]}};
}

typedef array<int,3> d3;
typedef array<int,4> d4;
int n, m;

#define min2(x, y) { auto z = y; if (x > y) x = y; }
void do_dp(int cur_sum, int next_sum, int left_job, int right_job) {
    fo (su, cur_sum, min(next_sum, n+m)) {
        { 
            int nsu = su + 2;
            int LE = max(left_job, max(0, nsu - m));
            int RI = min(right_job+2, min(nsu, n));
            if (LE <= RI)
                memset((void*)&dp[nsu%4] + 8 * LE, 0x7f, 8 * (RI-LE+1));
        }

        {
            int LE = max(left_job, max(0, su - m)); int RI = min(right_job, min(su, n));
            fo (a, LE, RI)  { int b = su - a;

                auto &cur = dp[su%4][a];

                auto &mid = dp[(su+2)%4][a+1];
                min2(mid, cur + pii({{aa[a] != bb[b] ? 1 : 0, aa[a] < bb[b] ? 1 : 0}}));

                // rem
                auto &top = dp[(su+1)%4][a];
                min2(top, cur + pii({{1,0}}));
                //top = min(top, cur + pii({{1,0}}));

                // add
                auto &ri = dp[(su+1)%4][a+1];
                min2(ri, cur + pii({{1, 0}}));
                // ri = min(ri, cur + pii({{1, 0}}));

                // rep?
                //mid = min(mid, cur + pii({{aa[a] != bb[b] ? 1 : 0, aa[a] < bb[b] ? 1 : 0}}));
            }
        }
    }
}



int main() {
    nodes = NumberOfNodes();
    id = MyNodeId();

#if 0
    n = 100000;
    m = 100000;
#else
    scanf("%d%d", &n, &m);
    scanf("%s%s", aa, bb);
#endif

    fo (i, 0, m) dp[0%2][i] = {{i,0}};
    fo (i, 0, m) dp[1%2][i] = {{(int)1e9, 0}};
    int le = (id * (m) + nodes - 1) / nodes;
    int ri = (((id + 1) * (m) + nodes - 1) / nodes);

    //if (id == 0) cerr << id <<" "<<le<<" "<<ri << endl;

    int diff = max(2, (n + 800 - 1) / 800);

    int m = 0;
    for (int rr = 0; rr <= n; rr += diff) {
        // get le 
        if (id > 0) { // r to r + diff
            Receive(id - 1);
            fo (r, rr, rr + diff) { rep (j, 2) le_hist[r][j] = GetInt(id - 1); }
        }
        else {
            fo (r, rr, rr + diff) le_hist[r] = {{ r, 0 }};
        }

        fo (r, rr, rr + diff) {
            dp[r%2][le] = le_hist[r];
            fo (c, le, ri) dp[(r+1)%2][c] = {{(int)1e9, (int)1e9}};
            fo (c, le, ri) {
                auto &cur = dp[r%2][c];


                // rem
                auto &top = dp[(r+1)%2][c];
                min2(top, cur + pii({{1,0}}));
                
                auto &mid = dp[(r+1)%2][c+1];
                min2(mid, cur + pii({{aa[r] != bb[c] ? 1 : 0, aa[r] < bb[c] ? 1 : 0}}));

                // add
                auto &ri = dp[r%2][c+1];
                min2(ri, cur + pii({{1, 0}}));
            }
            ri_hist[r] = dp[r%2][ri];
        }
        if (id != nodes - 1) {
            fo (r, rr, rr + diff) { rep (j, 2) PutInt(id + 1, ri_hist[r][j]); }

          //  if (id == 0) cerr << rr << " "<< m++ << endl;
            Send(id + 1);
        }
    }
    if (id == nodes - 1) {
        cout << ri_hist[n][0] << " " << ri_hist[n][1] << endl;
    }
    return 0;
}