QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#50437#4843. Infectious DiseaseYaoBIG#AC ✓3241ms112908kbC++172.9kb2022-09-26 03:18:032022-09-26 03:18:06

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-09-26 03:18:06]
  • 评测
  • 测评结果:AC
  • 用时:3241ms
  • 内存:112908kb
  • [2022-09-26 03:18:03]
  • 提交

answer

#include "bits/stdc++.h"
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define revrep(i, a, n) for (int i = n; i >= a; --i)
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
using namespace std;

string to_string(const string &s) { return '"' + s + '"'; }
template<class A> string to_string(A v) {
	bool first = 1;
	string res = "{";
	for (const auto &x: v) {
		if (!first) res += ", ";
		first = 0;
		res += to_string(x);
	}
	res += "}";
	return res;
}
void debug_out() { cerr << endl; }
template<class H, class... T> void debug_out(H h, T... t) {
	cerr << " " << to_string(h);
	debug_out(t...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

using vi = vector<int>;
using ll = long long;
using pii = pair<int, int>;

template<const int &mod> struct Z {
	int x;
	Z(ll a = 0): x(a % mod) { if (x < 0) x += mod; }
	explicit operator int() const { return x; }
	Z& operator +=(Z b) { x += b.x; if (x >= mod) x -= mod; return *this; }
	Z& operator -=(Z b) { x -= b.x; if (x < 0) x += mod; return *this; }
	Z& operator *=(Z b) { x = 1ll * x * b.x % mod; return *this; }
	friend Z operator +(Z a, Z b) { return a += b; }
	friend Z operator -(Z a, Z b) { return a -= b; }
	friend Z operator *(Z a, Z b) { return a *= b; }

	Z pow(ll k) const {
		Z res = 1, a = *this;
		for (; k; k >>= 1, a = a * a)  if (k & 1) res = res * a;
		return res;
	}
	Z& operator /=(Z b) {
		assert(b.x != 0);
		*this = *this * b.pow(mod - 2);
		return *this;
	}
	friend Z operator /(Z a, Z b) { return a /= b; }
	friend string to_string(Z a) { return to_string(a.x); }
};
const int mod = 1e9 + 7;
using Mint = Z<mod>;

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	
	vi pw3(16);
	pw3[0] = 1;
	rep(i, 1, 15) pw3[i] = pw3[i - 1] * 3;

	int n; cin >> n;

	vector<Mint> fac(n + 1), ifac(n + 1);
	fac[0] = ifac[0] = ifac[1] = 1;
	rep(i, 2, n) ifac[i] = ifac[mod % i] * (mod - mod / i);
	rep(i, 1, n) ifac[i] *= ifac[i - 1];
	rep(i, 1, n) fac[i] = fac[i - 1] * i;

	auto binom = [&](int n, int m) {
		if (m < 0 || n < m) return Mint{0};
		else return fac[n] * ifac[m] * ifac[n - m];
	};
	auto ibinom = [&](int n, int m) {
		if (m < 0 || n < m) return Mint{0};
		else return ifac[n] * fac[m] * fac[n - m];
	};
	vector<Mint> dp{0, 1};
	Mint ans = 0;
	rep(i, 0, 15) {
		ans += i * dp[0];
		int unvac = n - pw3[i];
		int bound = min(unvac, 1 << i);
		if (unvac < 0) break;
		int nunvac = max(0, n - pw3[i + 1]);
		int nbound = min(nunvac, 1 << (i + 1));
		vector<Mint> tmp(min(unvac, 1 << (i + 1)) + 1), ndp(nbound + 1);
		rep(j, 1, bound) if (dp[j].x) {
			int nj = min(j * 2, unvac);
			tmp[nj] += dp[j];
		}
		// debug(i, unvac, nunvac, tmp, nbound);
		rep(j, 1, min(unvac, 1 << (i + 1))) {
			rep(k, 0, min(j, nbound)) {
				ndp[k] += tmp[j] * binom(j, k) * binom(unvac - j, nunvac - k) * ibinom(unvac, nunvac);
			}
		}
		swap(dp, ndp);
	}
	printf("%d\n", (int)ans);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3776kb

input:

2

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 2ms
memory: 3704kb

input:

114

output:

505208013

result:

ok 1 number(s): "505208013"

Test #3:

score: 0
Accepted
time: 3241ms
memory: 112908kb

input:

14000000

output:

44565093

result:

ok 1 number(s): "44565093"

Test #4:

score: 0
Accepted
time: 3151ms
memory: 100104kb

input:

12345678

output:

123143093

result:

ok 1 number(s): "123143093"

Test #5:

score: 0
Accepted
time: 195ms
memory: 9524kb

input:

776777

output:

764022068

result:

ok 1 number(s): "764022068"

Test #6:

score: 0
Accepted
time: 2ms
memory: 3756kb

input:

3

output:

1

result:

ok 1 number(s): "1"

Test #7:

score: 0
Accepted
time: 2ms
memory: 3744kb

input:

4

output:

666666673

result:

ok 1 number(s): "666666673"

Test #8:

score: 0
Accepted
time: 2ms
memory: 3792kb

input:

5

output:

833333341

result:

ok 1 number(s): "833333341"

Test #9:

score: 0
Accepted
time: 2ms
memory: 3696kb

input:

6

output:

300000004

result:

ok 1 number(s): "300000004"

Test #10:

score: 0
Accepted
time: 2ms
memory: 3660kb

input:

7

output:

533333339

result:

ok 1 number(s): "533333339"

Test #11:

score: 0
Accepted
time: 1ms
memory: 3628kb

input:

8

output:

952380961

result:

ok 1 number(s): "952380961"

Test #12:

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

input:

9

output:

964285723

result:

ok 1 number(s): "964285723"

Test #13:

score: 0
Accepted
time: 2ms
memory: 3700kb

input:

10

output:

416666672

result:

ok 1 number(s): "416666672"

Test #14:

score: 0
Accepted
time: 2ms
memory: 3620kb

input:

26

output:

990086590

result:

ok 1 number(s): "990086590"

Test #15:

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

input:

27

output:

528360342

result:

ok 1 number(s): "528360342"

Test #16:

score: 0
Accepted
time: 2ms
memory: 3696kb

input:

28

output:

273239648

result:

ok 1 number(s): "273239648"

Test #17:

score: 0
Accepted
time: 801ms
memory: 40780kb

input:

4782967

output:

332194401

result:

ok 1 number(s): "332194401"

Test #18:

score: 0
Accepted
time: 818ms
memory: 40832kb

input:

4782968

output:

362625027

result:

ok 1 number(s): "362625027"

Test #19:

score: 0
Accepted
time: 816ms
memory: 40900kb

input:

4782969

output:

971032452

result:

ok 1 number(s): "971032452"

Test #20:

score: 0
Accepted
time: 806ms
memory: 40912kb

input:

4782970

output:

452836984

result:

ok 1 number(s): "452836984"

Test #21:

score: 0
Accepted
time: 793ms
memory: 40856kb

input:

4782971

output:

349324970

result:

ok 1 number(s): "349324970"

Test #22:

score: 0
Accepted
time: 808ms
memory: 40852kb

input:

4782972

output:

46862963

result:

ok 1 number(s): "46862963"

Test #23:

score: 0
Accepted
time: 12ms
memory: 4308kb

input:

114514

output:

972669965

result:

ok 1 number(s): "972669965"

Test #24:

score: 0
Accepted
time: 733ms
memory: 18460kb

input:

1919810

output:

482430785

result:

ok 1 number(s): "482430785"

Test #25:

score: 0
Accepted
time: 3001ms
memory: 72856kb

input:

8877877

output:

486769120

result:

ok 1 number(s): "486769120"