CQOI2018 简要题解

破解D-H协议
列个式子会发现是 B S G S BSGS BSGS的模板题,直接码就是了。
代码:

#include
#include
#define ri register int
using namespace std;
inline int read(){
    int ans=0;
    char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
    return ans;
}
typedef long long ll;
inline int ksm(int a,int p,int mod){int ret=1;for(;p;p>>=1,a=(ll)a*a%mod)if(p&1)ret=(ll)ret*a%mod;return ret;}
int g,p,sqr,inv;
tr1::unordered_map<int,int>S;
inline void init(int a,int mod){
    sqr=ceil(sqrt(mod)),inv=ksm(a,mod-2,mod),a=ksm(a,sqr,mod);
    for(ri i=0,mul=1;i<=mod/sqr;++i,mul=(ll)mul*a%mod)S[mul]=i?i:sqr;
}
inline int query(int b,int mod){for(ri i=0,mul=b;i<sqr;++i,mul=(ll)mul*inv%mod)if(S[mul])return (S[mul])%sqr*sqr+i;}
int main(){
    int n,g,p;
    g=read(),p=read(),init(g,p);
    for(ri tt=read(),a,b;tt;--tt){
        a=query(read()%p,p),b=query(read()%p,p);
        cout<<ksm(ksm(g,a,p),b,p)<<'\n'; 
    }
    return 0;
}

社交网络
按照题意模拟写有向图的矩阵树就完了。
代码:

#include
#define ri register int
using namespace std;
const int mod=10007,N=255;
inline int read(){
    int ans=0;
    char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
    return ans;
}
int n,m,a[N][N];
inline int add(const int&a,const int&b){return a+b>=mod?a+b-mod:a+b;}
inline int dec(const int&a,const int&b){return a>=b?a-b:a-b+mod;}
inline int mul(const int&a,const int&b){return a*b%mod;}
inline int gauss(){
    int ret=1;
    for(ri i=2;i<=n;++i){
        for(ri j=i+1;j<=n;++j){
            while(a[j][i]){
                int tmp=a[i][i]/a[j][i];
                for(ri k=i;k<=n;++k)a[i][k]=dec(a[i][k],mul(tmp,a[j][k]));
                for(ri k=i;k<=n;++k)swap(a[i][k],a[j][k]);
                ret=mod-ret;
            }
        }
        ret=mul(ret,a[i][i]);
    }
    return ret;
}
int main(){
    n=read(),m=read();
    for(ri i=1,u,v;i<=m;++i)u=read(),v=read(),--a[u][v],++a[u][u];
    for(ri i=1;i<=n;++i)for(ri j=1;j<=n;++j)if(a[i][j]<0)a[i][j]+=mod;
    cout<<gauss();
    return 0;
}

交错序列
二项式展开一下 x a y b = ( n − y ) a y b = ∑ i = 0 a C a i n i ( − 1 ) a − i y a + b − i x^ay^b=(n-y)^ay^b=\sum_{i=0}^aC_a^in^i(-1)^{a-i}y^{a+b-i} xayb=(ny)ayb=i=0aCaini(1)aiya+bi
然后用矩阵快速幂优化 d p dp dp转移出 y y y每个幂的个数即可。
代码:

#include
#define ri register int
using namespace std;
const int N=1e7+5,M=185;
int mod,lim,n,a,b,ans=0,C[M][M],m;
typedef long long ll;
inline int add(const int&a,const int&b){return a+b>=mod?a+b-mod:a+b;}
inline int dec(const int&a,const int&b){return a>=b?a-b:a-b+mod;}
inline int mul(const int&a,const int&b){return (ll)a*b%mod;}
struct Mat{
    int a[M][M];
    inline int*operator[](const int&k){return a[k];}
    Mat(int x=0){for(ri i=0;i<lim;++i)for(ri j=0;j<lim;++j)a[i][j]=i==j?x:0;}
    friend inline Mat operator*(Mat A,Mat B){
        Mat ret(0);
        for(ri i=0;i<lim;++i)for(ri k=0;k<lim;++k)if(A[i][k])
        for(ri j=0;j<lim;++j)if(B[k][j])ret[i][j]=add(ret[i][j],mul(A[i][k],B[k][j]));
        return ret;
    }
    friend inline Mat operator^(Mat A,int p){
        Mat ret(1);
        for(;p;p>>=1,A=A*A)if(p&1)ret=ret*A;
        return ret;
    }
}S;
int main(){
    scanf("%d%d%d%d",&n,&a,&b,&mod);
    lim=(m=a+b+1)<<1;
    C[0][0]=1;
    for(ri i=1;i<=a+b;++i){
        C[i][0]=1;
        for(ri j=1;j<=i;++j)C[i][j]=add(C[i-1][j],C[i-1][j-1]);
    }
    for(ri i=0;i<m;++i){
        S[i][i]=S[i][i+m]=1;
        for(ri j=0;j<=i;++j)S[i+m][j]=C[i][j];
    }
    S=S^n;
    for(ri i=0,mult=1;i<=a;++i){
        if((a-i)&1)ans=dec(ans,mul(mul(C[a][i],mult),add(S[a+b-i][0],S[a+b-i+m][0])));
        else ans=add(ans,mul(mul(C[a][i],mult),add(S[a+b-i][0],S[a+b-i+m][0])));
        mult=mul(mult,n);
    }
    cout<<ans;
    return 0;
}

解锁屏幕
状压 d p dp dp水题。
f s t a t , i f_{stat,i} fstat,i表示解锁状态为 s t a t stat stat,最后一个解锁的是 i i i的方案数,枚举下一个解锁点转移即可,注意预处理不可行的状态。
代码:

#include
#define ri register int
using namespace std;
inline int read(){
    int ans=0;
    bool f=1;
    char ch=getchar();
    while(!isdigit(ch))f^=ch=='-',ch=getchar();
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
    return f?ans:-ans;
}
typedef long long ll;
const int N=20,mod=100000007;
int n,f[1<<N][N],ban[N][N],cnt[1<<N],ans=0,id[1<<N];
struct pot{
    int x,y;
    pot(int _x=0,int _y=0):x(_x),y(_y){}
    friend inline pot operator+(const pot&a,const pot&b){return pot(a.x+b.x,a.y+b.y);}
    friend inline pot operator-(const pot&a,const pot&b){return pot(a.x-b.x,a.y-b.y);}
    friend inline int operator^(const pot&a,const pot&b){return a.x*b.y-a.y*b.x;}
}a[N];
inline int lowbit(const int&x){return x&-x;}
inline int add(const int&a,const int&b){return a+b>=mod?a+b-mod:a+b;}
inline int dec(const int&a,const int&b){return a>=b?a-b:a-b+mod;}
inline int mul(const int&a,const int&b){return (ll)a*b%mod;}
inline void update(int&a,const int&b){a=add(a,b);}
inline bool check(int i,int j,int k){
    pot x=a[i],y=a[j],z=a[k];
    return z.x>=min(x.x,y.x)&&z.x<=max(x.x,y.x)&&z.y>=min(x.y,y.y)&&z.y<=max(x.y,y.y)&&((z-x)^(z-y))==0;
}
int main(){
    n=read();
    for(ri i=0;i<n;++i)a[i].x=read(),a[i].y=read();
    for(ri i=0;i<n;++i)for(ri j=0;j<n;++j)if(i^j)for(ri k=0;k<n;++k)
    if(k!=i&&k!=j)if(check(i,j,k))ban[i][j]|=1<<k;
    for(ri i=0;i<n;++i)f[1<<i][i]=1;
    for(ri i=0,up=1<<n;i<up;++i)cnt[i]=cnt[i>>1]+(i&1);
    for(ri i=0;i<n;++i)id[1<<i]=i;
    for(ri i=0,up=(1<<n)-1;i<=up;++i){
        if(!i)continue;
        for(ri all=i,j=lowbit(all);all;all^=j,j=lowbit(all)){
            if(f[i][id[j]])for(ri det=up^i,k=lowbit(det);det;det^=k,k=lowbit(det)){
                if((ban[id[j]][id[k]]&i)^ban[id[j]][id[k]])continue;
                update(f[i^k][id[k]],f[i][id[j]]);
            }
        }
    }
    for(ri i=0;i<(1<<n);++i)if(cnt[i]>3)
    for(ri all=i,j=lowbit(all);all;all^=j,j=lowbit(all))update(ans,f[i][id[j]]);
    cout<<ans;
    return 0;
}

九连环
首先手玩几分钟小数据来推一个结论:
f n = f n − 1 + 2 f n − 2 + 1 f_n=f_{n-1}+2f_{n-2}+1 fn=fn1+2fn2+1
然后可以构造两个等式:

  1. ( f n + f n − 1 + 1 ) = 2 ( f n − 1 + f n − 2 + 1 ) (f_n+f_{n-1}+1)=2(f_{n-1}+f_{n-2}+1) (fn+fn1+1)=2(fn1+fn2+1)
  2. ( f n − 2 f n − 1 − 1 2 ) = − ( f n − 1 − f n − 1 2 ) (f_n-2f_{n-1}-\frac12)=-(f_{n-1}-f_n-\frac12) (fn2fn121)=(fn1fn21)

然后变形一波可以得到 f n = ⌊ 2 n + 1 − 1 3 ⌋ f_n=\left\lfloor\frac{2^{n+1}-1}3\right\rfloor fn=32n+11
然后用 f f t fft fft优化高精+快速幂即可。
代码:

#include
#define ri register int
using namespace std;
typedef long double ldb;
struct Cp{
    ldb x,y;
    friend inline Cp operator+(const Cp&a,const Cp&b){return (Cp){a.x+b.x,a.y+b.y};}
    friend inline Cp operator-(const Cp&a,const Cp&b){return (Cp){a.x-b.x,a.y-b.y};}
    friend inline Cp operator*(const Cp&a,const Cp&b){return (Cp){a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x};}
    friend inline Cp operator*(const Cp&a,const ldb&b){return (Cp){a.x*b,a.y*b};}
    friend inline Cp operator/(const Cp&a,const ldb&b){return (Cp){a.x/b,a.y/b};}
};
const ldb pi=acos(-1.0);
int lim,tim;
vector<Cp>A,B;
vector<int>pos;
inline void init(const int&up){ 
    lim=1,tim=0;
    while(lim<=up)lim<<=1,++tim;
    pos.resize(lim),A.resize(lim),B.resize(lim),pos[0]=0;
    for(ri i=0;i<lim;++i)pos[i]=(pos[i>>1]>>1)|((i&1)<<(tim-1));
}
inline void fft(vector<Cp>&a,const int&type){
    for(ri i=0;i<lim;++i)if(i<pos[i])swap(a[i],a[pos[i]]);
    for(ri mid=1;mid<lim;mid<<=1){
        Cp wn=(Cp){cos(pi/mid),type*sin(pi/mid)};
        for(ri j=0,len=mid<<1;j<lim;j+=len){
            Cp w=(Cp){1,0},a0,a1;
            for(ri k=0;k<mid;++k,w=w*wn){
                a0=a[j+k],a1=a[j+k+mid]*w;
                a[j+k]=a0+a1,a[j+k+mid]=a0-a1;
            }
        }
    }
    if(type==-1)for(ri i=0;i<lim;++i)a[i]=a[i]/(ldb)lim;
}
struct poly{
    vector<Cp>a;
    poly(int k=0,Cp x=(Cp){0,0}){a.resize(k+1),a[k]=x;}
    inline int deg()const{return a.size()-1;}
    inline poly extend(const int&k){poly ret=*this;return ret.a.resize(k+1),ret;}
    inline Cp&operator[](const int&k){return a[k];}
    inline const Cp&operator[](const int&k)const{return a[k];}
    friend inline poly operator*(const poly&a,const poly&b){
        int n=a.deg(),m=b.deg();
        init(n+m);
        poly ret(lim);
        for(ri i=0;i<=n;++i)A[i]=a[i];
        for(ri i=0;i<=m;++i)B[i]=b[i];
        for(ri i=n+1;i<lim;++i)A[i]=(Cp){0,0};
        for(ri i=m+1;i<lim;++i)B[i]=(Cp){0,0};
        fft(A,1),fft(B,1);
        for(ri i=0;i<lim;++i)A[i]=A[i]*B[i];
        return fft(A,-1),ret.a=A,ret;
    }
    friend inline poly operator*(const poly&a,const ldb&b){
        poly ret(a.deg());
        for(ri i=0;i<=a.deg();++i)ret[i]=a[i]*b;
        return ret;
    }
    friend inline poly operator/(const poly&a,const ldb&b){
        poly ret(a.deg());
        for(ri i=0;i<=a.deg();++i)ret[i]=a[i]/b;
        return ret;
    }
};
inline poly update(const poly&a){
    vector<int>res;
    int sum=0;
    for(ri val,i=0,up=a.deg();i<=up;++i)val=((int)(a[i].x+0.5))+sum,sum=val/10,res.push_back(val%10);
    while(sum)res.push_back(sum%10),sum/=10;
    while(!res[res.size()-1]&&res.size()!=1)res.pop_back();
    poly ret(res.size()-1);
    for(ri i=0;i<=ret.deg();++i)ret[i]=(Cp){res[i],0};
    return ret;
}
inline void ksm(int p){
    poly ret,a;
    ret[0]=(Cp){1,0},a[0]=(Cp){2,0};
    while(p){
        a=update(a);
        if(p&1)ret=update(ret),ret=ret*a;
        a=a*a,p>>=1;
    }
    ret=update(ret),reverse(ret.a.begin(),ret.a.end());
    vector<int>ans;
    for(ri val,divv=0,i=0;i<=ret.deg();++i)val=(int)ret[i].x+divv,ans.push_back(val/3),divv=val%3*10;
    int pos=0;
    while(!ans[pos]&&pos!=ans.size()-1)++pos;
    for(ri i=pos;i<ans.size();++i)cout<<ans[i];
    puts("");
}
char s[10005];
int main(){
    int n,m;
    scanf("%d",&m);
    while(m--)scanf("%d",&n),ksm(n+1);
    return 0;
}

异或序列
简单的异或前缀和处理+莫队统计答案。
代码:

#include
#define N 100005
using namespace std;
inline int read(){
	int ans=0;
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
	return ans;
}
int sum[N],block,n,m,k,cnt[N],dl=0,dr=-1,ans[N],tmp=0;
struct Q{int l,r,id,blo;}q[N];
inline bool cmp(Q a,Q b){return a.blo==b.blo?a.r<b.r:a.blo<b.blo;}
int main(){
	n=read(),m=read(),k=read(),block=sqrt(n);
	for(int i=1;i<=n;++i)sum[i]=read()^sum[i-1];
	for(int i=1;i<=m;++i)q[i].id=i,q[i].l=read()-1,q[i].r=read(),q[i].blo=(q[i].l-1)/block+1;
	sort(q+1,q+m+1,cmp);
	for(int i=1;i<=m;++i){
		if(q[i].r<dl||q[i].l>dr){
			for(int j=dl;j<=dr;++j)--cnt[sum[j]];
			tmp=0,dl=q[i].l,dr=q[i].r;
			for(int j=dl;j<=dr;++j)tmp+=cnt[k^sum[j]],++cnt[sum[j]];
		}
		else{
			while(dl<q[i].l)--cnt[sum[dl]],tmp-=cnt[k^sum[dl++]];
			while(dl>q[i].l)tmp+=cnt[k^sum[--dl]],++cnt[sum[dl]];
			while(dr<q[i].r)tmp+=cnt[k^sum[++dr]],++cnt[sum[dr]];
			while(dr>q[i].r)--cnt[sum[dr]],tmp-=cnt[k^sum[dr--]];
		}
		ans[q[i].id]=tmp;
	}
	for(int i=1;i<=m;++i)printf("%d\n",ans[i]);
	return 0;
}

你可能感兴趣的:(#,题解)