VvyLw's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub

:heavy_check_mark: test/modprime2.test.cpp

Depends on

Code

#define PROBLEM "https://yukicoder.me/problems/no/117"
#include <iostream>
#include "C++/math/ModPrime.hpp"
constexpr int mod = 1e9 + 7;
man::ModPrime<(int) 2e7 + 1> mp(mod);
int main() {
    std::cin.tie(nullptr) -> sync_with_stdio(false);
    int t;
    std::cin >> t;
    while(t--) {
        char c, tmp;
        int n, k;
        std::cin >> c >> tmp >> n >> tmp >> k >> tmp;
        std::cout << (c == 'C' ? mp.C(n, k) : c == 'P' ? mp.P(n, k) : mp.H(n, k)) << '\n';
    }
}
#line 1 "test/modprime2.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/117"
#include <iostream>
#line 2 "C++/math/ModPrime.hpp"
#include <array>
#include <algorithm>
#include <ranges>
#ifndef TEMPLATE
namespace man {
template <class T> constexpr inline T sqr(const T x) noexcept { return x * x; }
template <class T> constexpr inline T mod(T x, const T m) noexcept {
    x %= m;
    return x < 0 ? x + m : x;
}
}
#endif
namespace man {
template <int lim> struct ModPrime {
private:
    const long long m;
	std::array<long long, lim> f{}, rf{};
	const int len = std::min(m, (long long) lim);
    constexpr inline long long inv(long long x) noexcept {
        long long ret = 1, k = m - 2;
		while(k > 0) {
			if(k & 1) {
				ret = mod(ret * x, m);
			}
			x = mod(sqr(x), m);
			k >>= 1;
		}
		return ret;
    }
public:
    ModPrime(const long long mod_): m(mod_) {
		f[0] = 1;
		for(const auto i: std::views::iota(1, len)) {
			f[i] = mod(f[i - 1] * i, m);
		}
		rf[len - 1] = inv(f[len - 1]);
		for(const auto i: std::views::iota(1, len) | std::views::reverse) {
			rf[i - 1] = mod(rf[i] * i, m);
		}
    }
    constexpr inline long long C(const int n, const int k) const noexcept {
		if(k < 0 || n < k) {
			return 0;
		}
		const long long a = f[n], b = rf[n - k], c = rf[k], bc = mod(b * c, m);
		return mod(a * bc, m);
	}
	constexpr inline long long P(const int n, const int k) const noexcept {
		if(k < 0 || n < k) {
			return 0;
		}
		const long long a = f[n], b = rf[n - k];
		return mod(a * b, m);
	}
	constexpr inline long long H(const int n, const int k) const noexcept {
		if(n == 0 && k == 0) {
			return 1;
		}
		return C(n + k - 1, k);
	}
};
}
/**
 * @brief ModPrime
 */
#line 4 "test/modprime2.test.cpp"
constexpr int mod = 1e9 + 7;
man::ModPrime<(int) 2e7 + 1> mp(mod);
int main() {
    std::cin.tie(nullptr) -> sync_with_stdio(false);
    int t;
    std::cin >> t;
    while(t--) {
        char c, tmp;
        int n, k;
        std::cin >> c >> tmp >> n >> tmp >> k >> tmp;
        std::cout << (c == 'C' ? mp.C(n, k) : c == 'P' ? mp.P(n, k) : mp.H(n, k)) << '\n';
    }
}
Back to top page