[BZOJ3529] [SDOI2014] number table (Mobius inversion)

Click here to see the problem surface

Generally meaning of the questions: specifying a \ (n * m \) number of each table number \ (\ sum_ {D | I, D | D J} \) , find the number in the table is not larger than \ (A \) number Sum.

Without considering restrictions

Let us not consider limiting to push the wave equation.

First, the table easy to know the number of \ (I \) row \ (J \) the number of columns should be \ (\ Sigma (GCD (I, J)) \) .

And it is as follows:

\[\sum_{d=1}^{min(n,m)}\sigma(d)\sum_{i=1}^{\lfloor\frac nd\rfloor}\sum_{j=1}^{\lfloor\frac md\rfloor}[gcd(i,j)=1]\]

And \ ([gcd (i, j ) = 1] \) can be turned into \ (\ sum_ {P | GCD (I, J)} \ MU (P) \) , if the enumeration \ (P \) , to give :

\[\sum_{d=1}^{min(n,m)}\sigma(d)\sum_{p=1}^{\lfloor\frac{min(n,m)}d\rfloor}\mu(p)\lfloor\frac n{dp}\rfloor\lfloor\frac m{dp}\rfloor\]

Set \ (DP G = \) , adjusted to give the order enumerated:

\[\sum_{g=1}^{min(n,m)}\lfloor\frac n{dp}\rfloor\lfloor\frac m{dp}\rfloor\sum_{d|g}\sigma(d)\mu(\frac gd)\]

Offline processing restrictions

Consider the above formula only if \ (\ sigma (d) \ le a \) when the answer will be counted.

We consider the set \ (T (G) = \ sum_ {D | G} \ Sigma (D) \ MU (\ FRAC Gd) \) , are all started \ (0 \) .

Then we follow (A \) \ ascending enumeration asked each time the \ (\ sigma (d) \ le a \) a \ (D \) in \ (10 ^ 5 \) factor in the range corresponding to the the \ (T (g) \) are all plus \ (\ Sigma (D) \ MU (\ FRAC Gd) \) .

But using division block Noting inquiry request for a section \ (T \) value and then we use Fenwick tree maintenance on it.

Details on modulo

Note that this is the modulo \ (2 ^ {31} \) modulo, then we can consider open \ (unsigned \ int \) is calculated answer, then finally to its \ (2 ^ {31} \) modulo

Code

#include<bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int&
#define I inline
#define W while
#define N 100000
#define Q 20000
#define MxS 500000
#define UI unsigned
#define RU Reg unsigned
#define CU Con unsigned&
#define LL long long
#define Gmax(x,y) (x<(y)&&(x=(y)))
#define min(x,y) ((x)<(y)?(x):(y))
#define max(x,y) ((x)>(y)?(x):(y))
#define pb push_back
#define IT vector<int>::iterator
using namespace std;
int Qt,Qans[Q+5];vector<int> s[MxS+5];
struct Query//询问
{
    int x,y,v,pos;I Query(CI a=0,CI b=0,CI z=0,CI p=0):x(a),y(b),v(z),pos(p){}
    I bool operator < (Con Query& o) Con {return v<o.v;}
}q[Q+5];
class FastIO
{
    private:
        #define FS 100000
        #define tc() (A==B&&(B=(A=FI)+fread(FI,1,FS,stdin),A==B)?EOF:*A++)
        #define pc(c) (C==E&&(clear(),0),*C++=c)
        #define tn (x<<3)+(x<<1)
        #define D isdigit(c=tc())
        int T;char c,*A,*B,*C,*E,FI[FS],FO[FS],S[FS];
    public:
        I FastIO() {A=B=FI,C=FO,E=FO+FS;}
        Tp I void read(Ty& x) {x=0;W(!D);W(x=tn+(c&15),D);}
        Ts I void read(Ty& x,Ar&... y) {read(x),read(y...);}
        Tp I void write(Ty x) {W(S[++T]=x%10+48,x/=10);W(T) pc(S[T--]);}
        Tp I void writeln(Con Ty& x) {write(x),pc('\n');}
        I void clear() {fwrite(FO,1,C-FO,stdout),C=FO;}
}F;
class LinearSiever//线性筛
{
    private:
        int Pt,P[N+5],Mn[N+5];
        I LL Qpow(LL x,LL y) {LL t=1;W(y) y&1&&(t*=x),x*=x,y>>=1;return t;}//快速幂
    public:
        int MxSigma,sigma[N+5],mu[N+5];
        I void Sieve(CI S)
        {
            RI i,j,x,t;for(mu[1]=1,i=2;i<=S;++i)//筛mu,筛最小质因数用于求sigma
            {
                !Mn[i]&&(mu[P[++Pt]=i]=-1,Mn[i]=i);
                for(j=1;j<=Pt&&1LL*i*P[j]<=S;++j)
                    if(Mn[i*P[j]]=P[j],i%P[j]) mu[i*P[j]]=-mu[i];else break;
            }
            for(sigma[1]=1,i=2;i<=S;++i)//求sigma
            {
                x=i,t=0;W(!(x%Mn[i])) x/=Mn[i],++t;
                sigma[i]=sigma[x]*((Qpow(Mn[i],t+1)-1)/(Mn[i]-1)),Gmax(MxSigma,sigma[i]);
            }
        }
}L;
class TreeArray//树状数组
{
    private:
        #define lowbit(x) (x&-x)
        UI v[MxS+5];
        I UI QS(RI x) {RU t=0;W(x) t+=v[x],x-=lowbit(x);return t;}//询问前缀
    public:
        I void Add(RI x,CI y) {W(x<=L.MxSigma) v[x]+=y,x+=lowbit(x);}//单点修改
        I UI Qry(CI l,CI r) {return QS(r)-QS(l-1);}//区间查询
}T;
I void Upt(CI x,CI v) {for(RI i=1;1LL*x*i<=N;++i) T.Add(x*i,L.sigma[x]*L.mu[i]);}//更新一个数倍数的值
int main()
{
    RI i,p=1,t,x,y,v,l,r;UI ans;for(L.Sieve(N),i=1;i<=N;++i) s[L.sigma[i]].pb(i);//用桶对sigma值进行排序
    for(F.read(Qt),i=1;i<=Qt;++i) F.read(x,y,v),q[i]=Query(min(x,y),max(x,y),v,i);//读入询问
    for(sort(q+1,q+Qt+1),i=1;i<=Qt;++i)//对询问按a从小到大排序
    {
        W(p<=q[i].v) {for(IT it=s[p].begin();it!=s[p].end();++it) Upt(*it,p);++p;}//更新sigma(d)≤a的d的倍数的T值
        for(ans=0,t=min(q[i].x,q[i].y),l=1;l<=t;l=r+1)//除法分块
            r=min(q[i].x/(q[i].x/l),q[i].y/(q[i].y/l)),ans+=T.Qry(l,r)*(q[i].x/l)*(q[i].y/l);
        Qans[q[i].pos]=ans%(1LL<<31);//存储答案并取模
    }
    for(i=1;i<=Qt;++i) F.writeln(Qans[i]);return F.clear(),0;//输出答案
}

Guess you like

Origin www.cnblogs.com/chenxiaoran666/p/BZOJ3529.html