最后三点WA求助
查看原帖
最后三点WA求助
751866
__DIOsama__楼主2023/12/17 10:20
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <stack>
#include <cctype>
#include <cstring>
#include <cmath>
#include <complex>

#define int long long
#define rep(a,b,c) for(auto (a)=(b);(a)<=(c);(a)++)
#define per(a,b,c) for(auto (a)=(b);(a)>=(c);(a)--)

typedef long long ll;

const int mod = 1e9 + 7, inv2 = 500000004, inv3 = 333333336;
const int N = 1e5 + 5;

int n, Prime[N + 5], num, sp1[N + 5], sp2[N + 5];
int tot = 0;

int w[3 * N], dj1[N * 3], dj2[3 * N], sqr;
int idx1[N + 5], idx2[N + 5];

bool NotPrime[N + 5];

namespace Space {

    auto max = [](auto a, auto b) {return a > b ? a : b ;};
    auto min = [](auto a, auto b) {return a < b ? a : b ;};

    template < typename _Tp >
    inline _Tp read() {
        auto f = 1, a = 0;
        char ch = getchar ();
        while (ch < '0' || ch > '9') {f = (ch == '-' ? -1 : f);ch = getchar ();}
        while (ch >= '0' && ch <= '9') {a = (a << 1) + (a << 3) + (ch ^ '0');ch = getchar ();}
        return{a * f};
    }
}

using namespace Space;

template < typename _Tp , typename _Up>
class Solve {
private:
    inline 
    _Tp  __init__() {
        memset(NotPrime, false, sizeof NotPrime);
        num = 0;
        rep(i,2,N) {
            if (!NotPrime[i]) {
                Prime[++ num] = i;
                sp1[num] = (sp1[num - 1] + i) % mod;
                sp2[num] = (sp2[num - 1] + 1ll * i * i % mod) %mod;
            }
            for (int j = 1; j <= num && Prime[j] * i <= N; j ++ ) {
                NotPrime[i * Prime[j]] = 1;
                if (!(i % Prime[j])) break;
            }
        }
    }
    inline
    _Up s(_Up x, _Up y) {
        if (Prime[y] >= x) return{0};
        int k = x <= sqr ? idx1[x] : idx2[n / x] ;
        int ans = (dj2[k] - dj1[k] + mod - (sp2[y] - sp1[y]) + mod) % mod;
        for (int i = y + 1; i <= num && Prime[i] * Prime[i] <= x; i ++ ) {
            int P = Prime[i];
            for (int e = 1; P <= x; e ++ , P *= Prime[i]) {
                int xx = P % mod;
                ans = (ans + xx * (xx - 1) % mod * (s(x / P, i) + (e != 1))) % mod;
            } 
        }
        return{ans % mod};
    }
public:
    inline
    _Tp sol() {
        __init__();
        n = read<int>();
        sqr = sqrt(n);
        for(int l = 1, r; l <= n; l = r + 1) {
            r = (n / (n / l));
            w[++ tot] = n / l;
            dj1[tot] = w[tot] % mod;
            dj2[tot] = dj1[tot] * (dj1[tot] + 1) / 2 % mod * (2 * dj1[tot]  + 1) % mod * inv3 % mod - 1;
            if (dj2[tot] < 0) dj2[tot] += mod;
            dj1[tot] = dj1[tot] * (dj1[tot] + 1) / 2 % mod - 1;
            if (dj1[tot] < 0) dj1[tot] += mod;
            if (w[tot] <= sqr) idx1[w[tot]] = tot;
            else idx2[n / w[tot]] = tot; 
        }
        rep(i,1,num) {
            for (int j = 1; j <= tot && Prime[i] * Prime[i] <= w[j]; j ++ ) {
                int k = w[j] / Prime[i];
                k = k <= sqr ? idx1[k] : idx2[n / k];
                dj1[j] -= Prime[i] * (dj1[k] - sp1[i - 1] + mod) % mod;
                dj2[j] -= Prime[i] * Prime[i] % mod * (dj2[k] - sp2[i - 1] + mod) % mod;
                dj1[j] %= mod; dj2[j] %= mod;
                if(dj1[j] < 0) dj1[j] += mod;
                if(dj2[j] < 0) dj2[j] += mod;
            }
        }
        std::cout << (s(n ,0) + 1) % mod << '\n';
    }
};

Solve<void, int> T;

signed main() {
    T.sol();
    return 0;
}


2023/12/17 10:20
加载中...