malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   long long powr(long long a, long long b) {     long long res = 1;     while (b) {       if (b % 2 == 1) res = res * a % 1000000007;       a = a * a % 1000000007;       b /= 2;     }     return res;   }   int main() {     int p, k;     cin >> p >> k;     vector<int> g[p];     bool x[p];     memset(x, false, sizeof x);     vector<set<long long>> a;     if (k == 0) {       long long ans = powr(p, p - 1);       cout << ans <<   n ;       return 0;     }     for (int i = 0; i < p; ++i) {       if (x[i]) continue;       x[i] = 1;       long long yy = i;       set<long long> xz;       xz.insert(i);       do {         yy *= k;         yy %= p;         x[yy] = 1;         xz.insert(yy);       } while (yy != i);       a.push_back(xz);     }     long long ans = 1;     ans = powr(p, a.size() - 1);     if (k == 1) {       ans *= p;       ans %= 1000000007;     }     cout << ans <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   int n, m, k;   int main() {     cin >> n >> m >> k;     cout << m * (m - 1) / 2 << endl;     for (int i = 1; i <= m; i++) {       for (int j = i + 1; j <= m; j++) {         if (k)           cout << j <<     << i << endl;         else           cout << i <<     << j << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using uint = unsigned int;   using rl = double;   using pii = pair<int, int>;   const int MAXN = 3 * 100009;   int n, m;   struct Vert {     vector<int> to;     vector<pair<int, int>> qlist;     int depth;     ll vsum;   } v[MAXN];   ll psum[2 * MAXN];   void dfs(int it, int p, ll sum) {     v[it].depth = v[p].depth + 1;     for (auto q : v[it].qlist) {       psum[v[it].depth] += q.first;       psum[v[it].depth + q.second + 1] -= q.first;     }     sum += psum[v[it].depth];     v[it].vsum = sum;     for (auto t : v[it].to) {       if (t == p) continue;       dfs(t, it, sum);     }     for (auto q : v[it].qlist) {       psum[v[it].depth] -= q.first;       psum[v[it].depth + q.second + 1] += q.first;     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int a, b;       scanf( %d %d , &a, &b);       v[a].to.push_back(b);       v[b].to.push_back(a);     }     scanf( %d , &m);     for (int i = 0; i < m; i++) {       int it, d, x;       scanf( %d %d %d , &it, &d, &x);       v[it].qlist.push_back({x, min(d, n - 1)});     }     dfs(1, 0, 0);     for (int i = 1; i <= n; i++) {       printf( %lld  , v[i].vsum);     }     printf(  n );   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1010;   int C[N][N], dp[N][N];   int main() {     int m, x[110], y[110], sum[110];     sum[0] = 0;     scanf( %d , &m);     for (int i = 1; i <= m; i++) {       scanf( %d , x + i);       sum[i] = sum[i - 1] + x[i];     }     for (int i = 1; i <= m; i++) {       scanf( %d , y + i);     }     C[0][0] = 1;     for (int i = 1; i <= sum[m]; i++) {       C[i][0] = C[i][i] = 1;       for (int j = 1; j < i; j++) {         C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % 1000000007;       }     }     dp[0][0] = 1;     for (int i = 1; i <= m; i++) {       for (int j = 0; j <= sum[i]; j++) {         for (int k = 0; k <= min(y[i], j); k++) {           dp[i][j] =               (dp[i][j] + (long long)dp[i - 1][j - k] * C[sum[i] - (j - k)][k]) %               1000000007;         }       }     }     long long ans = dp[m][sum[m]] % 1000000007;     for (int i = 1; i <= m; i++) {       ans = (long long)(ans * C[sum[m] - sum[i - 1]][x[i]]) % 1000000007;     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char a[100005];   int b[100005];   map<pair<int, int>, int> maps;   int main() {     int nx, ny, x, y, x1, y1, n;     scanf( %d%d%d%d , &nx, &ny, &x, &y);     scanf(  %s , a);     for (int i = 0; i < 100005; i++) b[i] = 0;     b[0] = 1;     maps[make_pair(x, y)] = 1;     for (n = 0; a[n] !=   0 ; n++)       ;     for (int i = 0; i < n; i++) {       switch (a[i]) {         case  L :           y1 = y - 1;           x1 = x;           break;         case  R :           y1 = y + 1;           x1 = x;           break;         case  U :           x1 = x - 1;           y1 = y;           break;         case  D :           x1 = x + 1;           y1 = y;           break;       }       if (x1 <= nx && y1 <= ny && x1 > 0 && y1 > 0) {         if (maps[make_pair(x1, y1)] == 0) {           b[i + 1]++;           maps[make_pair(x1, y1)] = 1;         }         x = x1;         y = y1;       }     }     b[n] += nx * ny - maps.size();     for (int i = 0; i <= n; i++) printf( %d  , b[i]);     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     stack<char> st;     for (int i = 0; i < s.length(); i++) {       if (st.empty()) {         st.push(s[i]);       } else if (st.top() == s[i]) {         st.pop();       } else {         st.push(s[i]);       }     }     if (st.empty()) {       cout <<  Yes ;     } else {       cout <<  No ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long int N = 1e5 + 5;   const long long int mod = 1e15 + 7;   long long int dx[] = {1, -1, 0, 0};   long long int dy[] = {0, 0, 1, -1};   long long int y[N], yy[N], len[N];   long long int n, m, low, high, mid1, mid2;   double ans = (double)mod;   long long int ans_n, ans_m;   long long int a, b;   double find_distance(double x, double y, double u, double v) {     double temp = ((x - u) * (x - u) + (y - v) * (y - v));     temp = sqrt(temp);     return temp;   }   int main() {     cin >> n >> m >> a >> b;     for (long long int i = 1; i < n + 1; i++) {       cin >> y[i];     }     for (long long int i = 1; i < m + 1; i++) {       cin >> yy[i];     }     for (long long int i = 1; i < m + 1; i++) {       cin >> len[i];     }     for (long long int i = 1; i < m + 1; i++) {       low = 1;       high = n;       while ((high - low) > 5) {         mid1 = low + (high - low) / 3;         mid2 = low + 2 * (high - low) / 3;         double dis1 = find_distance(0, 0, a, y[mid1]) +                       find_distance(a, y[mid1], b, yy[i]) + len[i];         double dis2 = find_distance(0, 0, a, y[mid2]) +                       find_distance(a, y[mid2], b, yy[i]) + len[i];         if (dis1 < dis2) {           high = mid2;         } else {           low = mid1;         }       }       for (long long int zz = low; zz < high + 1; zz++) {         double temp = find_distance(0, 0, a, y[zz]) +                       find_distance(a, y[zz], b, yy[i]) + len[i];         if (temp < ans) {           ans = temp;           ans_n = zz;           ans_m = i;         }       }     }     cout << ans_n <<     << ans_m << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ii = tuple<long long, long long>;   const long long N = 1e6 + 7, inf = 1e9 + 42;   long long binpow(long long a, long long b) {     if (b == 0) return 1;     long long res = binpow(a, b / 2);     if (b % 2)       return (res * res * a) % 1000000007;     else       return (res * res) % 1000000007;   }   bool compare(pair<long long, long long> a, pair<long long, long long> b) {     return (a.first - a.second > b.first - b.second);   }   long long n;   vector<long long> prime_factors;   signed main() {     puts( Odd );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using pil = pair<long long, long long>;   using pii = pair<long long, long long>;   using pid = pair<double, double>;   long long mod = 1e9 + 7;   long long power(long long a, long long n) {     long long res = 1;     while (n) {       if (n % 2) res = (res * a) % mod;       a = (a * a) % mod;       n /= 2;     }     return res;   }   void solve() {     long long a, b;     cin >> a >> b;     long long B = (b * (b - 1) / 2) % mod;     long long A = ((a * (a + 1) / 2) % mod) * b + a;     A %= mod;     long long ans = (A * B) % mod;     cout << ans << endl;   }   int32_t main() {     long long t = 1;     while (t--) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 20;   int n, m;   struct line {     int type, x, y, lag;     line() { type = x = y = lag = 0; }     line(int a, int b, int c, int d) { type = a, x = b, y = c, lag = d; }     bool operator<(const line &a) const { return y > a.y; }   } t[7 * N];   int a[N], qu;   set<int> s[N];   long long f[N], ans[N];   void add(int x, int y) {     while (x <= n) {       f[x] += y;       x += x & (-x);     }   }   long long query(int x) {     long long ret = 0;     while (x) {       ret += f[x];       x -= x & (-x);     }     return ret;   }   void cdq(int l, int r) {     if (l == r) return;     int mid = l + r >> 1;     cdq(l, mid), cdq(mid + 1, r);     int j = l;     for (int i = mid + 1; i <= r; i++) {       while (j <= mid && t[j].y >= t[i].y) {         if (t[j].type == 0) add(t[j].x, (t[j].x - t[j].y) * t[j].lag);         j++;       }       if (t[i].type == 1) ans[t[i].lag] += query(t[i].x);     }     for (int i = l; i < j; i++)       if (t[i].type == 0) add(t[i].x, -(t[i].x - t[i].y) * t[i].lag);     sort(t + l, t + r + 1);   }   set<int>::iterator it;   int main() {     scanf( %d%d , &n, &m);     int cnt = 0;     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       if (s[a[i]].size()) {         it = s[a[i]].end();         it--;         t[++cnt] = line(0, i, *it, 1);       } else         t[++cnt] = line(0, i, 0, 1);       s[a[i]].insert(i);     }     int opt, x, y;     for (int i = 1; i <= m; i++) {       scanf( %d%d%d , &opt, &x, &y);       if (opt == 1) {         int L = 0, R = 0;         it = s[a[x]].find(x);         if (it != s[a[x]].begin()) t[++cnt] = line(0, x, L = *--it, -1), it++;         if (++it != s[a[x]].end()) t[++cnt] = line(0, R = *it, x, -1);         if (L && R) t[++cnt] = line(0, R, L, 1);         s[a[x]].erase(x);         s[a[x] = y].insert(x);         it = s[y].find(x), L = R = 0;         if (it != s[y].begin()) t[++cnt] = line(0, x, L = *--it, 1), it++;         if (++it != s[y].end()) t[++cnt] = line(0, R = *it, x, 1);         if (L && R) t[++cnt] = line(0, R, L, -1);       } else         t[++cnt] = line(1, y, x, ++qu);     }     cdq(1, cnt);     for (int i = 1; i <= qu; i++) printf( %lld n , ans[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a, b, n, i, p[1001], sz[1001], cnt[1001];   vector<pair<int, int> > v2;   vector<int> v;   int find_p(int a) {     if (p[a] == a) return a;     return p[a] = find_p(p[a]);   }   void unionn(int a, int b) {     int _a = a, _b = b;     a = find_p(a);     b = find_p(b);     if (a != b) {       if (sz[a] < sz[b]) swap(a, b);       p[b] = a;       sz[a] += sz[b];     } else       v2.push_back({_a, _b});   }   int main() {     cin >> n;     for (i = 1; i <= n; i++) p[i] = i, sz[i] = 1;     for (i = 1; i < n; i++) {       cin >> a >> b;       unionn(a, b);     }     cout << v2.size() << endl;     for (i = 1; i <= n; i++) {       p[i] = find_p(i);       if (!cnt[p[i]]) {         v.push_back(p[i]);         cnt[p[i]] = 1;       }     }     for (i = 0; i < v2.size(); i++) {       cout << v2[i].first <<     << v2[i].second <<     << v[i] <<     << v[i + 1]            << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   auto main() -> int {     int N, X, Y;     cin >> N;     vector<int> C(N);     for (int i = 0; i < N; i++) cin >> C[i];     cin >> X >> Y;     int l = 0;     int ret = 0;     for (int i = 0; i < N; i++) {       if (l < X) {         if (l + C[i] <= Y)           l += C[i];         else           break;       } else if (l >= X && l <= Y) {         int t = 0;         for (int j = i; j < N; j++) t += C[j];         if (t >= X && t <= Y) {           ret = i + 1;           break;         } else {           l += C[i];         }       }     }     cout << ret << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int N, M, i, j;   int a, b, v[105];   bool ok = true;   int main() {     cin >> N >> M;     for (i = 1; i <= N; i++) {       cin >> a >> b;       a++;       v[a]++;       v[b + 1]--;     }     for (i = 1; i <= M; i++) {       v[i] += v[i - 1];       if (v[i] == 0) ok = false;     }     if (ok == true)       cout <<  YES ;     else       cout <<  NO ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int OO = 1e9, N = 1e5;   int main() {     long long n, c = 0, o = 0;     cin >> n;     string s;     cin >> s;     queue<int> q;     for (int i = 0; i < n; i++) {       o += (s[i] ==  ( );       c += (s[i] ==  ) );     }     int sma7 = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  )  && !q.empty())         q.pop();       else if (s[i] ==  ) )         sma7++;       else         q.push(0);     }     if (o == c && sma7 <= 1)       cout <<  YES ;     else       cout <<  NO ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, a[100009], x[100009], h[100009], id[100009], seg[400009];   void upd(int pos, int l, int r, int pp, int v) {     if (l == r) {       seg[pos] = v;       return;     }     int m = (l + r) / 2;     if (m >= pp)       upd(2 * pos + 1, l, m, pp, v);     else       upd(2 * pos + 2, m + 1, r, pp, v);     seg[pos] = max(seg[2 * pos + 1], seg[2 * pos + 2]);   }   int qu(int pos, int l, int r, int b, int e) {     if (b <= l && r <= e) return seg[pos];     int m = (l + r) / 2;     int r1 = 0, r2 = 0;     if (m >= b) r1 = qu(2 * pos + 1, l, m, b, e);     if (m < e) r2 = qu(2 * pos + 2, m + 1, r, b, e);     return max(r1, r2);   }   int ans[100009];   void bs(int i, int l, int r) {     int m;     int o = x[i] + h[i] - 1;     while (r - l > 1) {       m = (l + r) / 2;       if (o < x[m])         r = m;       else         l = m;     }     if (l == i)       ans[id[i]] = i;     else       ans[id[i]] = qu(0, 0, n - 1, i + 1, l);     upd(0, 0, n - 1, i, ans[id[i]]);     ans[id[i]] -= i;     ans[id[i]]++;   }   pair<int, pair<int, int> > p[100009];   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x[i] >> h[i];       p[i] = {x[i], {h[i], i}};     }     sort(p, p + n);     for (int i = 0; i < n; i++) {       x[i] = p[i].first;       h[i] = p[i].second.first;       id[i] = p[i].second.second;     }     ans[id[n - 1]] = 1;     upd(0, 0, n - 1, n - 1, n - 1);     for (int i = n - 2; i >= 0; i--) {       bs(i, i, n);     }     for (int i = 0; i < n; i++) cout << ans[i] <<    ;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> g[100100];   pair<int, int> DFS(int now) {     int all[2][2] = {};     int odd = 1, eve = 0, codd, ceve, todd, teve, csz, sw[2];     pair<int, int> tmp;     all[0][0] = all[1][0] = 1;     for (int i = 0; i < g[now].size(); i++) {       tmp = DFS(g[now][i]);       codd = tmp.first;       ceve = tmp.second + 1;       todd = (1LL * odd * ceve % 1000000007 + 1LL * eve * codd % 1000000007) %              1000000007;       teve = (1LL * eve * ceve % 1000000007 + 1LL * odd * codd % 1000000007) %              1000000007;       eve = teve;       odd = todd;       all[0][0] = 1LL * all[0][0] * ceve % 1000000007;       sw[0] = (1LL * all[1][1] * codd % 1000000007 + all[1][0]) % 1000000007;       sw[1] = (1LL * all[1][0] * codd % 1000000007 + all[1][1]) % 1000000007;       all[1][1] = sw[1];       all[1][0] = sw[0];     }     return make_pair(         ((odd + odd - all[0][0]) % 1000000007 + 1000000007) % 1000000007,         ((eve + eve - all[1][1]) % 1000000007 + 1000000007) % 1000000007);   }   int main() {     int n, p, i;     pair<int, int> ans;     scanf( %d , &n);     for (i = 2; i <= n; i++) {       scanf( %d , &p);       g[p].push_back(i);     }     ans = DFS(1);     printf( %d n , (ans.first + ans.second) % 1000000007);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int arr[1005];   int res[1005];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 0; i < n; ++i) cin >> arr[i];     sort(arr, arr + n);     int ini = 0, fin = n - 1, idx = 0;     while (ini <= fin) {       res[idx] = arr[ini++];       res[idx + 1] = arr[fin--];       idx += 2;     }     for (int i = 0; i < n; ++i) cout << res[i] <<    ;     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-8;   const double pi = acos(-1);   const long long inf = 0x3f3f3f3f3f3f3f3f;   const int INF = 0x3f3f3f3f;   const int MAX = 2e5 + 10;   const long long mod = 1e9 + 7;   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   char c[3] = { R ,  G ,  B };   int main() {     int t;     cin >> t;     while (t--) {       int dif[3] = {0};       int n, k;       cin >> n >> k;       string s;       cin >> s;       for (int i = (0); i < (3); ++i) {         for (int j = (0); j < (k); ++j) {           if (s[j] != c[(j + i) % 3]) dif[i]++;         }       }       int ans = 0;       ans = min(dif[0], min(dif[1], dif[2]));       for (int i = 1; i + k - 1 < n; i++) {         int a0 = dif[0], a1 = dif[1], a2 = dif[2];         dif[0] = a2 - (s[i - 1] ==  B  ? 0 : 1) +                  (s[i + k - 1] == c[(k - 1) % 3] ? 0 : 1);         dif[1] =             a0 - (s[i - 1] ==  R  ? 0 : 1) + ((s[i + k - 1] == c[k % 3]) ? 0 : 1);         dif[2] = a1 - (s[i - 1] ==  G  ? 0 : 1) +                  ((s[i + k - 1] == c[(k + 1) % 3]) ? 0 : 1);         ans = min(ans, min(dif[0], min(dif[1], dif[2])));       }       cout << ans <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 500005, INF = 0x3f3f3f3f;   int ta, tb, n, T;   long long t[maxn], t1[maxn];   int main(void) {     cin >> n >> ta >> tb >> T;     string s;     char a;     cin >> a;     if (a ==  w ) T -= tb;     T--;     if (T < 0) {       cout << 0 << endl;       return 0;     }     cin >> s;     for (int i = 0; i < n - 1; i++) {       t[i] = t[i - 1] + 1 + ta;       if (s[i] ==  w ) t[i] += tb;     }     reverse(s.begin(), s.end());     for (int i = 0; i < n - 1; i++) {       t1[i] = t1[i - 1] + 1 + ta;       if (s[i] ==  w ) t1[i] += tb;     }     int res = 0;     for (int i = 0; i < n; i++) {       if (t1[i - 1] > T) break;       int l = 0, r = n - i;       while (l < r - 1) {         int mid = (l + r) / 2;         if (mid > 0 && t1[i - 1] + t[mid - 1] + i * ta <= T)           l = mid;         else           r = mid;       }       res = max(res, l + i);     }     for (int i = 0; i < n; i++) {       if (t[i - 1] > T) break;       int l = 0, r = n - i;       while (l < r - 1) {         int mid = (l + r) / 2;         if (mid > 0 && t[i - 1] + t1[mid - 1] + i * ta <= T)           l = mid;         else           r = mid;       }       res = max(res, l + i);     }     cout << res + 1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool dif[10];   int p[1005], n, x, y, ans1, ans2;   inline void chk(int p) {     int i, sz = 0, res;     for (i = 1; i <= n; i++) {       if (i & (1 << p)) {         sz++;       }     }     if (sz == 0) {       dif[p] = 0;       return;     }     printf( ? %d  , sz);     for (i = 1; i <= n; i++) {       if (i & (1 << p)) {         printf( %d  , i);       }     }     printf(  n );     fflush(stdout);     scanf( %d , &res);     if (sz & 1) {       if (res == x) {         dif[p] = 0;         return;       }       if (res == y) {         dif[p] = 1;         return;       }     } else {       if (res == (x ^ y)) {         dif[p] = 1;         return;       }       if (res == 0) {         dif[p] = 0;         return;       }     }   }   inline bool ok(int pr) {     int res, i;     printf( ? %d  , pr);     for (i = 1; i <= pr; i++) {       printf( %d  , p[i]);     }     printf(  n );     fflush(stdout);     scanf( %d , &res);     if (pr & 1) {       if (res == x) {         return 0;       }       if (res == y) {         return 1;       }     } else {       if (res == (x ^ y)) {         return 1;       }       if (res == 0) {         return 0;       }     }   }   inline int get(int x) {     int i, cnt = 0;     for (i = 1; i <= n; i++) {       if (!(i & (1 << x))) {         continue;       }       p[++cnt] = i;     }     int l = 1, r = cnt;     while (l <= r) {       int mid = (l + r) >> 1;       if (ok(mid)) {         r = mid - 1;       } else {         l = mid + 1;       }     }     return p[l];   }   int main() {     int i;     scanf( %d%d%d , &n, &x, &y);     for (i = 9; i >= 0; i--) {       chk(i);     }     for (i = 9; i >= 0; i--) {       if (dif[i]) {         ans1 = get(i);         break;       }     }     ans2 = ans1;     for (i = 9; i >= 0; i--) {       if (dif[i]) {         ans2 ^= (1 << i);       }     }     printf( ! %d %d n , ans2, ans1);     fflush(stdout);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string s;   queue<char> q;   vector<int> sp;   int main() {     long long n, l = 0, r = 0, k = 0;     cin >> s;     if (s[0] !=  a  && s[0] !=  e  && s[0] !=  u  && s[0] !=  o  && s[0] !=  i ) {       q.push(s[0]);       k = 1;     }     for (int i = 1; i < s.size(); i++) {       if (s[i] !=  a  && s[i] !=  e  && s[i] !=  u  && s[i] !=  o  &&           s[i] !=  i ) {         if (s[i] != s[i - 1]) q.push(s[i]);         k++;         if (k >= 3 && q.size() > 1) {           sp.push_back(i);           n = q.size();           while (q.size() > 1) q.pop();           k = 1;         }       } else {         while (q.size() != 0) q.pop();         k = 0;       }     }     int j = 0;     sp.push_back(s.size());     for (int i = 0; i < s.size(); i++) {       while (i < sp[j]) {         cout << s[i];         i++;       }       j++;       if (i < s.size()) cout <<     << s[i];     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int arr[100005];   int brr[100005];   char s[1234][1234];   int vis[1234][1234];   int has[1234][1234];   int f[1234567];   int n, m, c, g;   void dfs(int x, int y) {     if (x < 0 || y < 0 || x == n || y == m || vis[x][y] || s[x][y] ==  * ) return;     if (s[x][y] ==  . ) {       vis[x][y] = 1;       has[x][y] = g;       f[g]++;       dfs(x, y + 1);       dfs(x, y - 1);       dfs(x - 1, y);       dfs(x + 1, y);     }   }   int main() {     cin >> n >> m;     int i, j;     for (i = 0; i < n; i++) {       for (j = 0; j < m; ++j) {         cin >> s[i][j];       }     }     for (i = 0; i < n; ++i) {       for (j = 0; j < m; ++j) {         if (s[i][j] ==  .  && vis[i][j] == 0) {           g++;           dfs(i, j);         }       }     }     for (i = 0; i < n; ++i) {       for (j = 0; j < m; ++j) {         c = 1;         set<int> se;         if (s[i][j] ==  * ) {           if (j > 0) se.insert(has[i][j - 1]);           if (i > 0) se.insert(has[i - 1][j]);           if (j + 1 < m) se.insert(has[i][j + 1]);           if (i + 1 < n) se.insert(has[i + 1][j]);           for (set<int>::iterator it = se.begin(); it != se.end(); ++it) {             c = c + f[*it];           }           cout << c % 10;         } else           cout <<  . ;       }       cout << endl;     }   }