CF1957E Carousel of Combinations

· · 个人记录

https://codeforces.com/contest/1957/problem/E

首先观察到 C(i, j) = \dfrac{\prod\limits_{k = i - j + 1}^{i}k}{j}。

考虑推 C(i, j) \bmod j:

C(i, j) \bmod j = \dfrac{\prod\limits_{k = i - j + 1}^{i}k}{j} \bmod j

考虑到 i - j + 1 \sim i 之间一定有一个 j 的倍数,这个数是 j\left\lfloor\dfrac{i}{j}\right\rfloor,我们只需要把它拎出来,剩下的 \bmod j 就是 1 \sim j - 1,乘起来就是 (j -1)!,整理有:

C(i, j) \bmod j = (j - 1)! \left\lfloor\dfrac{i}{j}\right\rfloor \bmod j

根据威尔逊定理:

(p - 1)! \bmod p = \begin{cases}2, p = 4\\-1, p \in \mathbb{P}\\0, \text{others}\end{cases}

然后我们就可以根据 \left\lfloor\dfrac{i}{j}\right\rfloor 的值暴力预处理答案数组了,考虑差分即可,时间复杂度就是 O(10^6 \log \log 10^6)。

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/rope>

bool Mbe;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128;

std :: mt19937 rnd (std :: chrono :: steady_clock :: now ().time_since_epoch ().count ());
template < typename T > T gen (const T l, const T r) {
  return rnd () % (r - l + 1) + l;
}
const double pi = std :: acos (-1);

template < typename T > inline void read (T &val) {
  T x = 0;
  bool f = false;
  char c = std :: getchar ();
  for ( ; c < '0' || c > '9' ; c = std :: getchar ()) {
    f |= (c == '-');
  }
  for ( ; c >= '0' && c <= '9' ; c = std :: getchar ()) {
    x = (x << 1) + (x << 3) + (c & 15);
  }
  val = (!f) ? (x) : (-x);
  return ;
}

#define debug(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
const i64 hashmod1 = 2147483579, hashmod2 = 1610612741, hashmod3 = 805306457;

template < typename T, typename I > void chkmin (T &p, I q) {
  T q_ = q; 
  p = std :: min (p, q_); 
  return ;
}
template < typename T, typename I > void chkmax (T &p, I q) {
  T q_ = q; 
  p = std :: max (p, q_); 
  return ;
}

const int N = static_cast < int > (1e6) + 25;
const i64 mod = static_cast < i64 > (1e9) + 7;

bool isprime[N];
std :: vector < i64 > need;

i64 ans[N];

i64 norm (i64 x) {
  if (x >= mod) {
    return x % mod;
  }
  else if (x == 0) {
    return 0;
  }
  else {
    return (mod - (-x) % mod) % mod;
  }
}

void init () {
  for (auto p : need) {
    if (p == 4) {
      for (i64 i = 1;i <= static_cast < i64 > (1e6); ++ i) {
        ans[i] += (i / p) * 2 % p;
        ans[i + 1] -= (i / p) * 2 % p;
      }
    }
    else {
      for (i64 i = 0;i <= static_cast < i64 > (1e6); i += p) {
        i64 L = i == 0 ? 1 : i;
        i64 R = std :: min (i + p - 1, static_cast < i64 > (1e6));
        i64 v = (L / p) * (p - 1) % p;
        ans[L] += v;
        ans[R + 1] -= v;
      }
    }
  }
  for (int i = 1;i <= static_cast < int > (1e6); ++ i) {
    ans[i] += ans[i - 1];
  }
  for (int i = 1;i <= static_cast < int > (1e6); ++ i) {
    ans[i] = norm (ans[i]);
    ans[i] += ans[i - 1];
    ans[i] %= mod;
  }
  return ;
}

bool Med;
signed main () {
  debug ("%.8lf MB\n", (&Mbe - &Med) / 1048576.0);

  for (int i = 2;i <= static_cast < int > (1e6); ++ i) isprime[i] = true;
  for (int i = 2;i <= static_cast < int > (1e6); ++ i) {
    if (isprime[i]) {
      for (int j = i + i;j <= static_cast < int > (1e6); j += i) {
        isprime[j] = false;
      }
    }
  }

  need.clear ();
  for (int i = 2;i <= static_cast < int > (1e6); ++ i) {
    if (isprime[i] || i == 4) {
      need.emplace_back (static_cast < i64 > (i));
    }
  }
  init ();

  int _;
  read (_);
  while (_ --) {
    int n;
    read (n);
    printf ("%lld\n", ans[n] % mod);
  }

  debug ("%.8lf ms\n", 1e3 * clock () / CLOCKS_PER_SEC);
  return 0;
}
// g++ "CF1957E. Carousel of Combinations.cpp" -o CF1957E -std=c++14 -O2 -Wall -Wextra -Wshadow
// check! no inline! (except fastIO)