跳到正文

题解

题解:[XRCOI Round 1] 稻花香里说丰年

gcx114514约 7 分钟
原文
文章信息与版本

文章信息

发布
2024年12月22日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 7 分钟
获赞
3
题目
P11891

版本

  1. v1当前

考虑将其拆分,W(l,r)=T(l,r)∗A(l,r)+T(l,r)∗B(l,r)W(l,r)=T(l,r)*A(l,r)+T(l,r)*B(l,r)。

然后将 BB 反转,也变成求 0101,T(l,r)T(l,r) 变成求相同,方便一些。

然后考虑求:对于每个 [ai=bi][a_i=b_i],求与之匹配的 0101 子序列个数。

方便起见,我们正反跑两遍,分三类讨论(常数要大一些,不过好写很多)。

  • 0101 全在 [ai=bi][a_i=b_i] 前面。
  • 0101 全在 [ai=bi][a_i=b_i] 后面。
  • 00 在左边,11 在右边。

先考虑第一种,其余的同理可以知道怎么做。

0101 全在左边时产生贡献当且仅当其与 [ai=bi][a_i=b_i] 分在了同一个段,也就是 00 的位置与 ii 在同一个段,假设 00 在位置 jj,则对答案的贡献是 2j−1×2n−i2^{j-1}\times 2^{n-i}。

然后用 resres 把 ∑2j−1\sum 2^{j-1} 存下来,这个是容易的,出现 00 时与出现 11 时分开讨论。

C++
#include<bits/stdc++.h>

using namespace std;

namespace Fread {
	const int SIZE=1<<21;char buf[SIZE],*S,*T;
	inline char getchar() {if(S==T){T=(S=buf)+fread(buf,1,SIZE,stdin);if(S==T)return '\n';}return *S++;}
}
namespace Fwrite {
	const int SIZE=1<<21;
	char buf[SIZE],*S=buf,*T=buf+SIZE;
	inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}
	inline void putchar(char c){*S++=c;if(S==T)flush();}
	struct POPOSSIBLE{~POPOSSIBLE(){flush();}}ztr;
}
#define getchar Fread :: getchar
#define putchar Fwrite :: putchar
namespace Fastio{
	struct Reader{
	    template<typename T>
    	Reader& operator >> (T& x) {
        	char c=getchar();T f=1;
        	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}x=0;
        	while(c>='0'&&c<='9'){x=x*10+(c-'0');c=getchar();}x*=f;
	        return *this;
    	}
	    Reader(){}
	}cin;
	struct Writer{
	    template<typename T>
	    Writer& operator << (T x) {
	        if(x==0){putchar('0');return *this;}
	        static int sta[45];int top=0;
	        while(x){sta[++top]=x%10;x/=10;}
	        while(top){putchar(sta[top]+'0');--top;}
	        return *this;
    	}
    	Writer& operator << (char c) {putchar(c);return *this;}
    	Writer(){}
	}cout;
}
#define endl '\n'
#define cin Fastio :: cin
#define cout Fastio :: cout
typedef unsigned long long ull;

const int mod=1e9+7;
struct modint {
    int val;
    static int norm(const int& x) { return x < 0 ? x + mod : x; }
    inline int Mod(const int& x){return x>=mod?x-mod:x;}
    modint() : val(0) {}
    modint(const int& m) : val(m) {}
    modint& operator-=(const modint& o) { return val = norm(val - o.val), *this; }
    modint& operator+=(const modint& o) { return val = Mod(val + o.val), *this; }
    modint& operator*=(const modint& o) { return val = static_cast<int>(1ll * val * o.val % mod), *this; }
    modint operator+(const modint& o) { return Mod(val+o.val); }
    modint operator*(const modint& o) const { return 1ll*val*o.val%mod; }
    inline void Mul(){val=Mod(val<<1);}
};
const modint inv2=(mod+1)/2;

const int N=1e5+10;
modint sum1,sum2,res,ans,pref,preg,suff,sufg;
bitset < 100000001 > f,g;
int n,op;

inline modint expow(modint x,int y){
	modint res=1;
	for(;y;y>>=1){if(y&1) res*=x;x*=x;}
	return res;
}

int main(){
	cin>>n>>op;register ull f1,f2,g1,g2,x1,y1,x2,y2,z1,z2,nowf,nowg;
	if(op==0){for(int i=1;i<=n;++i){cin>>x1>>y1;f[i]=x1;g[i]=!y1;}}
	else {
		cin>>x1>>y1>>z1>>f1>>f2;
		cin>>x2>>y2>>z2>>g1>>g2;
		int cnt=0;
		while(cnt+64<=n){
			nowf=f2*y1+f1*x1+z1;
			nowg=g2*y2+g1*x2+z2;
			f1=f2;f2=nowf;g1=g2;g2=nowg;
			for(int i=1;i<=64;++i){
				++cnt;
				f[cnt]=nowf&1;
				g[cnt]=(nowg&1)^1;
				nowf>>=1;nowg>>=1;
			}
		}
		nowf=f2*y1+f1*x1+z1;
		nowg=g2*y2+g1*x2+z2;
		f1=f2;f2=nowf;g1=g2;g2=nowg;
		for(int i=cnt+1;i<=n;++i){
			f[i]=nowf&1;
			g[i]=(nowg&1)^1;
			nowf>>=1;nowg>>=1;
		}
	}
	modint p1=expow(2,n),p2=inv2;
	for(int i=n;i;--i){
		p1*=inv2;p2.Mul();
		f[i]?sum1+=p2,suff+=p2:res+=sum1;
		g[i]?sum2+=p2,sufg+=p2:res+=sum2;
		ans+=(f[i]==g[i])?res*p1:0;
	}
	sum1=sum2=res=0;
	p1=expow(2,n);p2=inv2;
	for(int i=1;i<=n;++i){
		p1*=inv2;p2.Mul();
		f[i]?res+=sum1,suff-=p1:sum1+=p2;
		g[i]?res+=sum2,sufg-=p1:sum2+=p2;
		ans+=(f[i]==g[i])?res*p1+suff*pref+sufg*preg:0;
		(!f[i])?pref+=p2:0;
		(!g[i])?preg+=p2:0;
	}
	cout<<ans.val<<endl;
	return 0;
}