結果

問題 No.129 お年玉(2)
ユーザー not_522not_522
提出日時 2015-07-28 06:34:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 366 ms / 5,000 ms
コード長 4,558 bytes
コンパイル時間 1,436 ms
コンパイル使用メモリ 153,720 KB
実行使用メモリ 18,868 KB
最終ジャッジ日時 2023-08-18 17:25:07
合計ジャッジ時間 10,065 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
18,632 KB
testcase_01 AC 61 ms
18,664 KB
testcase_02 AC 62 ms
18,664 KB
testcase_03 AC 61 ms
18,716 KB
testcase_04 AC 59 ms
18,464 KB
testcase_05 AC 84 ms
18,660 KB
testcase_06 AC 253 ms
18,536 KB
testcase_07 AC 248 ms
18,516 KB
testcase_08 AC 79 ms
18,468 KB
testcase_09 AC 221 ms
18,856 KB
testcase_10 AC 199 ms
18,664 KB
testcase_11 AC 220 ms
18,680 KB
testcase_12 AC 70 ms
18,804 KB
testcase_13 AC 69 ms
18,868 KB
testcase_14 AC 253 ms
18,580 KB
testcase_15 AC 230 ms
18,688 KB
testcase_16 AC 293 ms
18,716 KB
testcase_17 AC 171 ms
18,860 KB
testcase_18 AC 204 ms
18,612 KB
testcase_19 AC 89 ms
18,568 KB
testcase_20 AC 332 ms
18,668 KB
testcase_21 AC 202 ms
18,540 KB
testcase_22 AC 194 ms
18,680 KB
testcase_23 AC 231 ms
18,668 KB
testcase_24 AC 75 ms
18,808 KB
testcase_25 AC 253 ms
18,536 KB
testcase_26 AC 136 ms
18,856 KB
testcase_27 AC 142 ms
18,852 KB
testcase_28 AC 366 ms
18,668 KB
testcase_29 AC 63 ms
18,664 KB
testcase_30 AC 63 ms
18,688 KB
testcase_31 AC 63 ms
18,848 KB
testcase_32 AC 69 ms
18,532 KB
testcase_33 AC 71 ms
18,796 KB
testcase_34 AC 67 ms
18,792 KB
testcase_35 AC 73 ms
18,764 KB
testcase_36 AC 68 ms
18,740 KB
testcase_37 AC 67 ms
18,672 KB
testcase_38 AC 100 ms
18,848 KB
testcase_39 AC 93 ms
18,584 KB
testcase_40 AC 142 ms
18,556 KB
testcase_41 AC 142 ms
18,720 KB
testcase_42 AC 96 ms
18,532 KB
testcase_43 AC 75 ms
18,660 KB
testcase_44 AC 113 ms
18,672 KB
testcase_45 AC 78 ms
18,792 KB
testcase_46 AC 71 ms
18,728 KB
testcase_47 AC 185 ms
18,856 KB
testcase_48 AC 86 ms
18,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

template<typename T> inline T gcd(T a, T b) {
  return __gcd(a, b);
}

template<typename T> inline T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}

template<typename T> inline T floor(T a, T b) {
  return a / b * b <= a ? a / b : a / b - 1;
}

template<typename T> inline T ceil(T a, T b) {
  return floor(a + b - 1, b);
}

template<typename T> inline T round(T a, T b) {
  return floor(a + b / 2);
}

template<typename T> inline T mod(T a, T b) {
  return a - floor(a, b) * b;
}

template<typename T> class Combination {
private:
  vector<vector<T>> comb;
  
public:
  Combination(int n = 0) : comb(n, vector<T>(n, 0)) {
    for (int i = 0; i < n; ++i) comb[i][0] = 1;
    for (int i = 1; i < n; ++i) {
      for (int j = 1; j < n; ++j) {
        comb[i][j] = comb[i - 1][j] + comb[i - 1][j - 1];
      }
    }
  }

  T combination(int n, int m) {
    if (n < m) return 0;
    if (n < (int)comb.size()) return comb[n][m];
    T res = 1;
    for (int i = 0; i < min(m, n - m); ++i) res = res * (n - i) / (i + 1);
    return res;
  }

  T combination_safety(int n, int m) {
    if (n < m) return 0;
    if (n < (int)comb.size()) return comb[n][m];
    m = min(m, n - m);
    vector<int> a(m), b(m);
    iota(a.begin(), a.end(), n - m + 1);
    iota(b.begin(), b.end(), 1);
    for (auto i : b) {
      for (auto& j : a) {
        auto g = gcd(i, j);
        i /= g;
        j /= g;
        if (i == 1) break;
      }
    }
    T res = 1;
    for (auto i : a) res = res * i;
    return res;
  }

  T repetition(int n, int r) {
    return combination(n + r - 1, r);
  }
};

namespace arithmetic {
  template<typename T> class Addition {
  public:
    template<typename V> T operator+(const V& v) const {
      return T(static_cast<const T&>(*this)) += v;
    }
  };

  template<typename T> class Subtraction {
  public:
    template<typename V> T operator-(const V& v) const {
      return T(static_cast<const T&>(*this)) -= v;
    }
  };

  template<typename T> class Multiplication {
  public:
    template<typename V> T operator*(const V& v) const {
      return T(static_cast<const T&>(*this)) *= v;
    }
  };

  template<typename T> class Division {
  public:
    template<typename V> T operator/(const V& v) const {
      return T(static_cast<const T&>(*this)) /= v;
    }
  };

  template<typename T> class Modulus {
  public:
    template<typename V> T operator%(const V& v) const {
      return T(static_cast<const T&>(*this)) %= v;
    }
  };
}

template<typename T> class IndivisibleArithmetic : public arithmetic::Addition<T>, public arithmetic::Subtraction<T>, public arithmetic::Multiplication<T> {};

template<typename T> class Arithmetic : public IndivisibleArithmetic<T>, public arithmetic::Division<T> {};

class Inverse {
private:
  long long mod;
	vector<long long> inv;
  
public:
  Inverse() {}
  
	Inverse(long long mod, long long n = 1000000) : mod(mod) {
    inv = vector<long long>(n, 1);
    for (int i = 2; i < n; ++i) inv[i] = inv[mod % i] * (mod - mod / i) % mod;
  }
  
	long long operator()(long long a) const {
		if (a < (int)inv.size()) return inv[a];
		long long b = mod, x = 1, y = 0;
		while (b) {
			long long t = a / b;
			swap(a -= t * b, b);
			swap(x -= t * y, y);
		}
		return (x %= mod) < 0 ? x + mod : x;
	}
};

class Mint : public Arithmetic<Mint> {
private:
  static long long mod;
  static Inverse inverse;
  long long val;
	
public:
	Mint() {}

	Mint(const long long& val) {
    this->val = val % mod;
    if (this->val < 0) this->val += mod;
  }

  static void setMod(const long long& m) {
    mod = m;
    inverse = Inverse(m);
  }
	
	Mint operator+=(const Mint& m) {
		val += m.val;
		if (val >= mod) val -= mod;
		return *this;
	}
  
	Mint operator-=(const Mint& m) {
		val -= m.val;
		if (val < 0) val += mod;
		return *this;
	}
  
	Mint operator*=(const Mint& m) {
		val *= m.val;
		val %= mod;
		return *this;
	}
  
	Mint operator/=(const Mint& m) {
		val *= inverse(m.val);
		val %= mod;
		return *this;
	}
	
	Mint operator++() {return val += 1;}
	
	operator long long() {
		return val;
	}

  Mint identity() const {
    return 1;
  }
};

long long Mint::mod = 1000000007;
Inverse Mint::inverse(1000000007);

ostream& operator<<(ostream& os, Mint a) {
	os << (long long)a;
	return os;
}

istream& operator>>(istream& is, Mint& a) {
	long long n;
	is >> n;
	a = n;
	return is;
}

int main() {
  long long n, m;
  cin >> n >> m;
  n /= 1000;
  n %= m;
  Mint::setMod(1000000000);
  Combination<Mint> comb;
  cout << comb.combination_safety(m, n) << endl;
}
0