結果
問題 | No.2382 Amidakuji M |
ユーザー |
![]() |
提出日時 | 2023-07-14 22:03:42 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 71 ms / 2,000 ms |
コード長 | 1,592 bytes |
コンパイル時間 | 1,557 ms |
コンパイル使用メモリ | 171,384 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-09-16 07:04:57 |
合計ジャッジ時間 | 2,965 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
#include <bits/stdc++.h>using namespace std;using ll = long long;#define MOD (998244353)class BIT{int n;vector<int> a;public:BIT(int n_) : n(n_){a.resize(n + 1, 0);}void add(int i, int x){while (i <= n){a[i] += x;i += i & (-i);}}int sum(int i){int ret = 0;while (i > 0){ret += a[i];i -= i & (-i);}return ret;}int sum(int l, int r){return sum(r) - sum(l - 1);}};ll modpow(ll a, ll x){ll ret = 1;a %= MOD;while (x > 0){if (x % 2 == 1){ret = ret * a % MOD;}a = a * a % MOD;x >>= 1;}return ret;}int main(){int N;ll M;cin >> N >> M;vector<int> P(N);BIT tree(N);ll sum = 0;for (int i = 0; i < N; i++){cin >> P[i];sum += (ll)tree.sum(P[i] + 1, N);tree.add(P[i], 1);}if (sum == 0){cout << 0 << endl;return 0;}if (sum % 2 == 0){ll ans = ((sum - 1) / M + 1) * M;if (ans % 2 == 1){ans += M;}cout << ans << endl;}else{if (M % 2 == 0){cout << -1 << endl;}else{ll ans = ((sum - 1) / M + 1) * M;if (ans % 2 == 0){ans += M;}cout << ans << endl;}}}