QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#65824#4843. Infectious Diseaseasd12AC ✓1715ms113552kbC++173.2kb2022-12-03 18:50:182022-12-03 18:50:19

Judging History

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

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

answer

#include<bits/stdc++.h>
using namespace std;
#define ctz __builtin_ctzll
#define ppc __builtin_popcountll
#define par __builtin_parityll
#define all(x) (x).begin(), (x).end()
using LL = long long;
using ull = unsigned long long;
using db = double;
using ldb = long double;
const int INF = 1e9;
const db EPS = 1e-8;
const db PI = acos(-1);

const int mod = 1e9 + 7;

int norm(int x) {
    if (x < 0) {x += mod;}
    if (x >= mod) {x -= mod;}
    return x;
}
template<class T>
T ksm(T x,LL y){
    T res = 1;
    for(;y;y>>=1){
        if(y&1)
            res = res*x;
        x=x*x;
    }
    return res;
}
struct modint {
    int x;
    modint(int x = 0) : x(norm(x)) {}
    modint(LL x) : x(norm((int)(x%mod))) {}
    int val() const {return x;}
    modint operator-() const {return modint(norm(mod - x));}
    modint inv() const {assert(x != 0);return ksm(*this, mod - 2);}
    modint &operator*=(const modint &rhs) {x = LL(x) * rhs.x % mod;return *this;}
    modint &operator+=(const modint &rhs) {x = norm(x + rhs.x);return *this;}
    modint &operator-=(const modint &rhs) {x = norm(x - rhs.x);return *this;}
    modint &operator/=(const modint &rhs) {return *this *= rhs.inv();}
    friend modint operator*(const modint &lhs, const modint &rhs) {modint res = lhs;res *= rhs;return res;}
    friend modint operator+(const modint &lhs, const modint &rhs) {modint res = lhs;res += rhs;return res;}
    friend modint operator-(const modint &lhs, const modint &rhs) {modint res = lhs;res -= rhs;return res;}
    friend modint operator/(const modint &lhs, const modint &rhs) {modint res = lhs;res /= rhs;return res;}
    friend std::istream &operator>>(std::istream &is, modint &a) {LL v;is >> v;a = modint(v);return is;}
    friend std::ostream &operator<<(std::ostream &os, const modint &a) {return os << a.val();}
};

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cout << fixed << setprecision(15);

    int n;
    cin >> n;

    vector<modint> fac(n + 1), ifac(n + 1);
    fac[0] = 1;
    for(int i = 1; i <= n; i ++) {
        fac[i] = fac[i - 1] * i;
    }
    ifac[n] = fac[n].inv();
    for(int i = n; i >= 1; i --) {
        ifac[i - 1] = ifac[i] * i;
    }

    auto C = [&](int x, int y) -> modint {
        if(x < y) {
            return modint(0);
        }
        return fac[x] * ifac[y] * ifac[x - y];
    };

    vector<vector<modint>> f(16, vector<modint>(16385, 0));
    f[0][1] = 1;
    for(int i = 1; i <= 15; i ++) {
        int s = n - ksm(3, i - 1), t = 2 * ksm(3, i - 1);
        if(s <= t) {
            for(int j = 1; j <= (1 << (i - 1)); j ++) {
                f[i][0] += f[i - 1][j];
            }
            break;
        }
        assert(s > t);
        modint inv = C(s, t).inv();
        for(int j = 0; j <= (1 << i); j ++) {
            for(int k = max(1, (j + 1) >> 1); k <= (1 << (i - 1)); k ++) {
                int g = 2 * k - j;
                f[i][j] += f[i - 1][k] * C(2 * k, g) * C(s - 2 * k, t - g) * inv;
            }
        }
    }

    modint ans = 0;
    for(int i = 1; i <= 15; i ++) {
        ans += i * f[i][0];
    }

    cout << ans << '\n';

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 4028kb

input:

2

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 4ms
memory: 3960kb

input:

114

output:

505208013

result:

ok 1 number(s): "505208013"

Test #3:

score: 0
Accepted
time: 1715ms
memory: 113552kb

input:

14000000

output:

44565093

result:

ok 1 number(s): "44565093"

Test #4:

score: 0
Accepted
time: 1645ms
memory: 100492kb

input:

12345678

output:

123143093

result:

ok 1 number(s): "123143093"

Test #5:

score: 0
Accepted
time: 110ms
memory: 10292kb

input:

776777

output:

764022068

result:

ok 1 number(s): "764022068"

Test #6:

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

input:

3

output:

1

result:

ok 1 number(s): "1"

Test #7:

score: 0
Accepted
time: 4ms
memory: 3992kb

input:

4

output:

666666673

result:

ok 1 number(s): "666666673"

Test #8:

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

input:

5

output:

833333341

result:

ok 1 number(s): "833333341"

Test #9:

score: 0
Accepted
time: 3ms
memory: 3888kb

input:

6

output:

300000004

result:

ok 1 number(s): "300000004"

Test #10:

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

input:

7

output:

533333339

result:

ok 1 number(s): "533333339"

Test #11:

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

input:

8

output:

952380961

result:

ok 1 number(s): "952380961"

Test #12:

score: 0
Accepted
time: 4ms
memory: 4028kb

input:

9

output:

964285723

result:

ok 1 number(s): "964285723"

Test #13:

score: 0
Accepted
time: 5ms
memory: 3992kb

input:

10

output:

416666672

result:

ok 1 number(s): "416666672"

Test #14:

score: 0
Accepted
time: 4ms
memory: 4032kb

input:

26

output:

990086590

result:

ok 1 number(s): "990086590"

Test #15:

score: 0
Accepted
time: 3ms
memory: 4024kb

input:

27

output:

528360342

result:

ok 1 number(s): "528360342"

Test #16:

score: 0
Accepted
time: 4ms
memory: 3952kb

input:

28

output:

273239648

result:

ok 1 number(s): "273239648"

Test #17:

score: 0
Accepted
time: 434ms
memory: 41348kb

input:

4782967

output:

332194401

result:

ok 1 number(s): "332194401"

Test #18:

score: 0
Accepted
time: 417ms
memory: 41476kb

input:

4782968

output:

362625027

result:

ok 1 number(s): "362625027"

Test #19:

score: 0
Accepted
time: 443ms
memory: 41424kb

input:

4782969

output:

971032452

result:

ok 1 number(s): "971032452"

Test #20:

score: 0
Accepted
time: 1473ms
memory: 41368kb

input:

4782970

output:

452836984

result:

ok 1 number(s): "452836984"

Test #21:

score: 0
Accepted
time: 1476ms
memory: 41472kb

input:

4782971

output:

349324970

result:

ok 1 number(s): "349324970"

Test #22:

score: 0
Accepted
time: 1463ms
memory: 41432kb

input:

4782972

output:

46862963

result:

ok 1 number(s): "46862963"

Test #23:

score: 0
Accepted
time: 6ms
memory: 4996kb

input:

114514

output:

972669965

result:

ok 1 number(s): "972669965"

Test #24:

score: 0
Accepted
time: 409ms
memory: 19176kb

input:

1919810

output:

482430785

result:

ok 1 number(s): "482430785"

Test #25:

score: 0
Accepted
time: 1698ms
memory: 73340kb

input:

8877877

output:

486769120

result:

ok 1 number(s): "486769120"