QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#575376#9309. GraphpropaneAC ✓1366ms60764kbC++203.8kb2024-09-19 13:23:072024-09-19 13:23:09

Judging History

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

  • [2024-09-19 13:23:09]
  • 评测
  • 测评结果:AC
  • 用时:1366ms
  • 内存:60764kb
  • [2024-09-19 13:23:07]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<vector>
#include<unordered_map>
using namespace std;
using LL = long long;

const int maxn = 1e7 + 5;
bool isPrime[maxn];
int primes[maxn], cnt;
int s[maxn];
void init(){
    isPrime[1] = 1;
    for(int i = 2; i < maxn; i++){
        if (!isPrime[i]) primes[cnt++] = i, s[i] = 1;
        for(int j = 0; i * primes[j] < maxn; j++){
            isPrime[i * primes[j]] = 1;
            if (i % primes[j] == 0) break;
        }
    }
    for(int i = 1; i < maxn; i++) s[i] += s[i - 1];
}	

long long int_sqrt(long long x){
    long long ans = 0;
    for (long long k = 1LL << 30; k != 0; k /= 2){
        if ((ans + k) * (ans + k) <= x){
            ans += k;
        }
    }
    return ans;
}

unordered_map<LL, LL> mp;

LL prime_pi(const LL N) {
    if (N < maxn) return s[N];
    if (mp.contains(N)) return mp[N];

    const int v = int_sqrt(N);
    int s = (v + 1) / 2;
    vector<int> smalls(s);

    for (int i = 1; i < s; ++i)
        smalls[i] = i;

    vector<int> roughs(s);

    for (int i = 0; i < s; ++i)
        roughs[i] = 2 * i + 1;

    vector<LL> larges(s);

    for (int i = 0; i < s; ++i)
        larges[i] = (N / (2 * i + 1) - 1) / 2;

    vector<bool> skip(v + 1);
    const auto divide = [](LL n, LL d) -> int { return double(n) / d; };
    const auto half = [](int n) -> int { return (n - 1) >> 1; };
    int pc = 0;

    for (int p = 3; p <= v; p += 2)
        if (!skip[p]) {
            int q = p * p;

            if (LL(q) * q > N)
                break;

            skip[p] = true;

            for (int i = q; i <= v; i += 2 * p)
                skip[i] = true;

            int ns = 0;

            for (int k = 0; k < s; ++k) {
                int i = roughs[k];

                if (skip[i])
                    continue;

                LL d = LL(i) * p;
                larges[ns] = larges[k] - (d <= v ? larges[smalls[d >> 1] - pc] : smalls[half(divide(N, d))]) + pc;
                roughs[ns++] = i;
            }

            s = ns;

            for (int i = half(v), j = ((v / p) - 1) | 1; j >= p; j -= 2) {
                int c = smalls[j >> 1] - pc;

                for (int e = (j * p) >> 1; i >= e; --i)
                    smalls[i] -= c;
            }

            ++pc;
        }

    larges[0] += LL(s + 2 * (pc - 1)) * (s - 1) / 2;

    for (int k = 1; k < s; ++k)
        larges[0] -= larges[k];

    for (int l = 1; l < s; ++l) {
        int q = roughs[l];
        LL M = N / q;
        int e = smalls[half(M / q)] - pc;

        if (e < l + 1)
            break;

        LL t = 0;

        for (int k = l + 1; k <= e; ++k)
            t += smalls[half(divide(M, roughs[k]))];

        larges[0] += t - LL(e - l) * (pc + l - 1);
    }

    return mp[N] = larges[0] + 1;
}

const int mod = 998244353;

int mul(int a, int b){
    return 1LL * a * b % mod;
}

int qpow(int a, LL b){
    int ans = 1;
    while(b){
        if (b & 1) ans = mul(ans, a);
        b >>= 1;
        a = mul(a, a);
    }
    return ans;
}

int main(){

#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif

    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    init();
    LL n;
    cin >> n;

    auto f = [&](LL m){
        if (m == 1) return 1;
        LL cnt = prime_pi(m) - prime_pi(m / 2);
        LL k = 1;
        k += cnt;
        if (m - cnt - 1 > 0) k += 1;
        int ans = qpow(m % mod, k - 2);
        if (m - cnt - 1 > 0) ans = mul(ans, (m - cnt - 1) % mod);
        return ans;
    };

    LL ans = 1;
    for(LL l = 1, r; l <= n; l = r + 1){
        r = n / (n / l);
        ans = mul(ans, qpow(f(n / l), r - l + 1));
    }
    cout << ans << '\n';

}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 37ms
memory: 56724kb

input:

4

output:

8

result:

ok answer is '8'

Test #2:

score: 0
Accepted
time: 32ms
memory: 56916kb

input:

2

output:

1

result:

ok answer is '1'

Test #3:

score: 0
Accepted
time: 39ms
memory: 56720kb

input:

123

output:

671840470

result:

ok answer is '671840470'

Test #4:

score: 0
Accepted
time: 42ms
memory: 56744kb

input:

233

output:

353738465

result:

ok answer is '353738465'

Test #5:

score: 0
Accepted
time: 36ms
memory: 56940kb

input:

5981

output:

970246821

result:

ok answer is '970246821'

Test #6:

score: 0
Accepted
time: 36ms
memory: 56712kb

input:

86422

output:

897815688

result:

ok answer is '897815688'

Test #7:

score: 0
Accepted
time: 36ms
memory: 56748kb

input:

145444

output:

189843901

result:

ok answer is '189843901'

Test #8:

score: 0
Accepted
time: 31ms
memory: 58796kb

input:

901000

output:

819449452

result:

ok answer is '819449452'

Test #9:

score: 0
Accepted
time: 40ms
memory: 56720kb

input:

1000000

output:

113573943

result:

ok answer is '113573943'

Test #10:

score: 0
Accepted
time: 41ms
memory: 56640kb

input:

23333333

output:

949849384

result:

ok answer is '949849384'

Test #11:

score: 0
Accepted
time: 38ms
memory: 56728kb

input:

102850434

output:

604886751

result:

ok answer is '604886751'

Test #12:

score: 0
Accepted
time: 57ms
memory: 57196kb

input:

998244353

output:

0

result:

ok answer is '0'

Test #13:

score: 0
Accepted
time: 44ms
memory: 56964kb

input:

1000000007

output:

318420284

result:

ok answer is '318420284'

Test #14:

score: 0
Accepted
time: 69ms
memory: 56960kb

input:

2147483547

output:

688759898

result:

ok answer is '688759898'

Test #15:

score: 0
Accepted
time: 112ms
memory: 57344kb

input:

5120103302

output:

116870489

result:

ok answer is '116870489'

Test #16:

score: 0
Accepted
time: 309ms
memory: 57676kb

input:

19834593299

output:

523663743

result:

ok answer is '523663743'

Test #17:

score: 0
Accepted
time: 740ms
memory: 57948kb

input:

52500109238

output:

195086665

result:

ok answer is '195086665'

Test #18:

score: 0
Accepted
time: 1152ms
memory: 58512kb

input:

84848352911

output:

107959260

result:

ok answer is '107959260'

Test #19:

score: 0
Accepted
time: 1358ms
memory: 58716kb

input:

99824435322

output:

0

result:

ok answer is '0'

Test #20:

score: 0
Accepted
time: 1352ms
memory: 58936kb

input:

99999999354

output:

316301711

result:

ok answer is '316301711'

Test #21:

score: 0
Accepted
time: 1366ms
memory: 60764kb

input:

100000000000

output:

396843576

result:

ok answer is '396843576'

Extra Test:

score: 0
Extra Test Passed