結果

問題 No.2362 Inversion Number of Mod of Linear
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-06-23 21:45:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 127 ms / 2,000 ms
コード長 4,774 bytes
コンパイル時間 1,026 ms
コンパイル使用メモリ 104,388 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-13 16:45:05
合計ジャッジ時間 2,651 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 82 ms
4,376 KB
testcase_04 AC 28 ms
4,376 KB
testcase_05 AC 10 ms
4,380 KB
testcase_06 AC 127 ms
4,376 KB
testcase_07 AC 55 ms
4,380 KB
testcase_08 AC 68 ms
4,376 KB
testcase_09 AC 68 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

// using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }

#ifndef LIBRA_OTHER_INT128_H_
#define LIBRA_OTHER_INT128_H_

#include <stdio.h>
#include <iostream>

constexpr unsigned __int128 toUInt128(const char *s) {
  unsigned __int128 x = 0;
  for (; *s; ++s) x = x * 10 + (*s - '0');
  return x;
}
constexpr __int128 toInt128(const char *s) {
  if (*s == '-') return -toInt128(s + 1);
  __int128 x = 0;
  for (; *s; ++s) x = x * 10 + (*s - '0');
  return x;
}
unsigned __int128 inUInt128() {
  static char buf[41];
  scanf("%s", buf);
  return toUInt128(buf);
}
__int128 inInt128() {
  static char buf[41];
  scanf("%s", buf);
  return toInt128(buf);
}

void out(unsigned __int128 x) {
  static char buf[41];
  int len = 0;
  do { buf[len++] = '0' + static_cast<int>(x % 10); } while (x /= 10);
  for (int i = len; --i >= 0; ) putchar(buf[i]);
}
void out(__int128 x) {
  if (x < 0) {
    putchar('-');
    out(-static_cast<unsigned __int128>(x));
  } else {
    out(static_cast<unsigned __int128>(x));
  }
}
std::ostream &operator<<(std::ostream &os, unsigned __int128 x) {
  static char buf[41];
  int len = 0;
  do { buf[len++] = '0' + static_cast<int>(x % 10); } while (x /= 10);
  for (int i = len; --i >= 0; ) os << buf[i];
  return os;
}
std::ostream &operator<<(std::ostream &os, __int128 x) {
  if (x < 0) {
    os << '-' << -static_cast<unsigned __int128>(x);
  } else {
    os << static_cast<unsigned __int128>(x);
  }
  return os;
}

#endif  // LIBRA_OTHER_INT128_H_


// y^f(0) x y^(f(1)-f(0)) x y^(f(2)-f(1)) x ... x y^(f(n)-f(n-1))
//   where f(i) = floor((a i + b) / m)
// S: (unsigned or signed) integer
//   (a n + b) and (m + a) does not overflow
// T: monoid with pow
//   e: identity
template <class S, class T> T pathUnder(S m, S a, S b, S n, T e, T x, T y) {
  assert(m >= 1); assert(a >= 0); assert(b >= 0); assert(n >= 0);
  S c = (a * n + b) / m;
  T pre = e, suf = e;
  for (; ; ) {
    const S p = a / m; a %= m; x = x * y.pow(p);
    const S q = b / m; b %= m; pre = pre * y.pow(q);
    c -= (p * n + q);
    if (c == 0) return pre * x.pow(n) * suf;
    const S d = (m * c - b - 1) / a + 1;
    suf = y * x.pow(n - d) * suf;
    b = m - b - 1 + a; swap(m, a); n = c - 1; c = d; swap(x, y);
  }
}


using Int = __int128;

struct Data {
  Int val, sz, sum0, sum1;
  Data() : val(0), sz(0), sum0(0), sum1(0) {}
  friend Data operator*(const Data &a, const Data &b) {
    Data c;
    c.val = a.val + b.val;
    c.sz = a.sz + b.sz;
    c.sum0 = a.sum0 + b.sz * a.val + b.sum0;
    c.sum1 = a.sum1 + b.sz * a.sz * a.val + (b.sz * (b.sz - 1) / 2) * a.val + a.sz * b.sum0 + b.sum1;
    return c;
  }
  Data pow(Int e) const {
    for (Data a = *this, b; ; a = a * a) {
      if (e & 1) b = b * a;
      if (!(e >>= 1)) return b;
    }
  }
};

// \sum[0<=i<n] i^k ((ai+b) mod m)
Data solve(Int n, Int m, Int a, Int b) {
  Data ini, x, y;
  ini.val = b;
  x.val = a;
  x.sz = 1;
  y.val = -m;
  const Data res = pathUnder(m, a, b, n, Data(), x, y);
  return ini * res;
}

int main() {
  for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
    const Int N = inInt128();
    const Int M = inInt128();
    const Int X = inInt128();
    const Int Y = inInt128();
    
    Int ans = 0;
    
    // 4, 2, 0, -2, 4
    {
      const Data res = solve(N, M, X, Y);
      ans += (N - 1) * res.sum0;
      ans -= 2 * res.sum1;
    }
    
    // 4, 3, 2, 1, 0
    {
      const Data res = solve(N, M, X, X);
      ans += (N - 1) * res.sum0;
      ans -= res.sum1;
    }
    
    assert(ans % M == 0);
    ans /= M;
    out(ans);
    puts("");
  }
#ifndef LOCAL
  break;
#endif
  }
  return 0;
}
0