[BZOJ3813][清华集训2014]奇数国(数论+bit)

题目描述

传送门

题解

题意就是求[l,r]所有数的积的 φ
对于每一个数分解质因数,然后存入bit,维护单点修改区间查询
然后求 φ 的时候先对每一个质因子单独求(公式 pkp1p ),然后再合并( φ(ab)=φ(a)φ(b),(a,b)=1
这样时间复杂度应该是 O(n(2logn60+60logn))3.06s ,所以狂卡一波常数。。。

其实更优越的姿势应该是用二进制位记录质因子是否出现,这样合并的时候直接做位运算就可以了。不过就不能写bit了(无法前缀和加减)

代码

#include
#include
#include
#include
#include
using namespace std;
#define LL long long
#define Mod 19961993
#define N 100005

int n=100000;
int prime[61]={0,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281};
int num[282]={0,0,1,2,0,3,0,4,0,0,0,5,0,6,0,0,0,7,0,8,0,0,0,9,0,0,0,0,0,10,0,11,0,0,0,0,0,12,0,0,0,13,0,14,0,0,0,15,0,0,0,0,0,16,0,0,0,0,0,17,0,18,0,0,0,0,0,19,0,0,0,20,0,21,0,0,0,0,0,22,0,0,0,23,0,0,0,0,0,24,0,0,0,0,0,0,0,25,0,0,0,26,0,27,0,0,0,28,0,29,0,0,0,30,0,0,0,0,0,0,0,0,0,0,0,0,0,31,0,0,0,32,0,0,0,0,0,33,0,34,0,0,0,0,0,0,0,0,0,35,0,36,0,0,0,0,0,37,0,0,0,0,0,38,0,0,0,39,0,0,0,0,0,40,0,0,0,0,0,41,0,42,0,0,0,0,0,0,0,0,0,43,0,44,0,0,0,45,0,46,0,0,0,0,0,0,0,0,0,0,0,47,0,0,0,0,0,0,0,0,0,0,0,48,0,0,0,49,0,50,0,0,0,51,0,0,0,0,0,52,0,53,0,0,0,0,0,0,0,0,0,54,0,0,0,0,0,55,0,0,0,0,0,56,0,0,0,0,0,57,0,58,0,0,0,0,0,59,0,0,0,60};
LL inv[300];
struct data{int a[65];};
data operator += (data &a,data b)
{
    for (int i=1;i<=60;++i)
        a.a[i]+=b.a[i];
    return a;
}
data operator - (data a,data b)
{
    for (int i=1;i<=60;++i)
        a.a[i]-=b.a[i];
    return a;
}
data operator * (data a,int b)
{
    for (int i=1;i<=60;++i)
        a.a[i]*=b;
    return a;
}
int T,opt,x,y;
data val[N],C[N];

data calc(int x)
{
    data ans;memset(ans.a,0,sizeof(ans.a));
    for (int i=1;i<=60&&x>0&&prime[i]*prime[i]<=x;++i)
        if (x%prime[i]==0)
        {
            while (x%prime[i]==0)
            {
                ++ans.a[i];
                x/=prime[i];
            }
        }
    if (x>0) ++ans.a[num[x]];
    return ans;
}
void init(int loc)
{
    for (int i=loc;i<=n;i+=i&(-i))
        ++C[i].a[2];
}
void add(int loc,data a,int val)
{
    for (int i=loc;i<=n;i+=i&-i)
        C[i]+=a*val;
}
data query(int loc)
{
    data ans;memset(ans.a,0,sizeof(ans.a));
    for (int i=loc;i>=1;i-=i&-i)
        ans+=C[i];
    return ans;

}
LL fast_pow(LL a,int p)
{
    LL ans=1LL;
    for (;p;p>>=1,a=a*a%Mod)
        if (p&1)
            ans=ans*a%Mod;
    return ans;
}
LL phi(int x,int p)
{
    LL ans=fast_pow(x,p);
    ans=ans*(x-1)%Mod*inv[x]%Mod;
    return ans;
}

int main()
{
    for (int i=1;i<=n;++i) val[i].a[2]=1,init(i);
    for (int i=1;i<=60;++i) inv[prime[i]]=fast_pow(prime[i],Mod-2);

    scanf("%d",&T);
    while (T--)
    {
        scanf("%d",&opt);
        scanf("%d%d",&x,&y);
        if (opt==0)
        {
            data now=query(y)-query(x-1);
            LL ans=1LL;
            for (int i=1;i<=60;++i)
                if (now.a[i]) ans=ans*phi(prime[i],now.a[i])%Mod;
            printf("%lld\n",ans);
        }
        else
        {
            add(x,val[x],-1);
            val[x]=calc(y);
            add(x,val[x],1);
        }
    }
}

你可能感兴趣的:(题解,bit,数论)