QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#331076#6137. Sub-cycle GraphLainAC ✓175ms4248kbC++236.2kb2024-02-17 23:18:112024-02-17 23:18:11

Judging History

你现在查看的是最新测评结果

  • [2024-02-17 23:18:11]
  • 评测
  • 测评结果:AC
  • 用时:175ms
  • 内存:4248kb
  • [2024-02-17 23:18:11]
  • 提交

answer

#include "bits/stdc++.h"
using namespace std;


// Source: neal
// Modulo arithmetic
template <const int &MOD>
struct _m_int {
  int val;

  _m_int(int64_t v = 0) {
    if (v < 0) v = v % MOD + MOD;
    if (v >= MOD) v %= MOD;
    val = int(v);
  }

  _m_int(uint64_t v) {
    if (v >= MOD) v %= MOD;
    val = int(v);
  }

  _m_int(int v) : _m_int(int64_t(v)) {}
  _m_int(unsigned v) : _m_int(uint64_t(v)) {}

  static int inv_mod(int a, int m = MOD) {
    // https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Example
    int g = m, r = a, x = 0, y = 1;

    while (r != 0) {
      int q = g / r;
      g %= r;
      swap(g, r);
      x -= q * y;
      swap(x, y);
    }

    return x < 0 ? x + m : x;
  }

  explicit operator int() const { return val; }
  explicit operator unsigned() const { return val; }
  explicit operator int64_t() const { return val; }
  explicit operator uint64_t() const { return val; }
  explicit operator double() const { return val; }
  explicit operator long double() const { return val; }

  _m_int &operator+=(const _m_int &other) {
    val -= MOD - other.val;
    if (val < 0) val += MOD;
    return *this;
  }

  _m_int &operator-=(const _m_int &other) {
    val -= other.val;
    if (val < 0) val += MOD;
    return *this;
  }

  static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
#if !defined(_WIN32) || defined(_WIN64)
    return unsigned(x % m);
#endif
    // Optimized mod for Codeforces 32-bit machines.
    // x must be less than 2^32 * m for this to work, so that x / m fits in an
    // unsigned 32-bit int.
    unsigned x_high = unsigned(x >> 32), x_low = unsigned(x);
    unsigned quot, rem;
    asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
    return rem;
  }

  _m_int &operator*=(const _m_int &other) {
    val = fast_mod(uint64_t(val) * other.val);
    return *this;
  }

  _m_int &operator/=(const _m_int &other) { return *this *= other.inv(); }

  friend _m_int operator+(const _m_int &a, const _m_int &b) {
    return _m_int(a) += b;
  }
  friend _m_int operator-(const _m_int &a, const _m_int &b) {
    return _m_int(a) -= b;
  }
  friend _m_int operator*(const _m_int &a, const _m_int &b) {
    return _m_int(a) *= b;
  }
  friend _m_int operator/(const _m_int &a, const _m_int &b) {
    return _m_int(a) /= b;
  }

  _m_int &operator++() {
    val = val == MOD - 1 ? 0 : val + 1;
    return *this;
  }

  _m_int &operator--() {
    val = val == 0 ? MOD - 1 : val - 1;
    return *this;
  }

  _m_int operator++(int) {
    _m_int before = *this;
    ++*this;
    return before;
  }
  _m_int operator--(int) {
    _m_int before = *this;
    --*this;
    return before;
  }

  _m_int operator-() const { return val == 0 ? 0 : MOD - val; }

  friend bool operator==(const _m_int &a, const _m_int &b) {
    return a.val == b.val;
  }
  friend bool operator!=(const _m_int &a, const _m_int &b) {
    return a.val != b.val;
  }
  friend bool operator<(const _m_int &a, const _m_int &b) {
    return a.val < b.val;
  }
  friend bool operator>(const _m_int &a, const _m_int &b) {
    return a.val > b.val;
  }
  friend bool operator<=(const _m_int &a, const _m_int &b) {
    return a.val <= b.val;
  }
  friend bool operator>=(const _m_int &a, const _m_int &b) {
    return a.val >= b.val;
  }

  _m_int inv() const { return inv_mod(val); }

  _m_int pow(int64_t p) const {
    if (p < 0) return inv().pow(-p);

    _m_int a = *this, result = 1;

    while (p > 0) {
      if (p & 1) result *= a;

      p >>= 1;

      if (p > 0) a *= a;
    }

    return result;
  }

  friend ostream &operator<<(ostream &os, const _m_int &m) {
    return os << m.val;
  }

  friend istream &operator>>(istream &is, _m_int &m) {
    int64_t x;
    is >> x;
    m = _m_int(x);
    return is;
  }
};

const int MOD = 1e9+7;
using mod_int = _m_int<MOD>;


// Source: Based on neal's choose implementation
// Tested on: ABC 235 G
namespace internal_choose {
std::vector<mod_int> factorial = {1}, inv_factorial = {1};
int built_max = 0;

void prepare_factorials(int max) {
  if (max <= built_max) return;
  max += max / 100;
  factorial.resize(max + 1);
  inv_factorial.resize(max + 1);
  for (int i = built_max + 1; i <= max; i++)
    factorial[i] = i * factorial[i - 1];
  inv_factorial[max] = factorial[max].inv();
  for (int i = max - 1; i > built_max; i--)
    inv_factorial[i] = inv_factorial[i + 1] * (i + 1);
  built_max = max;
}
};  // namespace internal_choose

mod_int factorial(int n) {
  if (n < 0) return 0;
  internal_choose::prepare_factorials(n);
  return internal_choose::factorial[n];
}

mod_int inv_factorial(int n) {
  if (n < 0) return 0;
  internal_choose::prepare_factorials(n);
  return internal_choose::inv_factorial[n];
}

mod_int choose(int n, int r) {
  if (r < 0 || r > n) return 0;
  internal_choose::prepare_factorials(n);
  return internal_choose::factorial[n] * internal_choose::inv_factorial[n - r] *
         internal_choose::inv_factorial[r];
}

mod_int permute(int n, int r) {
  if (r < 0 || r > n) return 0;
  internal_choose::prepare_factorials(n);
  return internal_choose::factorial[n] * internal_choose::inv_factorial[n - r];
}

mod_int inv_choose(int n, int r) {
  assert(r >= 0 && r <= n);
  internal_choose::prepare_factorials(n);
  return internal_choose::inv_factorial[n] * internal_choose::factorial[n - r] *
         internal_choose::factorial[r];
}

mod_int inv_permute(int n, int r) {
  assert(r >= 0 && r <= n);
  internal_choose::prepare_factorials(n);
  return internal_choose::inv_factorial[n] * internal_choose::factorial[n - r];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int tt;
  cin >> tt;
  while(tt--) {
    int n, m;
    cin >> n >> m;
    if (m > n) {
      cout << 0 << '\n';
      continue;
    }
    if (m == 0) {
      cout << 1 << '\n';
      continue;
    }
    if (n == m) {
      cout << factorial(n-1)/2 << '\n';
      continue;
    }
    mod_int ans = 0, prod = 1;
    for (int i = 1; i <= n-m; i++) {
      prod *= 2*i-1;
      ans += choose(n, n-m-i) * factorial(m-i) * choose(m-1, i-1) * choose(m+i, 2*i) * prod;
    }
    cout << ans << '\n';
  }
}


詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3612kb

input:

3
4 2
4 3
5 3

output:

15
12
90

result:

ok 3 number(s): "15 12 90"

Test #2:

score: 0
Accepted
time: 175ms
memory: 4248kb

input:

17446
3 0
3 1
3 2
3 3
4 0
4 1
4 2
4 3
4 4
5 0
5 1
5 2
5 3
5 4
5 5
6 0
6 1
6 2
6 3
6 4
6 5
6 6
7 0
7 1
7 2
7 3
7 4
7 5
7 6
7 7
8 0
8 1
8 2
8 3
8 4
8 5
8 6
8 7
8 8
9 0
9 1
9 2
9 3
9 4
9 5
9 6
9 7
9 8
9 9
10 0
10 1
10 2
10 3
10 4
10 5
10 6
10 7
10 8
10 9
10 10
11 0
11 1
11 2
11 3
11 4
11 5
11 6
11 7
11...

output:

1
3
3
1
1
6
15
12
3
1
10
45
90
60
12
1
15
105
375
630
360
60
1
21
210
1155
3465
5040
2520
360
1
28
378
2940
13545
35280
45360
20160
2520
1
36
630
6552
42525
170100
393120
453600
181440
20160
1
45
990
13230
114345
643545
2286900
4762800
4989600
1814400
181440
1
55
1485
24750
273735
2047815
10239075
3...

result:

ok 17446 numbers