QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#798056#8761. 另一个计数问题lengliAC ✓986ms30980kbC++235.6kb2024-12-04 00:40:212024-12-04 00:40:22

Judging History

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

  • [2024-12-04 00:40:22]
  • 评测
  • 测评结果:AC
  • 用时:986ms
  • 内存:30980kb
  • [2024-12-04 00:40:21]
  • 提交

answer

/*
lengli_QAQ
Hope there are no bugs!!!
*/
#include <bits/stdc++.h>
#define fastio std::ios::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0)
#define all(x) x.begin(),x.end()
#define pb push_back

template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  bool operator<(const ModInt &a) const { return (x < a.x); }
  bool operator>(const ModInt &a) const { return (x > a.x); }
  bool operator<=(const ModInt &a) const { return (x <= a.x); }
  bool operator>=(const ModInt &a) const { return (x >= a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
  friend std::istream &operator>>(std::istream &is, ModInt &a) {int v;is >> v;a = ModInt(v);return is;}
};
constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;

#define i64 long long

constexpr int N=2000010;

namespace min25{
    i64 n,idx,D;
    i64 w[N],id1[N],id2[N];
    i64 prime[N],st[N],cnt;
    std::vector<std::vector<Mint>> g,sum;

    i64 id(i64 x){
        if(x<=D) return id1[x];
        return id2[n/x];
    }

    void init(i64 nn){
        n=nn;
        g.clear(),sum.clear();
        idx=0,cnt=0;
        D=std::sqrt(n);

        for(i64 l=1,r;l<=n;l=r+1){
            r=n/(n/l),w[++idx]=n/l;
            if(w[idx]<=D) id1[w[idx]]=idx;
            else id2[n/w[idx]]=idx;
        }

        for(i64 i=2;i<=D;i++){
            if(!st[i]) prime[++cnt]=i;
            for(i64 j=1;prime[j]*i<=D;j++){
                st[i*prime[j]]=1;
                if(i%prime[j]==0) break;
            }
        }
    }

    void insert(std::function<Mint(i64)> f,std::function<Mint(i64)> presum){
        std::vector<Mint> ng(idx+2),nsum(cnt+2);
        for(int i=1;i<=idx;i++) ng[i]=presum(w[i])-1;
        for(int i=1;i<=cnt;i++) nsum[i]=(nsum[i-1]+f(prime[i]));
        for(int i=1;i<=cnt;i++){
            for(int j=1;j<=idx and prime[i]*prime[i]<=w[j];j++){
                ng[j]=ng[j]-f(prime[i])*(ng[id(w[j]/prime[i])]-nsum[i-1]);
            }
        }
        g.pb(ng),sum.pb(nsum);
    }

    Mint jxf(i64 x){
        return Mint(x)*x-x;
    }

    Mint S(i64 x,int j){
        if(prime[j]>x) return 0;
        Mint res=(g[0][id(x)]-sum[0][j])-(g[1][id(x)]-sum[1][j]);
        for(int i=j+1;i<=cnt and prime[i]*prime[i]<=x;i++){
            for(i64 e=1,sp=prime[i];sp<=x;sp*=prime[i],e++){
                res+=jxf(sp)*(S(x/sp,i)+(e>1));
            }
        }
        return res;
    }
}

void solve(){
    i64 n;
    std::cin>>n;
    Mint sum=Mint(n-1)*(n+2)/2;
    Mint res=sum*sum;
    res-=Mint(2*n+1)*(n+1)*n/6;
    res+=1;
    res/=2;

    Mint ans=0;
    Mint d=0;

    {
        std::function<Mint(i64)> f=[&](i64 x){
            return x;
        };
        std::function<Mint(i64)> pre=[&](i64 x){
            return Mint(x+1)*x/2;
        };
        min25::init(n);
        min25::insert(f,pre);
        Mint t=min25::g[0][1]-min25::g[0][min25::id(n/2)];

        d=t*t;
        res-=t*sum;
    }

    {
        std::function<Mint(i64)> f=[&](i64 x){
            return Mint(x)*x;
        };
        std::function<Mint(i64)> pre=[&](i64 x){
            return Mint(2*x+1)*Mint(x+1)*x/6;
        };
        min25::init(n);
        min25::insert(f,pre);
        Mint t=min25::g[0][1]-min25::g[0][min25::id(n/2)];
        d+=t;
        d/=2;
        res+=d;
    }

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

signed main(){
    fastio;
    
    int T;
    T=1;
    while(T--) solve();
    
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 12004kb

input:

4

output:

8

result:

ok 1 number(s): "8"

Test #2:

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

input:

5

output:

8

result:

ok 1 number(s): "8"

Test #3:

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

input:

6

output:

80

result:

ok 1 number(s): "80"

Test #4:

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

input:

7

output:

80

result:

ok 1 number(s): "80"

Test #5:

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

input:

8

output:

200

result:

ok 1 number(s): "200"

Test #6:

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

input:

9

output:

407

result:

ok 1 number(s): "407"

Test #7:

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

input:

10

output:

937

result:

ok 1 number(s): "937"

Test #8:

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

input:

79

output:

3224298

result:

ok 1 number(s): "3224298"

Test #9:

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

input:

123

output:

21077222

result:

ok 1 number(s): "21077222"

Test #10:

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

input:

158

output:

57411585

result:

ok 1 number(s): "57411585"

Test #11:

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

input:

285

output:

605750829

result:

ok 1 number(s): "605750829"

Test #12:

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

input:

355

output:

509863120

result:

ok 1 number(s): "509863120"

Test #13:

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

input:

484

output:

311440260

result:

ok 1 number(s): "311440260"

Test #14:

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

input:

520

output:

102191845

result:

ok 1 number(s): "102191845"

Test #15:

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

input:

706

output:

300787918

result:

ok 1 number(s): "300787918"

Test #16:

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

input:

747

output:

505062591

result:

ok 1 number(s): "505062591"

Test #17:

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

input:

784

output:

181810798

result:

ok 1 number(s): "181810798"

Test #18:

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

input:

76879

output:

716166793

result:

ok 1 number(s): "716166793"

Test #19:

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

input:

209295

output:

753032272

result:

ok 1 number(s): "753032272"

Test #20:

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

input:

220895

output:

874612082

result:

ok 1 number(s): "874612082"

Test #21:

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

input:

243390

output:

68635874

result:

ok 1 number(s): "68635874"

Test #22:

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

input:

414767

output:

862578797

result:

ok 1 number(s): "862578797"

Test #23:

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

input:

431662

output:

231728766

result:

ok 1 number(s): "231728766"

Test #24:

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

input:

521130

output:

106207351

result:

ok 1 number(s): "106207351"

Test #25:

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

input:

668419

output:

580625063

result:

ok 1 number(s): "580625063"

Test #26:

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

input:

700378

output:

790849562

result:

ok 1 number(s): "790849562"

Test #27:

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

input:

965876

output:

856082142

result:

ok 1 number(s): "856082142"

Test #28:

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

input:

998244350

output:

539142456

result:

ok 1 number(s): "539142456"

Test #29:

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

input:

998244351

output:

730264865

result:

ok 1 number(s): "730264865"

Test #30:

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

input:

998244352

output:

326703895

result:

ok 1 number(s): "326703895"

Test #31:

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

input:

998244353

output:

326703895

result:

ok 1 number(s): "326703895"

Test #32:

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

input:

998244354

output:

730264864

result:

ok 1 number(s): "730264864"

Test #33:

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

input:

998244355

output:

539142451

result:

ok 1 number(s): "539142451"

Test #34:

score: 0
Accepted
time: 37ms
memory: 14280kb

input:

998244356

output:

751581014

result:

ok 1 number(s): "751581014"

Test #35:

score: 0
Accepted
time: 68ms
memory: 14648kb

input:

2165916141

output:

216013547

result:

ok 1 number(s): "216013547"

Test #36:

score: 0
Accepted
time: 96ms
memory: 14836kb

input:

3550627266

output:

318019384

result:

ok 1 number(s): "318019384"

Test #37:

score: 0
Accepted
time: 215ms
memory: 18184kb

input:

11640239920

output:

137498099

result:

ok 1 number(s): "137498099"

Test #38:

score: 0
Accepted
time: 272ms
memory: 19692kb

input:

16191777349

output:

991399721

result:

ok 1 number(s): "991399721"

Test #39:

score: 0
Accepted
time: 428ms
memory: 23448kb

input:

31326230483

output:

99981147

result:

ok 1 number(s): "99981147"

Test #40:

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

input:

32810385543

output:

284259680

result:

ok 1 number(s): "284259680"

Test #41:

score: 0
Accepted
time: 484ms
memory: 23424kb

input:

37368395332

output:

511468046

result:

ok 1 number(s): "511468046"

Test #42:

score: 0
Accepted
time: 512ms
memory: 22536kb

input:

40002331093

output:

282851705

result:

ok 1 number(s): "282851705"

Test #43:

score: 0
Accepted
time: 850ms
memory: 29756kb

input:

82884464396

output:

767050832

result:

ok 1 number(s): "767050832"

Test #44:

score: 0
Accepted
time: 951ms
memory: 28816kb

input:

96506992785

output:

31413975

result:

ok 1 number(s): "31413975"

Test #45:

score: 0
Accepted
time: 973ms
memory: 30980kb

input:

99999999995

output:

456189842

result:

ok 1 number(s): "456189842"

Test #46:

score: 0
Accepted
time: 986ms
memory: 29436kb

input:

99999999996

output:

516138273

result:

ok 1 number(s): "516138273"

Test #47:

score: 0
Accepted
time: 980ms
memory: 29096kb

input:

99999999997

output:

136420410

result:

ok 1 number(s): "136420410"

Test #48:

score: 0
Accepted
time: 971ms
memory: 29036kb

input:

99999999998

output:

841974696

result:

ok 1 number(s): "841974696"

Test #49:

score: 0
Accepted
time: 971ms
memory: 30604kb

input:

99999999999

output:

164762165

result:

ok 1 number(s): "164762165"

Test #50:

score: 0
Accepted
time: 978ms
memory: 30616kb

input:

100000000000

output:

627965619

result:

ok 1 number(s): "627965619"