結果

問題 No.125 悪の花弁
ユーザー rsk0315
提出日時 2019-08-16 23:32:51
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 60 ms / 5,000 ms
コード長 3,373 bytes
コンパイル時間 1,090 ms
コンパイル使用メモリ 62,424 KB
最終ジャッジ日時 2025-01-07 13:00:30
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 6
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:126:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  126 |   scanf("%zu", &K);
      |   ~~~~~^~~~~~~~~~~
main.cpp:129:26: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  129 |   for (auto& ci: C) scanf("%jd", &ci);
      |                     ~~~~~^~~~~~~~~~~~

ソースコード

diff #
プレゼンテーションモードにする

#include <cstdio>
#include <cstdint>
#include <vector>
#include <algorithm>
#include <numeric>
#include <utility>
intmax_t gcd(intmax_t m, intmax_t n) {
while (n) std::swap(m %= n, n);
return m;
}
std::vector<intmax_t> divisor(intmax_t n) {
std::vector<intmax_t> res;
for (intmax_t i = 1; i*i <= n; ++i) {
if (n % i == 0) {
res.push_back(i);
if (i != n/i) res.push_back(n/i);
}
}
std::sort(res.begin(), res.end());
return res;
}
constexpr intmax_t operator ""_jd(unsigned long long n) { return n; }
constexpr uintmax_t operator ""_ju(unsigned long long n) { return n; }
constexpr size_t operator ""_zu(unsigned long long n) { return n; }
// constexpr ptrdiff_t operator ""_td(unsigned long long n) { return n; }
template <typename Tp>
Tp gcd(Tp a, Tp b, Tp& x, Tp& y) {
x = Tp(0);
y = Tp(1);
for (Tp u = y, v = x; a;) {
Tp q = b/a;
std::swap(x -= q*u, u);
std::swap(y -= q*v, v);
std::swap(b -= q*a, a);
}
return b;
}
template <typename Tp>
Tp modinv(Tp a, Tp mod) {
Tp x, y;
gcd(a, mod, x, y);
x %= mod;
if (x < 0) x += mod;
return x;
}
template <typename Tp>
Tp modadd(Tp a, Tp b, Tp mod) {
a += b % mod;
if (a < 0) a += mod;
if (a >= mod) a -= mod;
return a;
}
template <typename Tp>
Tp modadd(std::initializer_list<Tp> const& adds, Tp mod) {
Tp res = 0;
for (auto const& add: adds) {
res += add % mod;
if (res < 0) res += mod;
if (res >= mod) res -= mod;
}
return res;
}
template <typename Tp>
Tp modsub(Tp a, Tp b, Tp mod) {
a -= b % mod;
if (a < 0) a += mod;
if (a >= mod) a -= mod;
return a;
}
template <typename Tp>
Tp modmul(std::initializer_list<Tp> const& muls, Tp mod) {
Tp res = 1;
for (auto const& mul: muls) (res *= mul) %= mod;
return res;
}
template <typename Tp>
Tp modpow(Tp base, intmax_t iexp, Tp mod) {
Tp res = 1;
for (Tp dbl = base; iexp; iexp >>= 1) {
if (iexp & 1) res = res * dbl % mod;
dbl = dbl * dbl % mod;
}
return res;
}
class modfactorial {
std::vector<intmax_t> fact, fact_inv;
intmax_t mod;
public:
modfactorial(intmax_t N, intmax_t mod): mod(mod) {
fact.resize(N+1);
fact_inv.resize(N+1);
fact[0] = 1;
for (intmax_t i = 1; i <= N; ++i)
fact[i] = fact[i-1] * i % mod;
fact_inv[N] = modinv(fact[N], mod);
for (intmax_t i = N; i--;)
fact_inv[i] = fact_inv[i+1] * (i+1) % mod;
}
intmax_t operator ()(intmax_t k) const {
return fact[k];
}
intmax_t inverse(intmax_t k) const {
return fact_inv[k];
}
};
constexpr intmax_t mod = 1e9+7;
int main() {
size_t K;
scanf("%zu", &K);
std::vector<intmax_t> C(K);
for (auto& ci: C) scanf("%jd", &ci);
intmax_t cs = std::accumulate(C.begin(), C.end(), 0_jd);
intmax_t cg = std::accumulate(C.begin(), C.end(), 0_jd, [](auto x, auto y) { return gcd(x, y); });
modfactorial mf(cs, mod);
auto ds = divisor(cg);
std::vector<intmax_t> f(ds.size());
for (size_t i = 0; i < ds.size(); ++i) {
intmax_t d = ds[i];
f[i] = mf(cs/d);
for (size_t j = 0; j < K; ++j)
(f[i] *= mf.inverse(C[j] / d)) %= mod;
}
intmax_t res = 0;
for (size_t i = ds.size(); i--;) {
for (size_t j = i+1; j < ds.size(); ++j) {
if (ds[j] % ds[i] == 0)
f[i] = (f[i]+mod-f[j]) % mod;
}
(res += f[i] * modinv(cs/ds[i], mod)) %= mod;
}
printf("%jd\n", res);
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0