結果
問題 | No.125 悪の花弁 |
ユーザー |
![]() |
提出日時 | 2024-02-08 21:39:47 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 41 ms / 5,000 ms |
コード長 | 15,880 bytes |
コンパイル時間 | 2,979 ms |
コンパイル使用メモリ | 215,564 KB |
最終ジャッジ日時 | 2025-02-19 02:54:02 |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 6 |
ソースコード
// #define _GLIBCXX_DEBUG#include <bits/stdc++.h>// clang-format offstd::ostream&operator<<(std::ostream&os,std::int8_t x){return os<<(int)x;}std::ostream&operator<<(std::ostream&os,std::uint8_t x){return os<<(int)x;}std::ostream&operator<<(std::ostream&os,const __int128_t &v){if(!v)os<<"0";__int128_t tmp=v<0?(os<<"-",-v):v;std::string s;while(tmp)s+='0'+(tmp%10),tmp/=10;return std::reverse(s.begin(),s.end()),os<<s;}std::ostream&operator<<(std::ostream&os,const __uint128_t &v){if(!v)os<<"0";__uint128_t tmp=v;std::string s;while(tmp)s+='0'+(tmp%10),tmp/=10;returnstd::reverse(s.begin(),s.end()),os<<s;}#define checkpoint() (void(0))#define debug(...) (void(0))#define debugArray(x,n) (void(0))#define debugMatrix(x,h,w) (void(0))// clang-format on#include <type_traits>template <class Int> constexpr inline Int mod_inv(Int a, Int mod) {static_assert(std::is_signed_v<Int>);Int x= 1, y= 0, b= mod;for (Int q= 0, z= 0; b;) z= x, x= y, y= z - y * (q= a / b), z= a, a= b, b= z - b * q;return assert(a == 1), x < 0 ? mod - (-x) % mod : x % mod;}namespace math_internal {using namespace std;using u8= unsigned char;using u32= unsigned;using i64= long long;using u64= unsigned long long;using u128= __uint128_t;#define CE constexpr#define IL inline#define NORM \if (n >= mod) n-= mod; \return n#define PLUS(U, M) \CE IL U plus(U l, U r) const { return l+= r, l < (M) ? l : l - (M); }#define DIFF(U, C, M) \CE IL U diff(U l, U r) const { return l-= r, l >> C ? l + (M) : l; }#define SGN(U) \static CE IL U set(U n) { return n; } \static CE IL U get(U n) { return n; } \static CE IL U norm(U n) { return n; }template <class u_t, class du_t, u8 B, u8 A> struct MP_Mo {u_t mod;CE MP_Mo(): mod(0), iv(0), r2(0) {}CE MP_Mo(u_t m): mod(m), iv(inv(m)), r2(-du_t(mod) % mod) {}CE IL u_t mul(u_t l, u_t r) const { return reduce(du_t(l) * r); }PLUS(u_t, mod << 1)DIFF(u_t, A, mod << 1)CE IL u_t set(u_t n) const { return mul(n, r2); }CE IL u_t get(u_t n) const {n= reduce(n);NORM;}CE IL u_t norm(u_t n) const { NORM; }private:u_t iv, r2;static CE u_t inv(u_t n, int e= 6, u_t x= 1) { return e ? inv(n, e - 1, x * (2 - x * n)) : x; }CE IL u_t reduce(const du_t &w) const { return u_t(w >> B) + mod - ((du_t(u_t(w) * iv) * mod) >> B); }};struct MP_Na {u32 mod;CE MP_Na(): mod(0){};CE MP_Na(u32 m): mod(m) {}CE IL u32 mul(u32 l, u32 r) const { return u64(l) * r % mod; }PLUS(u32, mod) DIFF(u32, 31, mod) SGN(u32)};struct MP_Br { // mod < 2^31u32 mod;CE MP_Br(): mod(0), s(0), x(0) {}CE MP_Br(u32 m): mod(m), s(95 - __builtin_clz(m - 1)), x(((u128(1) << s) + m - 1) / m) {}CE IL u32 mul(u32 l, u32 r) const { return rem(u64(l) * r); }PLUS(u32, mod) DIFF(u32, 31, mod) SGN(u32) private: u8 s;u64 x;CE IL u64 quo(u64 n) const { return (u128(x) * n) >> s; }CE IL u32 rem(u64 n) const { return n - quo(n) * mod; }};struct MP_Br2 { // 2^20 < mod <= 2^41u64 mod;CE MP_Br2(): mod(0), x(0) {}CE MP_Br2(u64 m): mod(m), x((u128(1) << 84) / m) {}CE IL u64 mul(u64 l, u64 r) const { return rem(u128(l) * r); }PLUS(u64, mod << 1)DIFF(u64, 63, mod << 1)static CE IL u64 set(u64 n) { return n; }CE IL u64 get(u64 n) const { NORM; }CE IL u64 norm(u64 n) const { NORM; }private:u64 x;CE IL u128 quo(const u128 &n) const { return (n * x) >> 84; }CE IL u64 rem(const u128 &n) const { return n - quo(n) * mod; }};struct MP_D2B1 {u8 s;u64 mod, d, v;CE MP_D2B1(): s(0), mod(0), d(0), v(0) {}CE MP_D2B1(u64 m): s(__builtin_clzll(m)), mod(m), d(m << s), v(u128(-1) / d) {}CE IL u64 mul(u64 l, u64 r) const { return rem((u128(l) * r) << s) >> s; }PLUS(u64, mod) DIFF(u64, 63, mod) SGN(u64) private: CE IL u64 rem(const u128 &u) const {u128 q= (u >> 64) * v + u;u64 r= u64(u) - (q >> 64) * d - d;if (r > u64(q)) r+= d;if (r >= d) r-= d;return r;}};template <class u_t, class MP> CE u_t pow(u_t x, u64 k, const MP &md) {for (u_t ret= md.set(1);; x= md.mul(x, x))if (k & 1 ? ret= md.mul(ret, x) : 0; !(k>>= 1)) return ret;}#undef NORM#undef PLUS#undef DIFF#undef SGN#undef CE}namespace math_internal {struct m_b {};struct s_b: m_b {};}template <class mod_t> constexpr bool is_modint_v= std::is_base_of_v<math_internal::m_b, mod_t>;template <class mod_t> constexpr bool is_staticmodint_v= std::is_base_of_v<math_internal::s_b, mod_t>;namespace math_internal {#define CE constexprtemplate <class MP, u64 MOD> struct SB: s_b {protected:static CE MP md= MP(MOD);};template <class Int, class U, class B> struct MInt: public B {using Uint= U;static CE inline auto mod() { return B::md.mod; }CE MInt(): x(0) {}template <class T, typename= enable_if_t<is_modint_v<T> && !is_same_v<T, MInt>>> CE MInt(T v): x(B::md.set(v.val() % B::md.mod)) {}CE MInt(__int128_t n): x(B::md.set((n < 0 ? ((n= (-n) % B::md.mod) ? B::md.mod - n : n) : n % B::md.mod))) {}CE MInt operator-() const { return MInt() - *this; }#define FUNC(name, op) \CE MInt name const { \MInt ret; \return ret.x= op, ret; \}FUNC(operator+(const MInt & r), B::md.plus(x, r.x))FUNC(operator-(const MInt & r), B::md.diff(x, r.x))FUNC(operator*(const MInt & r), B::md.mul(x, r.x))FUNC(pow(u64 k), math_internal::pow(x, k, B::md))#undef FUNCCE MInt operator/(const MInt& r) const { return *this * r.inv(); }CE MInt& operator+=(const MInt& r) { return *this= *this + r; }CE MInt& operator-=(const MInt& r) { return *this= *this - r; }CE MInt& operator*=(const MInt& r) { return *this= *this * r; }CE MInt& operator/=(const MInt& r) { return *this= *this / r; }CE bool operator==(const MInt& r) const { return B::md.norm(x) == B::md.norm(r.x); }CE bool operator!=(const MInt& r) const { return !(*this == r); }CE bool operator<(const MInt& r) const { return B::md.norm(x) < B::md.norm(r.x); }CE inline MInt inv() const { return mod_inv<Int>(val(), B::md.mod); }CE inline Uint val() const { return B::md.get(x); }friend ostream& operator<<(ostream& os, const MInt& r) { return os << r.val(); }friend istream& operator>>(istream& is, MInt& r) {i64 v;return is >> v, r= MInt(v), is;}private:Uint x;};template <u64 MOD> using ModInt= conditional_t < (MOD < (1 << 30)) & MOD, MInt<int, u32, SB<MP_Mo<u32, u64, 32, 31>, MOD>>, conditional_t < (MOD <(1ull << 62)) & MOD, MInt<i64, u64, SB<MP_Mo<u64, u128, 64, 63>, MOD>>, conditional_t<MOD<(1u << 31), MInt<int, u32, SB<MP_Na, MOD>>,conditional_t<MOD<(1ull << 32), MInt<i64, u32, SB<MP_Na, MOD>>, conditional_t<MOD <= (1ull << 41), MInt<i64, u64, SB<MP_Br2, MOD>>, MInt<i64, u64, SB<MP_D2B1, MOD>>>>>>>;#undef CE}using math_internal::ModInt;namespace math_internal {template <class Uint, class MP, u64... args> constexpr bool miller_rabin(Uint n) {const MP md(n);const Uint s= __builtin_ctzll(n - 1), d= n >> s, one= md.set(1), n1= md.norm(md.set(n - 1));for (auto a: {args...})if (Uint b= a % n; b)if (Uint p= md.norm(pow(md.set(b), d, md)); p != one)for (int i= s; p != n1; p= md.norm(md.mul(p, p)))if (!(--i)) return 0;return 1;}constexpr bool is_prime(u64 n) {if (n < 2 || n % 6 % 4 != 1) return (n | 1) == 3;if (n < (1 << 30)) return miller_rabin<u32, MP_Mo<u32, u64, 32, 31>, 2, 7, 61>(n);if (n < (1ull << 62)) return miller_rabin<u64, MP_Mo<u64, u128, 64, 63>, 2, 325, 9375, 28178, 450775, 9780504, 1795265022>(n);return miller_rabin<u64, MP_D2B1, 2, 325, 9375, 28178, 450775, 9780504, 1795265022>(n);}}using math_internal::is_prime;template <class Int> constexpr int bsf(Int a) {if constexpr (sizeof(Int) == 16) {uint64_t lo= a & uint64_t(-1);return lo ? __builtin_ctzll(lo) : 64 + __builtin_ctzll(a >> 64);} else if constexpr (sizeof(Int) == 8) return __builtin_ctzll(a);else return __builtin_ctz(a);}template <class Int> constexpr Int binary_gcd(Int a, Int b) {if (a == 0 || b == 0) return a + b;int n= bsf(a), m= bsf(b), s= 0;for (a>>= n, b>>= m; a != b;) {Int d= a - b;bool f= a > b;s= bsf(d), b= f ? b : a, a= (f ? d : -d) >> s;}return a << std::min(n, m);}namespace math_internal {template <class T> constexpr void bubble_sort(T *bg, T *ed) {for (int sz= ed - bg, i= 0; i < sz; i++)for (int j= sz; --j > i;)if (auto tmp= bg[j - 1]; bg[j - 1] > bg[j]) bg[j - 1]= bg[j], bg[j]= tmp;}template <class T, size_t _Nm> struct ConstexprArray {constexpr size_t size() const { return sz; }constexpr auto &operator[](int i) const { return dat[i]; }constexpr auto *begin() const { return dat; }constexpr auto *end() const { return dat + sz; }protected:T dat[_Nm]= {};size_t sz= 0;friend ostream &operator<<(ostream &os, const ConstexprArray &r) {os << "[";for (size_t i= 0; i < r.sz; ++i) os << r[i] << ",]"[i == r.sz - 1];return os;}};class Factors: public ConstexprArray<pair<u64, uint16_t>, 16> {template <class Uint, class MP> static constexpr Uint rho(Uint n, Uint c) {const MP md(n);auto f= [&md, n, c](Uint x) { return md.plus(md.mul(x, x), c); };const Uint m= 1LL << (__lg(n) / 5);Uint x= 1, y= md.set(2), z= 1, q= md.set(1), g= 1;for (Uint r= 1, i= 0; g == 1; r<<= 1) {for (x= y, i= r; i--;) y= f(y);for (Uint k= 0; k < r && g == 1; g= binary_gcd(md.get(q), n), k+= m)for (z= y, i= min(m, r - k); i--;) y= f(y), q= md.mul(q, md.diff(y, x));}if (g == n) do {z= f(z), g= binary_gcd(md.get(md.diff(z, x)), n);} while (g == 1);return g;}static constexpr u64 find_prime_factor(u64 n) {if (is_prime(n)) return n;for (u64 i= 100; i--;)if (n= n < (1 << 30) ? rho<u32, MP_Mo<u32, u64, 32, 31>>(n, i + 1) : n < (1ull << 62) ? rho<u64, MP_Mo<u64, u128, 64, 63>>(n, i + 1) : rho<u64,MP_D2B1>(n, i + 1); is_prime(n)) return n;return 0;}constexpr void init(u64 n) {for (u64 p= 2; p < 98 && p * p <= n; ++p)if (n % p == 0)for (dat[sz++].first= p; n % p == 0;) n/= p, ++dat[sz - 1].second;for (u64 p= 0; n > 1; dat[sz++].first= p)for (p= find_prime_factor(n); n % p == 0;) n/= p, ++dat[sz].second;}public:constexpr Factors()= default;constexpr Factors(u64 n) { init(n), bubble_sort(dat, dat + sz); }};} // namespace math_internalusing math_internal::Factors;constexpr uint64_t totient(const Factors &f) {uint64_t ret= 1, i= 0;for (auto [p, e]: f)for (ret*= p - 1, i= e; --i;) ret*= p;return ret;}constexpr auto totient(uint64_t n) { return totient(Factors(n)); }template <class Uint= uint64_t> std::vector<Uint> enumerate_divisors(const Factors &f) {int k= 1;for (auto [p, e]: f) k*= e + 1;std::vector<Uint> ret(k, 1);k= 1;for (auto [p, e]: f) {int sz= k;for (Uint pw= 1; pw*= p, e--;)for (int j= 0; j < sz;) ret[k++]= ret[j++] * pw;}return ret;}template <class Uint> std::vector<Uint> enumerate_divisors(Uint n) { return enumerate_divisors<Uint>(Factors(n)); }template <class T> struct ArrayOnDivisors {uint64_t n;uint8_t shift;std::vector<int> os, id;std::vector<std::pair<uint64_t, T>> dat;unsigned hash(uint64_t i) const { return (i * 11995408973635179863ULL) >> shift; }#define _UP for (int j= k; j < a; ++j)#define _DWN for (int j= a; j-- > k;)#define _OP(J, K, op) dat[i + J].second op##= dat[i + K].second#define _FUN(J, K, name) name(dat[i + J].second, dat[i + K].second)#define _ZETA(op) \int k= 1; \for (auto [p, e]: factors) { \int a= k * (e + 1); \for (int i= 0, d= dat.size(); i < d; i+= a) op; \k= a; \}public:Factors factors;template <class Uint> ArrayOnDivisors(uint64_t N, const Factors &factors, const std::vector<Uint> &divisors): n(N), shift(__builtin_clzll(divisors.size()) - 1), os((1 << (64 - shift)) + 1), id(divisors.size()), dat(divisors.size()), factors(factors) {for (int i= divisors.size(); i--;) dat[i].first= divisors[i];for (auto d: divisors) ++os[hash(d)];std::partial_sum(os.begin(), os.end(), os.begin());for (int i= divisors.size(); i--;) id[--os[hash(divisors[i])]]= i;}ArrayOnDivisors(uint64_t N, const Factors &factors): ArrayOnDivisors(N, factors, enumerate_divisors(factors)) {}ArrayOnDivisors(uint64_t N): ArrayOnDivisors(N, Factors(N)) {}T &operator[](uint64_t i) {assert(i && n % i == 0);for (unsigned a= hash(i), j= os[a]; j < os[a + 1]; ++j)if (auto &[d, v]= dat[id[j]]; d == i) return v;assert(0);}size_t size() const { return dat.size(); }auto begin() { return dat.begin(); }auto begin() const { return dat.begin(); }auto end() { return dat.begin() + os.back(); }auto end() const { return dat.begin() + os.back(); }/* f -> g s.t. g(n) = sum_{m|n} f(m) */void divisor_zeta() { _ZETA(_UP _OP(j, j - k, +)) }/* f -> h s.t. f(n) = sum_{m|n} h(m) */void divisor_mobius() { _ZETA(_DWN _OP(j, j - k, -)) }/* f -> g s.t. g(n) = sum_{n|m} f(m) */void multiple_zeta() { _ZETA(_DWN _OP(j - k, j, +)) }/* f -> h s.t. f(n) = sum_{n|m} h(m) */void multiple_mobius() { _ZETA(_UP _OP(j - k, j, -)) }/* f -> g s.t. g(n) = sum_{m|n} f(m), add(T& a, T b): a+=b */template <class F> void divisor_zeta(const F &add) { _ZETA(_UP _FUN(j, j - k, add)) }/* f -> h s.t. f(n) = sum_{m|n} h(m), sub(T& a, T b): a-=b */template <class F> void divisor_mobius(const F &sub) { _ZETA(_UP _FUN(j, j - k, sub)) }/* f -> g s.t. g(n) = sum_{n|m} f(m), add(T& a, T b): a+=b */template <class F> void multiple_zeta(const F &add) { _ZETA(_UP _FUN(j - k, j, add)) }/* f -> h s.t. f(n) = sum_{n|m} h(m), sub(T& a, T b): a-=b */template <class F> void multiple_mobius(const F &sub) { _ZETA(_UP _FUN(j - k, j, sub)) }#undef _UP#undef _DWN#undef _OP#undef _ZETA// f(p,e): multiplicative function of p^etemplate <typename F> void set_multiplicative(const F &f) {int k= 1;dat[0].second= 1;for (auto [p, e]: factors)for (int m= k, d= 1; d <= e; ++d)for (int i= 0; i < m;) dat[k++].second= dat[i++].second * f(p, d);}void set_totient() {int k= 1;dat[0].second= 1;for (auto [p, e]: factors) {uint64_t b= p - 1;for (int m= k; e--; b*= p)for (int i= 0; i < m;) dat[k++].second= dat[i++].second * b;}}void set_mobius() {set_multiplicative([](auto, auto e) { return e == 1 ? -1 : 0; });}};template <class mod_t> class FactorialPrecalculation {static_assert(is_modint_v<mod_t>);static inline std::vector<mod_t> iv, fct, fiv;public:static void reset() { iv.clear(), fct.clear(), fiv.clear(); }static inline mod_t inv(int n) {assert(0 < n);if (int k= iv.size(); k <= n) {if (iv.resize(n + 1); !k) iv[1]= 1, k= 2;for (int mod= mod_t::mod(), q; k <= n; ++k) q= (mod + k - 1) / k, iv[k]= iv[k * q - mod] * q;}return iv[n];}static inline mod_t fact(int n) {assert(0 <= n);if (int k= fct.size(); k <= n) {if (fct.resize(n + 1); !k) fct[0]= 1, k= 1;for (; k <= n; ++k) fct[k]= fct[k - 1] * k;}return fct[n];}static inline mod_t finv(int n) {assert(0 <= n);if (int k= fiv.size(); k <= n) {if (fiv.resize(n + 1); !k) fiv[0]= 1, k= 1;for (; k <= n; ++k) fiv[k]= fiv[k - 1] * inv(k);}return fiv[n];}static inline mod_t nPr(int n, int r) { return r < 0 || n < r ? mod_t(0) : fact(n) * finv(n - r); }// [x^r] (1 + x)^nstatic inline mod_t nCr(int n, int r) { return r < 0 || n < r ? mod_t(0) : fact(n) * finv(n - r) * finv(r); }// [x^r] (1 - x)^{-n}static inline mod_t nHr(int n, int r) { return !r ? mod_t(1) : nCr(n + r - 1, r); }};using namespace std;signed main() {cin.tie(0);ios::sync_with_stdio(0);using Mint= ModInt<int(1e9) + 7>;using F= FactorialPrecalculation<Mint>;int K;cin >> K;int tot= 0, g= 0;vector<int> C(K);for (int i= 0; i < K; ++i) cin >> C[i], tot+= C[i], g= gcd(g, C[i]);Mint ans= 0;ArrayOnDivisors<Mint> A(g);A.set_totient();for (auto [d, phi]: A) {Mint tmp= F::fact(tot / d);for (int i= 0; i < K; ++i) tmp*= F::finv(C[i] / d);tmp*= phi;ans+= tmp;}ans/= tot;cout << ans << '\n';return 0;}