結果
問題 | No.681 Fractal Gravity Glue |
ユーザー | anta |
提出日時 | 2018-04-27 23:25:45 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 2,437 bytes |
コンパイル時間 | 1,528 ms |
コンパイル使用メモリ | 167,516 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-27 22:18:59 |
合計ジャッジ時間 | 2,232 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 20 |
ソースコード
#include "bits/stdc++.h" using namespace std; template<int MOD> struct ModInt { static const int Mod = MOD; unsigned x; ModInt() : x(0) { } ModInt(signed sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; } ModInt(signed long long sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; } int get() const { return (int)x; } ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; } ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; } ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; } ModInt &operator/=(ModInt that) { return *this *= that.inverse(); } ModInt operator+(ModInt that) const { return ModInt(*this) += that; } ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } ModInt inverse() const { signed a = x, b = MOD, u = 1, v = 0; while (b) { signed t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } if (u < 0) u += Mod; ModInt res; res.x = (unsigned)u; return res; } bool operator==(ModInt that) const { return x == that.x; } bool operator!=(ModInt that) const { return x != that.x; } ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; } }; template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) { ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; } typedef ModInt<1000000007> mint; mint solve1(int b, int d) { return ((mint(d + 1) ^ b) + ((mint(d + 1) ^ b) - b - 1) * d - 1) / d; } int solve2(int b, int d) { static const int INF = 0x3f3f3f3f; long long sum = 0; long long p = 1; for (int i = b; i >= 1; -- i) { sum += p * d; if (sum > INF) return INF; p *= d + 1; } return (int)sum; } mint solve3(int n, int b, int d) { if (b == 1) return n; int x = solve2(b - 1, d); int q = min(n / (x + 1), d); int r = n - q * (x + 1); mint sum; sum += (solve1(b - 1, d) + b) * q; sum += solve3(min(r, x), b - 1, d); return sum; } int main() { int n; int b; int d; while (~scanf("%d%d%d", &n, &b, &d)) { //a(b,d) = a(b-1,d) * (d+1) + b*d //\sum_{i=1}^b i*(d+1)^(b-1)*d mint ans = solve1(b, d); ans -= solve3(n, min(b, 100), d); printf("%d\n", ans.get()); } }