Submission #7360840


Source Code Expand

Copy
#define DEBUG 0
/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 9/6/2019, 3:42:36 PM
 * Powered by Visual Studio Code
 */
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
using ll = long long;
class mint
{
public:
  static ll MOD;
  ll x;
  mint() : x(0) {}
  mint(ll x) : x(x % MOD) {}
  mint operator-() const { return x ? MOD - x : 0; }
  mint &operator+=(const mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  mint &operator-=(const mint &a) { return *this += -a; }
  mint &operator*=(const mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint &operator/=(const mint &a)
  {
    mint b{a};
    return *this *= b.power(MOD - 2);
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
  const mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      mint half = power(N / 2);
      return half * half;
    }
  }
};
ll mint::MOD = 1e9 + 7;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
class combination
{
public:
  vector<mint> inv, fact, factinv;
  static int MAX_SIZE;
  combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1; i < MAX_SIZE; i++)
    {
      fact[i] = mint(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  mint operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
};
int combination::MAX_SIZE = 3000010;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// constexpr double epsilon = 1e-10;
// constexpr ll infty = 1000000000000000LL;
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}

combination C{};

mint solve(ll N, ll K, ll R, ll x)
{
  mint ans{};
  for (auto l = K; l <= N; l++)
  {
    if (R - l * x + N < 0)
    {
      break;
    }
    if ((l + K) % 2 == 0)
    {
      ans += C(l, K) * C(N, l) * C(R - l * x + N, N);
    }
    else
    {
      ans -= C(l, K) * C(N, l) * C(R - l * x + N, N);
    }
  }
  return ans;
}

mint f(ll N, ll K, ll R)
{
  mint ans{C(R + N, N)};
  for (auto x = 0; x <= R; x++)
  {
    ans -= (solve(N, K, R, x) - solve(N, K, R - K, x));
  }
  return ans;
}

int main()
{
  ll N, K, L, R;
  cin >> N >> K >> L >> R;
  cout << f(N, K, R) - f(N, K, L - 1) << endl;
}

Submission Info

Submission Time
Task F - Candy Retribution
User kazunetakahashi
Language C++14 (GCC 5.4.1)
Score 0
Code Size 3702 Byte
Status

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 sample_01.txt, sample_02.txt, sample_03.txt
Subtask1 0 / 1000 sample_01.txt, sample_02.txt, sample_03.txt, sub1_01.txt, sub1_02.txt, sub1_03.txt, sub1_04.txt, sub1_05.txt, sub1_06.txt, sub1_07.txt, sub1_08.txt, sub1_09.txt, sub1_10.txt, sub1_11.txt, sub1_12.txt, sub1_13.txt, sub1_14.txt, sub1_15.txt, sub1_16.txt, sub1_17.txt, sub1_18.txt, sub1_19.txt, sub1_20.txt, sub1_21.txt, sub1_22.txt, sub1_23.txt, sub1_24.txt, sub1_25.txt, sub1_26.txt, sub1_27.txt, sub1_28.txt, sub1_29.txt, sub1_30.txt, sub1_31.txt, sub1_32.txt, sub1_33.txt, sub1_34.txt, sub1_35.txt, sub1_36.txt, sub1_37.txt
Case Name Status Exec Time Memory
sample_01.txt 334 ms 70528 KB
sample_02.txt 320 ms 70528 KB
sample_03.txt 694 ms 70528 KB
sub1_01.txt 1905 ms 70528 KB
sub1_02.txt 360 ms 70528 KB
sub1_03.txt 772 ms 70528 KB
sub1_04.txt 336 ms 70528 KB
sub1_05.txt 783 ms 70528 KB
sub1_06.txt 945 ms 70528 KB
sub1_07.txt 984 ms 70528 KB
sub1_08.txt 1712 ms 70528 KB
sub1_09.txt
sub1_10.txt
sub1_11.txt
sub1_12.txt
sub1_13.txt 388 ms 70528 KB
sub1_14.txt 622 ms 70528 KB
sub1_15.txt
sub1_16.txt 1531 ms 70528 KB
sub1_17.txt 1333 ms 70528 KB
sub1_18.txt 338 ms 70528 KB
sub1_19.txt 325 ms 70528 KB
sub1_20.txt 326 ms 70528 KB
sub1_21.txt 330 ms 70528 KB
sub1_22.txt 329 ms 70528 KB
sub1_23.txt 323 ms 70528 KB
sub1_24.txt 325 ms 70528 KB
sub1_25.txt 323 ms 70528 KB
sub1_26.txt
sub1_27.txt 335 ms 70528 KB
sub1_28.txt
sub1_29.txt 753 ms 70528 KB
sub1_30.txt 878 ms 70528 KB
sub1_31.txt 360 ms 70528 KB
sub1_32.txt 376 ms 70528 KB
sub1_33.txt 505 ms 70528 KB
sub1_34.txt 412 ms 70528 KB
sub1_35.txt 386 ms 70528 KB
sub1_36.txt 446 ms 70528 KB
sub1_37.txt 1767 ms 70528 KB