結果

問題 No.2382 Amidakuji M
ユーザー KoseTKoseT
提出日時 2023-07-14 22:49:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,807 bytes
コンパイル時間 2,326 ms
コンパイル使用メモリ 202,420 KB
実行使用メモリ 4,652 KB
最終ジャッジ日時 2023-10-14 13:08:55
合計ジャッジ時間 3,489 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 2 ms
4,368 KB
testcase_02 AC 1 ms
4,372 KB
testcase_03 AC 12 ms
4,372 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 13 ms
4,372 KB
testcase_07 AC 8 ms
4,372 KB
testcase_08 AC 2 ms
4,372 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 18 ms
4,368 KB
testcase_13 AC 1 ms
4,372 KB
testcase_14 AC 2 ms
4,372 KB
testcase_15 AC 1 ms
4,368 KB
testcase_16 AC 2 ms
4,372 KB
testcase_17 AC 2 ms
4,372 KB
testcase_18 WA -
testcase_19 AC 28 ms
4,580 KB
testcase_20 AC 28 ms
4,572 KB
testcase_21 AC 28 ms
4,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
 
using namespace std;
#ifndef ATCODER_FENWICKTREE_HPP
#define ATCODER_FENWICKTREE_HPP 1

#include <cassert>
#include <vector>

namespace atcoder {

// Reference: https://en.wikipedia.org/wiki/Fenwick_tree
template <class T> struct fenwick_tree {
    using U = unsigned;//internal::to_unsigned_t<T>;

  public:
    fenwick_tree() : _n(0) {}
    explicit fenwick_tree(int n) : _n(n), data(n) {}

    void add(int p, T x) {
        assert(0 <= p && p < _n);
        p++;
        while (p <= _n) {
            data[p - 1] += U(x);
            p += p & -p;
        }
    }

    T sum(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        return sum(r) - sum(l);
    }

  private:
    int _n;
    std::vector<U> data;

    U sum(int r) {
        U s = 0;
        while (r > 0) {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};

}  // namespace atcoder

#endif  // ATCODER_FENWICKTREE_HPP

long long gcd(long long a, long long b) {
  if (a % b == 0) return b;
  return gcd(b, a%b);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  long long m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i=0;i<n;++i) cin >> a[i];
  long long k {};
  {
    atcoder::fenwick_tree<int> fenwick_(n);
    for (int i=0;i<n;++i) {
      k += fenwick_.sum(a[i], n);
      fenwick_.add(a[i]-1, 1);
    }
  }
  // (k + 2* l) (l >= 0)
  if (k==0) {
    cout << k << '\n';
  } else if (k <= m && (m-k) % 2 == 0) {
    cout << m << '\n';
  } else if (m <= k) {
    long long g = gcd(m, k);
    // m*c >= k となる最小のp
    //  c >= (k/m)
    long long c = (k+m-1) / m;
    long long d = m * c;
    if ((d-k) % 2 == 0) {
      cout << d << '\n';
    } else {
      cout << "-1\n";
    }
  } else {
    cout << "-1\n";
  }
}
0