题解:P17766 「LDOI R2」位运算 / Bit

· · 题解

考虑数据点分治。

当 n\leq 120 的时候,可以直接贪心的,分 4 种情况讨论,有决策的时候优先加小的。建议先写暴力,有一堆特判,少一个都爆 0。

会了上面的暴力后,对于 n 很大的情况,相当于每次 delta 直接加上 2^i,然后有 1\sim 2 个数会减去 2^i。维护出这些数。其他的直接快速幂。注意优化排序,因为 5\times 10^5 组 120\log 120 过不了。可以用类归并排序把这玩意优化掉。然后就做完了。

时间复杂度:令数据点分治的阈值是 B,则复杂度为 \mathcal O(BT\log V),常数比较大。

#include<bits/stdc++.h>
using namespace std;
/*

*/
struct FSI{
    template<typename T>
    FSI& operator >> (T &res){
        res=0;T f=1;char ch=getchar();
        while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
        while (isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
        res*=f;
        return *this;
    }
}scan;
#define int long long
const int N=1e4+10,L=60,mod=998244853;
int T,n,i,j,a[N],pos;
int x,y,z,ans,k;
int delta;
int t[N],b[N],c[N],c1,c2,p1,p2,cnt;
int qp(int a,int b)
{
    int res=1;
    while (b)
    {
        if (b&1) res=res*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return res;
}
void work()
{
    scan>>n>>x>>y>>z;
    if (n==1)
    {
        if (x!=y||y!=z||x!=z){puts("-1");return;}
        printf("%lld\n",x%mod);
        return;
    }
    if (n<=120)
    {
        for (i=1;i<=n;i++) a[i]=0;
        for (i=L-1;i>=0;i--)
        {
            if ((x>>i)&1)
            {
                if (!((y>>i)&1)){puts("-1");return;}
                if (((z>>i)&1)!=n%2){puts("-1");return;}
                for (j=1;j<=n;j++) a[j]+=(1ll<<i);
            }
            else
            {
                if (!((y>>i)&1))
                {
                    if ((z>>i)&1){puts("-1");return;}
                }
                else
                {
                    if ((z>>i)&1)
                    {
                        for (j=1;j<=n/2*2-1;j++) a[j]+=(1ll<<i);
                        c1=c2=0;
                        for (j=1;j<=n/2*2-1;j++) b[++c1]=a[j];
                        for (j=n/2*2;j<=n;j++) c[++c2]=a[j];
                        p1=1;p2=1;
                        cnt=0;
                        while (p1<=c1&&p2<=c2)
                        {
                            if (b[p1]<c[p2]) a[++cnt]=b[p1],p1++;
                            else a[++cnt]=c[p2],p2++;
                        }
                        while (p1<=c1) a[++cnt]=b[p1],p1++;
                        while (p2<=c2) a[++cnt]=c[p2],p2++;
                    }
                    else
                    {
                        if (n==2){puts("-1");return;}
                        for (j=1;j<=(n-1)/2*2;j++) a[j]+=(1ll<<i);
                        c1=c2=0;
                        for (j=1;j<=(n-1)/2*2;j++) b[++c1]=a[j];
                        for (j=(n-1)/2*2+1;j<=n;j++) c[++c2]=a[j];
                        p1=1;p2=1;
                        cnt=0;
                        while (p1<=c1&&p2<=c2)
                        {
                            if (b[p1]<c[p2]) a[++cnt]=b[p1],p1++;
                            else a[++cnt]=c[p2],p2++;
                        }
                        while (p1<=c1) a[++cnt]=b[p1],p1++;
                        while (p2<=c2) a[++cnt]=c[p2],p2++;
                    }
                }
            }
        }
        for (i=1;i<=n;i++) a[i]%=mod;
        ans=1;
        for (i=1;i<=n;i++) ans=ans*a[i]%mod;
        printf("%lld\n",ans);
        return;
    }
    delta=0;
    cnt=0;
    for (i=L-1;i>=0;i--)
    {
        if ((x>>i)&1)
        {
            if (!((y>>i)&1)){puts("-1");return;}
            if (((z>>i)&1)!=n%2){puts("-1");return;}
            delta+=(1ll<<i);
        }
        else
        {
            if (!((y>>i)&1))
            {
                if ((z>>i)&1){puts("-1");return;}
            }
            else
            {
                if ((z>>i)&1)
                {
                    delta+=(1ll<<i);
                    for (j=1;j<=n-(n/2*2-1);j++) t[++cnt]=(1ll<<i);
                }
                else
                {
                    if (n==2){puts("-1");return;}
                    delta+=(1ll<<i);
                    for (j=1;j<=n-(n-1)/2*2;j++) t[++cnt]=(1ll<<i);
                }
            }
        }
    }
    ans=qp(delta%mod,n-cnt);
    for (i=1;i<=cnt;i++) ans=ans*((delta-t[i])%mod)%mod;
    printf("%lld\n",ans);
}
signed main()
{
    scan>>T;
    while (T--) work();
    return 0;
}