malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   vector<int> tab[100100], ans;   int n, ls[10];   bool used[100100];   bool chek(int a, int b) {     for (int i = 0; i < 4; i++)       if (tab[a][i] == b) return true;     return false;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n + n; i++) {       int a, b;       scanf( %d%d , &a, &b);       if (a == b) {         puts( -1 );         return 0;       }       tab[a].push_back(b);       tab[b].push_back(a);     }     for (int i = 1; i <= n; i++) {       if (tab[i].size() != 4) {         puts( -1 );         return 0;       }     }     if (n <= 10) {       int ar[30] = {};       for (int i = 0; i < n; i++) ar[i] = i + 1;       do {         ar[n] = ar[0];         ar[n + 1] = ar[1];         bool ok = true;         for (int i = 0; i < n; i++) {           if (!chek(ar[i], ar[i + 1]) || !chek(ar[i], ar[i + 2])) {             ok = false;             break;           }         }         if (ok) {           for (int i = 0; i < n; i++)             printf( %d%c , ar[i], i == n - 1 ?   n  :    );           return 0;         }       } while (std::next_permutation(ar, ar + n));       puts( -1 );       return 0;     }     int start = 1, pre = -1;     while (!used[start]) {       used[start] = true;       ans.push_back(start);       int nxt = start;       for (int i = 0; i < 4; i++) {         int cnt = 0;         for (int j = 0; j < 4; j++)           if (chek(tab[start][i], tab[start][j])) cnt++;         if (cnt == 2 && tab[start][i] != pre) {           nxt = tab[start][i];           break;         }       }       if (nxt == start) {         puts( -1 );         return 0;       }       pre = start;       start = nxt;     }     if ((int)ans.size() < n) {       puts( -1 );       return 0;     }     ans.push_back(ans[0]);     ans.push_back(ans[1]);     for (int i = 0; i < n; i++) {       if (!chek(ans[i], ans[i + 1]) || !chek(ans[i], ans[i + 2])) {         puts( -1 );         return 0;       }     }     for (int i = 0; i < n; i++) printf( %d%c , ans[i], i == n - 1 ?   n  :    );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, q, sum, x;   int main(void) {     scanf( %lld%lld , &n, &q);     while (q--) {       scanf( %lld , &x);       while (x % 2 == 0) {         sum = (x + 1) / 2 + 1, sum = n - sum, x += sum + 1;       }       printf( %lld n , x / 2 + 1);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   set<pair<int, int> > s[2];   bool eq(pair<int, int> a, pair<int, int> b) {     return (a.first == b.first && a.second == b.second);   }   int main() {     int n, m, i, a, b, j;     scanf( %d%d , &n, &m);     for (i = 0; i < n; i++) {       scanf( %d%d , &a, &b);       if (b < a) swap(a, b);       s[0].insert(make_pair(a, b));     }     for (i = 0; i < m; i++) {       scanf( %d%d , &a, &b);       if (b < a) swap(a, b);       s[1].insert(make_pair(a, b));     }     set<int> ans;     for (auto i : s[0])       for (auto j : s[1]) {         if (i.first == j.first && i.second == j.second)           continue;         else if (i.first != j.first && i.first != j.second &&                  i.second != j.first && i.second != j.second)           continue;         else if (i.first == j.first) {           for (auto k : s[0]) {             if (eq(k, j)) continue;             if (k.first == j.second || k.second == j.second)               return !printf( -1 n );           }           for (auto k : s[1]) {             if (eq(k, i)) continue;             if (k.first == i.second || k.second == i.second)               return !printf( -1 n );           }           ans.insert(i.first);         } else if (i.first == j.second) {           for (auto k : s[0]) {             if (eq(k, j)) continue;             if (k.first == j.first || k.second == j.first) return !printf( -1 n );           }           for (auto k : s[1]) {             if (eq(k, i)) continue;             if (k.first == i.second || k.second == i.second)               return !printf( -1 n );           }           ans.insert(i.first);         } else if (i.second == j.second) {           for (auto k : s[0]) {             if (eq(k, j)) continue;             if (k.first == j.first || k.second == j.first) return !printf( -1 n );           }           for (auto k : s[1]) {             if (eq(k, i)) continue;             if (k.first == i.first || k.second == i.first) return !printf( -1 n );           }           ans.insert(i.second);         } else if (i.second == j.first) {           for (auto k : s[0]) {             if (eq(k, j)) continue;             if (k.first == j.second || k.second == j.second)               return !printf( -1 n );           }           for (auto k : s[1]) {             if (eq(k, i)) continue;             if (k.first == i.first || k.second == i.first) return !printf( -1 n );           }           ans.insert(i.second);         }       }     if ((int)ans.size() > 1) return !printf( 0 n );     cout << *ans.begin() << endl;   }
#include <bits/stdc++.h>   using namespace std;   using LL = long long;   const LL INF = 2000000000000000000L;   struct Event {     char c;     LL v;     int pos;   };   void Dfs(int pos, vector<vector<pair<LL, int>>>& edge, vector<bool>& parallel,            vector<LL>& ans) {     if (edge[pos].size() == 0) {       ans[pos] = 1;       return;     }     if (parallel[pos]) {       for (auto [vv, j] : edge[pos]) {         Dfs(j, edge, parallel, ans);       }     } else {       int j = 0;       for (int i = 0; i < edge[pos].size(); ++i) {         if (edge[pos][i].first < edge[pos][j].first) j = i;       }       Dfs(edge[pos][j].second, edge, parallel, ans);     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int times;     cin >> times;     while (times--) {       int n;       cin >> n;       string ss;       getline(cin, ss);       string s;       for (char c : ss) {         if (c ==    ) continue;         s.push_back(c);       }       vector<vector<pair<LL, int>>> edge(s.size());       vector<bool> sign(s.size());       stack<Event> st;       for (int i = 0; i < s.size(); ++i) {         if (s[i] ==  ) ) {           vector<pair<LL, int>> x;           bool parallel = false;           int j = i;           while (true) {             Event e = st.top();             st.pop();             if (e.c ==  ( ) {               j = e.pos;               break;             }             if (e.c ==  P ) parallel = true;             if (e.c ==  S ) parallel = false;             if (e.c ==  * ) {               x.push_back({e.v, e.pos});             }           }           sign[j] = parallel;           for (int i = 0; i < x.size(); ++i) {             edge[j].push_back(x[i]);           }           if (parallel) {             LL ans = 0;             for (auto [a, b] : x) ans += a;             st.push({ * , ans, j});           } else {             LL ans = INF;             for (auto [a, b] : x) ans = min(ans, a);             st.push({ * , ans, j});           }         } else {           st.push({s[i], 1, i});         }       }       vector<LL> ans(s.size());       Dfs(0, edge, sign, ans);       cout <<  REVOLTING  ;       for (int i = 0; i < s.size(); ++i) {         if (s[i] ==  * ) cout << ans[i] * n * st.top().v <<    ;       }       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int basecon(int n, int b);   int gcd(int a, int b) {     if (b == 0)       return a;     else       return gcd(b, a % b);   }   int basecon(int n, int b) {     int sum = 0;     while (n) {       sum = sum + (n % b);       n = n / b;     }     return sum;   }   int main() {     int n, sum = 0, y, g;     cin >> n;     y = n - 2;     for (int i = 2; i < n; ++i) {       sum = sum + basecon(n, i);     }     g = gcd(sum, y);     sum = sum / g;     y = y / g;     cout << sum <<  /  << y << endl;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> v, ans;   void op(int i) {     int x = v[i + 2];     v[i + 2] = v[i + 1];     v[i + 1] = v[i];     v[i] = x;     if (ans.size() >= 2 && ans[ans.size() - 1] == i + 1 &&         ans[ans.size() - 2] == i + 1)       ans.pop_back(), ans.pop_back();     else       ans.push_back(i + 1);   }   void reach(int x, int y) {     while (y > x + 1) op(y - 2), y -= 2;     if (y == x + 1) op(y - 1), op(y - 1);   }   int main(int argc, char **argv) {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       int n, st = 0, temp = 0;       ans.clear();       cin >> n;       v.resize(n);       vector<int> ar(501, -1);       int val = -1, x, y;       for (int i = 0; i < n; i++) {         cin >> v[i];         if (ar[v[i]] != -1) val = v[i], x = ar[v[i]], y = i;         ar[v[i]] = i;       }       if (val != -1) v[x] = v[y] = 2000;       while (n - st > 3) {         int mi = 1000, in;         for (int j = st; j < n; j++)           if (v[j] < mi) mi = v[j], in = j;         reach(st, in);         st++;       }       if (val == -1) {         while (!(v[st] <= v[st + 1] && v[st + 1] <= v[st + 2])) {           op(st), temp++;           if (temp > 5) {             cout << -1 << endl;             goto brk;           }         }       } else {         while (v[st + 1] != 2000 || v[st + 2] != 2000) op(st);         while (st >= 0 && val < v[st]) op(st), op(st), st--;       }       cout << ans.size() << endl;       for (auto x : ans) cout << x <<    ;       cout << endl;     brk:;     }   }
#include <bits/stdc++.h>   using namespace std;   char a[5004][5004];   int vis[5004] = {0};   int fl = 0;   int n;   void dfs(int x, int par) {     if (fl == 1) return;     vis[x] = 1;     for (int i = 0; i < n; i++) {       if (a[x][i] ==  1  && a[i][par] ==  1 ) {         printf( %d %d %d n , par + 1, x + 1, i + 1);         fl = 1;         return;       }       if (vis[i] == 0 && a[x][i] ==  1 ) {         if (fl == 1) return;         dfs(i, x);         if (fl == 1) return;       }     }   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %s , a[i]);     }     for (int i = 0; i < n; i++) {       if (vis[i] == 0) {         dfs(i, i);       }     }     if (fl == 0) {       printf( -1 n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int il[5];   int wyn = 0;   void printState() {}   int n;   int main() {     ios::sync_with_stdio(0);     cin >> n;     while (n--) {       int tmp;       cin >> tmp;       il[tmp]++;     }     int tmp;     if (il[1] > il[2]) {       wyn += il[2];       il[3] += il[2];       il[1] -= il[2];       il[2] = 0;       printState();       wyn += il[1] / 3 * 2;       il[3] += il[1] / 3;       il[1] = il[1] % 3;       printState();       if (il[1] == 1 && il[3] > 0) {         tmp = min(il[1], il[3]);         wyn += tmp;         il[1] -= tmp;         il[3] -= tmp;         il[4] += tmp;       } else if (il[1] == 1) {         printState();         tmp = min(1, il[4] / 2);         il[1] -= tmp;         il[4] -= 2 * tmp;         wyn += 2 * tmp;       } else if (il[1] == 2 && il[4] > 0) {         wyn += il[1];         il[4] -= il[1];         il[3] += il[1] * 2;         il[1] = 0;       } else if (il[1] == 2) {         tmp = min(il[1], il[3]);         wyn += tmp;         il[1] -= tmp;         il[3] -= tmp;       }       printState();     } else {       wyn += il[1];       il[3] += il[1];       il[2] -= il[1];       il[1] = 0;       printState();       wyn += il[2] / 3 * 2;       il[3] += il[2] / 3 * 2;       il[2] = il[2] % 3;       printState();       if (il[2] == 1 && il[4] > 0) {         il[2] = 0;         il[4] -= 1;         il[3] += 2;         wyn += 1;       } else if (il[2] == 1 && il[3] >= 2) {         il[3] -= 2;         il[2] -= 1;         il[4] += 2;         wyn += 2;       } else if (il[2] == 2) {         il[4] += 1;         il[2] -= 2;         wyn += 2;       }       printState();     }     cout << (il[2] + il[1] > 0 ? -1 : wyn) <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e3 + 20;   bool A[maxn][maxn];   int d[maxn];   vector<pair<int, int> > ans;   void solve(set<int> &a) {     if ((int)a.size() == 1) {       int k = *a.begin();       for (int i = 0; i < k + 1; i++)         for (int j = i + 1; j < k + 1; j++) A[i][j] = A[j][i] = 1;       return;     }     set<int> tmp;     int k = *a.rbegin(), mn = *a.begin();     for (auto x : a)       if (x != k) tmp.insert(k - x);     solve(tmp);     for (int i = 0; i <= k - mn; i++)       for (int j = i + 1; j <= k - mn; j++)         A[i][j] = !A[i][j], A[j][i] = !A[j][i];     for (int i = 0; i <= k - mn; i++)       for (int j = k - mn + 1; j <= k; j++) A[i][j] = A[j][i] = 1;     for (int i = k - mn + 1; i <= k; i++)       for (int j = i + 1; j <= k; j++) A[i][j] = A[j][i] = 1;   }   int main() {     int m;     cin >> m;     set<int> tmp;     for (int i = 0; i < m; i++) cin >> d[i], tmp.insert(d[i]);     solve(tmp);     for (int i = 0; i <= d[m - 1]; i++)       for (int j = i + 1; j <= d[m - 1]; j++)         if (A[i][j]) ans.push_back({i, j});     printf( %d n , (int)ans.size());     for (auto x : ans) printf( %d %d n , x.first + 1, x.second + 1);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int n, m, i;     cin >> n >> m;     long long int arr[n + 5];     map<long long int, long long int> ma;     queue<long long int> q;     for (i = 0; i < n; i++) {       cin >> arr[i];       ma[arr[i]] = 0;       q.push(arr[i]);     }     long long int result = 0, cnt = 0;     long long int ans[m + 5];     while (!q.empty()) {       if (cnt >= m) break;       long long int x = q.front();       if (!ma.count(x + 1)) {         q.push(x + 1);         ans[cnt] = x + 1;         ma[x + 1] = ma[x] + 1;         result += ma[x + 1];         cnt++;       }       if (cnt >= m) break;       if (!ma.count(x - 1)) {         q.push(x - 1);         ans[cnt] = x - 1;         ma[x - 1] = ma[x] + 1;         result += ma[x - 1];         cnt++;       }       q.pop();     }     cout << result <<   n ;     for (i = 0; i < m; i++) cout << ans[i] <<    ;     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX_H = 100;   const int MAX_W = 100;   int hs[MAX_H], vs[MAX_W];   int ps[MAX_H][MAX_W], as[MAX_H * MAX_W];   int main() {     int h, w, q;     scanf( %d%d%d , &h, &w, &q);     for (int y = 0, p = 0; y < h; y++)       for (int x = 0; x < w; x++, p++) ps[y][x] = p;     while (q--) {       int op;       scanf( %d , &op);       if (op == 1) {         int y;         scanf( %d , &y);         y--;         int tp = ps[y][0];         for (int x = 1; x < w; x++) ps[y][x - 1] = ps[y][x];         ps[y][w - 1] = tp;       } else if (op == 2) {         int x;         scanf( %d , &x);         x--;         int tp = ps[0][x];         for (int y = 1; y < h; y++) ps[y - 1][x] = ps[y][x];         ps[h - 1][x] = tp;       } else {         int y, x, v;         scanf( %d%d%d , &y, &x, &v);         y--, x--;         as[ps[y][x]] = v;       }     }     for (int y = 0, p = 0; y < h; y++)       for (int x = 0; x < w; x++, p++) {         printf( %d , as[p]);         putchar(x + 1 < w ?     :   n );       }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long INF = 1000000000000000000;   struct fun {     long long a, b, nr;   };   void Dodaj(deque<fun>& D, fun nowy) {     while (D.size() >= 2) {       fun ost = D.front();       D.pop_front();       fun przed = D.front();       if ((ost.b - nowy.b) * (ost.a - przed.a) >           (nowy.a - ost.a) * (przed.b - ost.b)) {         D.push_front(ost);         break;       }     }     D.push_front(nowy);   }   int Pytaj(deque<fun>& D, long long x) {     while (D.size() >= 2) {       fun ost = D.back();       D.pop_back();       fun przed = D.back();       if (ost.a * x + ost.b < przed.a * x + przed.b) {         D.push_back(ost);         break;       }     }     return D.back().nr;   }   int main() {     ios_base::sync_with_stdio(0);     int n, m, p;     cin >> n >> m >> p;     vector<long long> s(n);     s[0] = 0;     for (int i = 1; i < n; ++i) {       cin >> s[i];       s[i] += s[i - 1];     }     vector<long long> a(m);     for (int i = 0; i < m; ++i) {       int h, t;       cin >> h >> t;       --h;       a[i] = t - s[h];     }     sort(a.begin(), a.end());     vector<long long> prefA(m);     prefA[0] = a[0];     for (int i = 1; i < m; ++i) prefA[i] = prefA[i - 1] + a[i];     vector<vector<long long> > dp(p, vector<long long>(m, INF));     for (int i = 0; i < m; ++i) dp[0][i] = a[i] * (i + 1) - prefA[i];     for (int i = 1; i < p; ++i) {       deque<fun> D;       Dodaj(D, {-(i - 1), dp[i - 1][i - 1] + prefA[i - 1], i - 1});       for (int j = i; j < m; ++j) {         int nr = Pytaj(D, a[j]);         dp[i][j] = dp[i - 1][nr] + a[j] * (j - nr) - prefA[j] + prefA[nr];         Dodaj(D, {-j, dp[i - 1][j] + prefA[j], j});       }     }     long long odp = INF;     for (int i = 0; i < p; ++i) odp = min(odp, dp[i][m - 1]);     cout << odp;     return 0;   }
#include <bits/stdc++.h>   using std::abs;   using std::cerr;   using std::cin;   using std::cout;   using std::map;   using std::max;   using std::min;   using std::pair;   using std::set;   using std::string;   using std::swap;   using std::vector;   using ll = long long;   using uint = unsigned int;   using pii = pair<int, int>;   using pll = pair<ll, ll>;   template <typename T>   void err(vector<string>::iterator it, T a) {     cerr << it->substr((*it)[0] ==    ) <<   =   << a <<   n ;   }   template <typename T, typename... Ts>   void err(vector<string>::iterator it, T a, Ts... as) {     cerr << it->substr((*it)[0] ==    ) <<   =   << a <<  ,  ;     err(++it, as...);   }   struct init {     init() {       cin.tie(0);       std::iostream::sync_with_stdio(0);       cout << std::fixed << std::setprecision(10);       cerr << std::fixed << std::setprecision(10);       srand(228);     }     ~init() {       cerr <<  Time elapsed:   << 1.0 * (double)clock() / CLOCKS_PER_SEC            <<  s. n ;     }   } init;   const int MAXN = 1001, INF = 1e9 + 7;   int ans[MAXN];   vector<int> wo[MAXN];   int ret[20][MAXN];   int main() {     int n;     cin >> n;     for (int step = 1, pw = 0; step < n; step *= 2, ++pw) {       vector<int> q0, q1;       for (int i = 0; i < n; i += step * 2) {         for (int j = 0; j < step && i + j < n; ++j) {           wo[i + j].emplace_back(pw * 2 + 1);           q0.emplace_back(i + j);         }         for (int j = step; j < step + step && i + j < n; ++j) {           q1.emplace_back(i + j);           wo[i + j].emplace_back(pw * 2);         }       }       if (!q0.empty()) {         cout << q0.size() <<   n ;         for (int i : q0) cout << i + 1 <<    ;         cout << std::endl;         for (int i = 0; i < n; ++i) cin >> ret[pw * 2][i];       }       if (!q1.empty()) {         cout << q1.size() <<   n ;         for (int i : q1) cout << i + 1 <<    ;         cout << std::endl;         for (int i = 0; i < n; ++i) cin >> ret[pw * 2 + 1][i];       }     }     cout <<  -1 n ;     for (int i = 0; i < n; ++i) {       int mn = INF;       for (int j : wo[i]) mn = min(mn, ret[j][i]);       cout << mn <<    ;     }     cout << std::endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long vis[80];   long long prime[80] = {0};   long long a[200000];   long long fc[80][200000] = {{0}};   int n;   int pnum;   int get_primes(int m) {     memset(vis, 0, sizeof(vis));     int cnt = 1;     for (int i = 2; i < m; i++) {       if (!vis[i]) {         prime[cnt++] = i;         for (int j = i * i; j < m; j += i) vis[j] = 1;       }     }     return cnt;   }   void change(long long k, long long g) {     for (int i = 1; i <= pnum;) {       if (k % prime[i] == 0) {         fc[i][g] ^= 1;         k /= prime[i];       } else         i++;     }     return;   }   long long gauss() {     long long g = 0;     int i, j;     for (j = 1; j <= n && g < pnum; j++) {       for (i = g + 1; i <= pnum; i++)         if (fc[i][j] == 1) break;       if (i <= pnum) {         g++;         for (int p = j; p <= n; p++) swap(fc[g][p], fc[i][p]);         for (int p = g + 1; p <= pnum; p++) {           if (fc[p][j] == 1) {             for (int q = j; q <= n; q++) fc[p][q] ^= fc[g][q];           }         }       }     }     return g;   }   long long qpow(long long a, long long b, long long m) {     long long ans = 1;     while (b) {       if (b & 1) {         ans = (ans * a) % m;         b--;       }       b /= 2;       a = a * a % m;     }     return ans;   }   int main() {     int cas = 1;     int t = 1;     get_primes(70);     pnum = 19;     {       memset(fc, 0, sizeof(fc));       cin >> n;       for (int i = 1; i <= n; i++) cin >> a[i], change(a[i], i);       int r = gauss();       long long ans = qpow(2, (long long)(n - r), 1000000007) - 1;       printf( %lld n , ans);     }   }
#include <bits/stdc++.h>   using namespace std;   long long MOD = 1e9 + 7;   long long FastPower(long long x, long long y) {     long long res = 1;     x = x % MOD;     while (y > 0) {       if (y & 1) res = (res * x) % MOD;       y = y >> 1;       x = (x * x) % MOD;     }     return res;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n, k;     string s;     cin >> s;     n = s.size();     long long x = 0, y = 0;     y = FastPower(2, n - 1);     for (int i = 0, j = n - 1; i < n; i++, j--) {       if (s[j] ==  1 ) {         x += FastPower(2, i);         x %= MOD;       }     }     cout << (x * y) % MOD;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int b;     map<int, long long> mp;     long long a = -1;     if (n > 1) {       for (int i = 0; i < n; i++) {         cin >> b;         mp[b - i] += b;         a = max(a, mp[b - i]);       }       cout << a;     } else {       cin >> b;       cout << b;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int t;     cin >> t;     while (t--) {       long long int n;       cin >> n;       long long int to = 0, thr = 0;       while (n % 2 == 0) {         to++;         n = n / 2;       }       while (n % 3 == 0) {         thr++;         n = n / 3;       }       if (n > 1) {         cout <<  -1 ;         cout <<   n ;       } else {         if (to > thr) {           cout <<  -1 ;           cout <<   n ;         } else {           cout << 2 * thr - to;           cout <<   n ;         }       }     }   }
#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long x = 0, f = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = (x << 1) + (x << 3) + ch -  0 ;       ch = getchar();     }     return x * f;   }   inline long long fast_pow(long long a, long long b, long long p) {     long long t = 1;     a %= p;     while (b) {       if (b & 1) t = t * a % p;       b >>= 1ll;       a = a * a % p;     }     return t;   }   long long inv[1010101], fac[1010101], n, h[1010101], K;   inline long long C(long long n, long long m) {     if (n < 0 || m < 0 || n < m) return 0;     return fac[n] * inv[m] % 998244353 * inv[n - m] % 998244353;   }   int main() {     n = read(), K = read();     long long ttt = 0;     for (int i = 1; i <= n; i++) {       h[i] = read();     }     for (int i = 1; i <= n; i++) {       if (h[i] != h[(i + 1 == n + 1) ? 1 : (i + 1)]) ttt++;     }     fac[0] = inv[0] = inv[1] = 1;     for (long long i = 2; i <= n; i++)       inv[i] = (998244353 - (998244353 / i) * inv[998244353 % i] % 998244353) %                998244353;     for (int i = 2; i <= n; i++) inv[i] = inv[i - 1] * inv[i] % 998244353;     for (long long i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % 998244353;     long long ans = 0;     long long inv2 = fast_pow(2ll, 998244353 - 2, 998244353);     for (long long i = 0; i <= ttt; i++) {       ans =           (ans +            C(ttt, i) * fast_pow(K - 2 + 998244353, ttt - i, 998244353) %                998244353 *                ((fast_pow(2ll, i, 998244353) - ((i % 2 == 1) ? 0 : C(i, i / 2))) *                     inv2 % 998244353 +                 998244353) %                998244353) %           998244353;     }     cout << ans * fast_pow(K, n - ttt, 998244353) % 998244353;     return 0;   }