[MtOI2019] Ghost Orchestra

topic

A very violent practice of spicy chicken

Considering the two numbers \ (\ GCD \) is the mass of all power take several \ (\ min \) , two numbers \ (\ rm lcm \) is the mass of all power take several \ (\ max \) , then the final answer must be \ (\ Prod P_i C_i} ^ {\) , and where the largest prime number does not exceed \ (the n-\) , so we consider the calculated power of each prime number is how many

So we will be successful \ (\ prod \) converted to \ (\ SUM \) , was right on the index \ (\ rm mod-1 \ ) modulo

For \ (\ RM of the type = 0 \) , we enumerate a prime number \ (the p-\) , calculate its power

That is

\[\sum_{i=0}\sum_{j=0}\sum_{k=0}\sum_{t_1=1}^{\left \lfloor \frac{A}{p^i} \right \rfloor}\sum_{t_2=1}^{\left \lfloor \frac{B}{p^j} \right \rfloor}\sum_{t_3=1}^{\left \lfloor \frac{C}{p^k} \right \rfloor}\max(i,j)-\min(i,k)[t_1\perp p][t_2\perp p][t_3\perp p]\]

This is clear ah, what we enumerate the current maximum power of the prime numbers, after this enumeration has the largest number of power, that is, \ (the p-i ^ \ Times t_1 \) , but if \ (t_1 \) and \ (p \) are not relatively prime, then \ (i \) is not \ (p ^ i \ times t \) of maximum power, so also have to ensure that \ (t_1 \ perp p \)

We have to change is the order of summation

\[\sum_{i=0}\sum_{j=0}\sum_{k=0}\max(i,j)-\min(i,k)\sum_{t_1=1}^{\left \lfloor \frac{A}{p^i} \right \rfloor}[t_1\perp p]\sum_{t_2=1}^{\left \lfloor \frac{B}{p^j} \right \rfloor}[t_2\perp p]\sum_{t_3=1}^{\left \lfloor \frac{C}{p^k} \right \rfloor}[t_3\perp p]\]

Also found demand \ (\ max (i, j ) \) and \ (\ min (i, k ) \) is quite independent, so we can be forced down into

\[C\times \sum_{i=0}\sum_{j=0}\max(i,j)\sum_{t_1=1}^{\left \lfloor \frac{A}{p^i} \right \rfloor}[t_1\perp p]\sum_{t_2=1}^{\left \lfloor \frac{B}{p^j} \right \rfloor}[t_2\perp p]\]

minus

\[B\times \sum_{i=0}\sum_{k=0}\min(i,k)\sum_{t_1=1}^{\left \lfloor \frac{A}{p^i} \right \rfloor}[t_1\perp p]\sum_{t_3=1}^{\left \lfloor \frac{C}{p^k} \right \rfloor}[t_3\perp p]\]

Now think about the \ (\ sum_ {t_1 = 1 } ^ {\ left \ lfloor \ frac {A} {p ^ i} \ right \ rfloor} [t_1 \ perp p] \) in the end how demand, and a prime number mutual quality if not the multiple of a prime number on the line, so we simply subtracting \ (1 \) to \ (\ left \ lfloor \ frac {a} {p ^ i} \ right \ rfloor \) where \ (P \) to multiple, i.e. \ (\ left \ lfloor \ frac {a} {p ^ i} \ right \ rfloor- \ left \ lfloor \ frac {a} {p ^ {i + 1}} \ right \ rfloor \)

That is, we now only required

\[B\times \sum_{i=0}\sum_{j=0}\max(i,j)(\left \lfloor \frac{A}{p^i} \right \rfloor-\left \lfloor \frac{A}{p^{i+1}} \right \rfloor)(\left \lfloor \frac{B}{p^j} \right \rfloor-\left \lfloor \frac{B}{p^{j+1}} \right \rfloor)\]

We can directly \ (O (\ log_p ^ 2 A) \) to calculate the persimmon, but we deal with it \ (\ left \ lfloor \ frac {A} {p ^ i} \ right \ rfloor- \ left \ lfloor \ frac {a} {p ^ { i + 1}} \ right \ rfloor \) prefix and discuss force can do \ (O (\ log_p a) \)

A to \ (\ n-) primes within about (\ frac {n} {\ ln n} \) \ a, is calculated for each prime number requires \ (O (\ log_p n) \) complexity, so do again complexity is probably \ (O (n) \) of

For \ (\ RM of the type = 1 \) , persimmon became

\[\sum_{i=0}\sum_{j=0}\sum_{k=0}\sum_{t_1=1}^{\left \lfloor \frac{A}{p^i} \right \rfloor}\sum_{t_2=1}^{\left \lfloor \frac{B}{p^j} \right \rfloor}\sum_{t_3=1}^{\left \lfloor \frac{C}{p^k} \right \rfloor}(\max(i,j)-\min(i,k))\times t_1\times t_2\times t_3\times p^{i+j+k}[t_1\perp p][t_2\perp p][t_3\perp p]\]

Engaging force can become a form of the above, it can also be in \ (O (n) \) seek out time

For \ (\ RM of the type = 2 \) , we consider the enumeration above \ (\ gcd \)

Set \ (f (A, B, C) \) to answer the first question, that is, three were taken to a superscript \ (A, B, C \ ) Contribution at

Routine enumeration \ (\ gcd \) to \ (d \) that is \ (d \) contribution at multiples

\[\sum_{d=1}^{min(A,B,C)}d\sum_{d|i}\mu(\frac{i}{d})f(\left \lfloor \frac{A}{d} \right \rfloor,\left \lfloor \frac{B}{d}\right \rfloor,\left \lfloor \frac{C}{d} \right \rfloor)\]

Is \ (\ left \ lfloor \ frac {A} {d} \ right \ rfloor, \ left \ lfloor \ frac {B} {d} \ right \ rfloor, \ left \ lfloor \ frac {C} {d} \ right \) rfloor \ number multiplied by the \ (D \) respectively, does not exceed \ (a, B, C \) , and these numbers \ (\ GCD \) are \ (D \) or \ (D \) multiples

And then summed to exchange routine order

\[\sum_{i=1}^{\min(A,B,C)}f(\left \lfloor \frac{A}{i} \right \rfloor,\left \lfloor \frac{B}{i}\right \rfloor,\left \lfloor \frac{C}{i} \right \rfloor)\sum_{d|i}\mu(\frac{i}{d})d=\sum_{i=1}^{\min(A,B,C)}f(\left \lfloor \frac{A}{i} \right \rfloor,\left \lfloor \frac{B}{i}\right \rfloor,\left \lfloor \frac{C}{i} \right \rfloor)\varphi(i)\]

Direct block divisible so vigorously, complexity should not exceed \ (O (n \ ln n ) \)

It.

Please always vigorously card

Code

#include<bits/stdc++.h>
#define re register
#define LL long long
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#pragma GCC optimize(3)
#pragma GCC optimize("-fcse-skip-blocks")
const int maxn=1e5+5;
int mod,P,T,R;
int is[maxn],p[maxn>>1],pre[maxn],phi[maxn],pw[maxn>>1];
int r[2],tax[2][20],pr[2][20];
inline int ksm(int a,int b) {
    int S=1;
    for(;b;b>>=1,a=1ll*a*a%mod) if(b&1) S=1ll*S*a%mod;
    return S;
}
inline int qm(int x) {return x>=P?x-P:x;}
inline int dqm(int x) {return x+=x>>31&P;}
inline int calc(int n,int p) {
    return dqm(pre[n]-1ll*pre[n/p]*p%P);
}
inline int out(int A) {
    int ans=1;
    for(re int i=1;i<=p[0]&&p[i]<=A;i++) 
        ans=1ll*ans*ksm(p[i],pw[i])%mod;
    return ans;
}
inline void clear(int A) {
    for(re int i=1;i<=p[0]&&p[i]<=A;i++) pw[i]=0;
}
inline void Solve(int A,int B,int C,int v) {
    int ans1=1;
    for(re int t=1;t<=p[0];t++) {
        if(p[t]>A&&p[t]>B&&p[t]>C) break;
        int now=0,tot=0;
        for(re int i=0,aa=A;aa;i++,aa/=p[t]) pr[0][i]=tax[0][i]=aa-aa/p[t],r[0]=i;
        for(re int j=0,bb=B;bb;j++,bb/=p[t]) pr[1][j]=tax[1][j]=bb-bb/p[t],r[1]=j;
        R=max(r[0],r[1]);
        for(re int i=1;i<=R;i++) pr[0][i]=qm(pr[0][i]+pr[0][i-1]);
        for(re int j=1;j<=R;j++) pr[1][j]=qm(pr[1][j]+pr[1][j-1]);
        for(re int i=0;i<=r[0];i++) 
            now=qm(now+1ll*tax[0][i]*pr[1][i]*i%P);
        for(re int j=1;j<=r[1];j++) 
            now=qm(now+1ll*tax[1][j]*pr[0][j-1]*j%P);
        for(re int i=1;i<=R;i++) pr[1][i]=0;
        for(re int i=r[0]+1;i<=R;i++) pr[0][i]=0;   
        now=1ll*now*C%P;
        for(re int k=0,cc=C;cc;k++,cc/=p[t]) pr[1][k]=tax[1][k]=cc-cc/p[t],r[1]=k;
        R=max(r[0],r[1]);
        for(re int i=r[0]+1;i<=R;i++) pr[0][i]=qm(pr[0][i-1]+pr[0][i]);
        for(re int k=1;k<=R;k++) pr[1][k]=qm(pr[1][k]+pr[1][k-1]);
        for(re int i=1;i<=r[0];i++) 
            tot=qm(tot+1ll*i*tax[0][i]*dqm(pr[1][r[1]]-pr[1][i-1])%P);
        for(re int k=1;k<=r[1];k++)
            tot=qm(tot+1ll*k*tax[1][k]*dqm(pr[0][r[0]]-pr[0][k])%P);
        for(re int i=0;i<=R;i++) pr[1][i]=pr[0][i]=0;
        now=dqm(now-1ll*B*tot%P);
        pw[t]=qm(pw[t]+1ll*now*v%P);
    }
}
inline int Calc(int n,int m,int h) {
    int U=min(min(n,m),h);
    for(re int l=2,r;l<=U;l=r+1) {
        r=min(n/(n/l),m/(m/l));
        r=min(r,h/(h/l));
        Solve(n/l,m/l,h/l,dqm(phi[r]-phi[l-1]));
    }
    return out(max(max(n,m),h));
}
int A,B,C;
int main() {
    scanf("%d%d",&T,&mod);P=mod-1;phi[1]=1;
    for(re int i=2;i<maxn;i++) {
        if(!is[i]) p[++p[0]]=i,phi[i]=i-1;
        for(re int j=1;j<=p[0]&&p[j]*i<maxn;j++) {
            is[p[j]*i]=1;if(i%p[j]==0) {
                phi[p[j]*i]=phi[i]*p[j];break;
            }
            phi[p[j]*i]=phi[i]*phi[p[j]];
        }
    }
    for(re int i=1;i<maxn;i++) 
        pre[i]=qm(pre[i-1]+i),phi[i]=qm(phi[i-1]+phi[i]);
    while(T--) {
        scanf("%d%d%d",&A,&B,&C);
        int ans2=1;
        for(re int t=1;t<=p[0];t++) {
            if(p[t]>A&&p[t]>B&&p[t]>C) break;
            int now=0,tot=0;
            for(re LL i=0,a=1;a<=A;i++,a*=p[t])
                for(re LL j=0,b=1;b<=B;j++,b*=p[t]) {
                    int v=1ll*max(i,j)*a*b%P;
                    tot=qm(tot+1ll*v*calc(A/a,p[t])*calc(B/b,p[t])%P*pre[C]%P);
            }
            for(re LL i=1,a=p[t];a<=A;i++,a*=p[t])
                for(re LL k=1,c=p[t];c<=C;k++,c*=p[t]) {
                    int v=1ll*min(i,k)*a*c%P;
                    now=qm(now+1ll*v*calc(A/a,p[t])*calc(C/c,p[t])%P*pre[B]%P);
            }
            ans2=1ll*ans2*ksm(p[t],dqm(tot-now))%mod;
        }
        Solve(A,B,C,1);printf("%d ",out(max(max(A,B),C)));
        printf("%d %d\n",ans2,Calc(A,B,C));clear(max(max(A,B),C));
    }
}

Guess you like

Origin www.cnblogs.com/asuldb/p/11447841.html