QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#637527#8339. Rooted TreeAntonioAC ✓1157ms3748kbC++204.0kb2024-10-13 13:12:562024-10-13 13:12:56

Judging History

This is the latest submission verdict.

  • [2024-10-13 13:12:56]
  • Judged
  • Verdict: AC
  • Time: 1157ms
  • Memory: 3748kb
  • [2024-10-13 13:12:56]
  • Submitted

answer

#include<bits/stdc++.h>

#define int long long
const int inf = 1e18;
const int INF = 2e9;

//#pragma warning(disable:4235)
//#pragma warning(disable:4996)
//#include <__msvc_int128.hpp>
//using int128_t = std::_Signed128;
//using uint128_t = std::_Unsigned128;
//using __int128_t = std::_Signed128;
#pragma GCC optimize(2)
#pragma GCC optimize(3)


using ll = long long;
using ull = unsigned long long;
using i64 = long long;


template<typename T, typename R>
std::ostream& operator<<(std::ostream& os, const std::pair<T, R>& p) {
	os << "(" << p.first << "," << p.second << ") ";
	return os;
}

void debug() {
	std::cout << "******************************debug()******************************\n";
}

template<typename T>
void debug(T x, std::string name = "x") {
	std::cerr << name << "=" << x << std::endl;
}

template<typename T>
void debug(const std::vector<T>& v, const std::string& name = "vec", size_t N = 0) {
	for (size_t i = 0; i < v.size(); ++i) {
		std::cerr << name << "[" << i << "] = " << v[i] << std::endl;
	}
}
using namespace std;
template<typename T, size_t N>
void debug(const std::vector<std::array<T, N>>& v, const std::string& name = "vec") {
	for (size_t i = 0; i < v.size(); ++i) {
		for (size_t j = 0; j < N; ++j) {
			std::cerr << name << "[" << i << "][" << j << "] = " << v[i][j] << " ";
		}
		std::cerr << std::endl << std::endl;
	}
}
using pii = std::pair<int, int>;

const int N = 6e4 + 10;

const int mod = 1e9 + 9;

template<const int T>
struct ModInt {
    const static int mod = T;
    int x;
    ModInt(signed x = 0) : x(x% mod) {}
    ModInt(long long x) : x((long long)(x% mod)) {}
    int val() { return x; }
    ModInt operator + (const ModInt& a) const { int x0 = x + a.x; return ModInt(x0 < mod ? x0 : x0 - mod); }
    ModInt operator - (const ModInt& a) const { int x0 = x - a.x; return ModInt(x0 < 0 ? x0 + mod : x0); }
    ModInt operator * (const ModInt& a) const { return ModInt(1LL * x * a.x % mod); }
    ModInt operator / (const ModInt& a) const { return *this * a.inv(); }
    bool operator == (const ModInt& a) const { return x == a.x; };
    bool operator != (const ModInt& a) const { return x != a.x; };
    void operator += (const ModInt& a) { x += a.x; if (x >= mod) x -= mod; }
    void operator -= (const ModInt& a) { x -= a.x; if (x < 0) x += mod; }
    void operator *= (const ModInt& a) { x = 1LL * x * a.x % mod; }
    void operator /= (const ModInt& a) { *this = *this / a; }
    friend ModInt operator + (int y, const ModInt& a) { int x0 = y + a.x; return ModInt(x0 < mod ? x0 : x0 - mod); }
    friend ModInt operator - (int y, const ModInt& a) { int x0 = y - a.x; return ModInt(x0 < 0 ? x0 + mod : x0); }
    friend ModInt operator * (int y, const ModInt& a) { return ModInt(1LL * y * a.x % mod); }
    friend ModInt operator / (int y, const ModInt& a) { return ModInt(y) / a; }
    friend std::ostream& operator<<(std::ostream& os, const ModInt& a) { return os << a.x; }
    friend std::istream& operator>>(std::istream& is, ModInt& t) { return is >> t.x; }

    ModInt pow(int64_t n) const {
        ModInt res(1), mul(x);
        while (n) {
            if (n & 1) res *= mul;
            mul *= mul;
            n >>= 1;
        }
        return res;
    }

    ModInt inv() const {
        int a = x, b = mod, u = 1, v = 0;
        while (b) {
            int t = a / b;
            a -= t * b; std::swap(a, b);
            u -= t * v; std::swap(u, v);
        }
        if (u < 0) u += mod;
        return u;
    }

};

using mint = ModInt<1000000009>;

void solve() {
    int n, m;
    std::cin >> m >> n;

    mint res = 0, sum = 1, v = 0;
    for (int i = 1; i <= n; ++i) {
        res += (v + 1) * m;

        v = ((v + 1) * m + (sum - 1) * v) * mint(sum + m - 1).inv();
        sum = sum - 1 + m;
    }

    std::cout << res << "\n";

}

signed main() {
	std::ios::sync_with_stdio(0);
	std::cin.tie(0);
	std::cout.tie(0);

	int t = 1;
	//std::cin >> t;

	while (t--) {
		solve();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3672kb

input:

6 2

output:

18

result:

ok 1 number(s): "18"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

2 6

output:

600000038

result:

ok 1 number(s): "600000038"

Test #3:

score: 0
Accepted
time: 63ms
memory: 3612kb

input:

83 613210

output:

424200026

result:

ok 1 number(s): "424200026"

Test #4:

score: 0
Accepted
time: 746ms
memory: 3688kb

input:

48 6713156

output:

198541581

result:

ok 1 number(s): "198541581"

Test #5:

score: 0
Accepted
time: 0ms
memory: 3724kb

input:

1 111

output:

6216

result:

ok 1 number(s): "6216"

Test #6:

score: 0
Accepted
time: 790ms
memory: 3672kb

input:

28 7304152

output:

457266679

result:

ok 1 number(s): "457266679"

Test #7:

score: 0
Accepted
time: 438ms
memory: 3664kb

input:

38 4101162

output:

232117382

result:

ok 1 number(s): "232117382"

Test #8:

score: 0
Accepted
time: 1118ms
memory: 3616kb

input:

51 9921154

output:

340670552

result:

ok 1 number(s): "340670552"

Test #9:

score: 0
Accepted
time: 194ms
memory: 3652kb

input:

79 1801157

output:

620550406

result:

ok 1 number(s): "620550406"

Test #10:

score: 0
Accepted
time: 575ms
memory: 3608kb

input:

22 5417157

output:

457449071

result:

ok 1 number(s): "457449071"

Test #11:

score: 0
Accepted
time: 335ms
memory: 3672kb

input:

25 3210162

output:

36368303

result:

ok 1 number(s): "36368303"

Test #12:

score: 0
Accepted
time: 318ms
memory: 3612kb

input:

67 2919160

output:

935195555

result:

ok 1 number(s): "935195555"

Test #13:

score: 0
Accepted
time: 989ms
memory: 3736kb

input:

77 8613163

output:

482832472

result:

ok 1 number(s): "482832472"

Test #14:

score: 0
Accepted
time: 1157ms
memory: 3672kb

input:

90 10000000

output:

275581651

result:

ok 1 number(s): "275581651"

Test #15:

score: 0
Accepted
time: 1155ms
memory: 3612kb

input:

99 9999999

output:

126087169

result:

ok 1 number(s): "126087169"

Test #16:

score: 0
Accepted
time: 1154ms
memory: 3748kb

input:

100 10000000

output:

451590067

result:

ok 1 number(s): "451590067"

Extra Test:

score: 0
Extra Test Passed