跳到正文

个人记录

普鸡组模拟赛题解(9.23)

AirQwQ约 8 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月5日
篇幅
约 8 分钟

版本

  1. v1当前

咕咕咕咕咕咕

T1

https://hjjoi.com/contest/C494/problem/A

solution⁡\operatorname{solution}

  • 根据题意模拟即可。

虽然简单但坑点还蛮多:

  • 多组数据影响,不能直接 break 需要全部输入完。

  • 注意 x<263x < 2^{63} 所以会炸 long long 需要 __int128 或者将不等式 cnt×x≤mcnt \times x \leq m 更改为 cnt≤mxcnt \leq \frac{m}{x} 之后再乘。

code⁡\operatorname{code}

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
	int q;
	cin.tie(0);cin>>q;
	while(q--){
		int n,m,ans=9e18;
        __int128 cnt=0;
		cin>>n>>m;
		for(int i=1;i<=n;i++){
			char c;int a;
			cin>>c>>a;
			if(c=='+') cnt+=a;
			if(c=='*') cnt*=a;
			if(c=='/') cnt/=a;
			if(cnt>m||cnt<0)
				ans=min(ans,i);
		}
		if(ans==9e18)
			cout<<"Not Overflow\n";
		else cout<<ans<<'\n';
	}
	return 0;
}

T2

https://hjjoi.com/contest/C494/problem/B

solution⁡\operatorname{solution}

观察最后构成的二维数组,因为由数组对应 xor⁡\operatorname{xor} 而成,所以对于任意合法路径,必然在同个小的由 00 构成的矩阵中。于是随便维护两个数组同一字符连续长度,最后 check⁡\operatorname{check} 相等,长度是否足够即可。

code⁡\operatorname{code}

ps:用的前缀和

C++
#include<bits/stdc++.h>
using namespace std;
int a[1000005],b[1000005];
int suma[1000005],sumb[1000005];
bool check(int x1,int y1,int x2,int y2){
	return (y1==0&&y2==0)||(x1==y1&&x2==y2);
}
int main(){
	cin.tie(0);
	int n,q;
	cin>>n>>q;
	for(int i=1;i<=n;i++)
		cin>>a[i],suma[i]=suma[i-1]+a[i];
	for(int i=1;i<=n;i++)
		cin>>b[i],sumb[i]=sumb[i-1]+b[i];
	while(q--){
		int xa,xb,ya,yb;
		cin>>xa>>xb>>ya>>yb;
		if(xa<ya) swap(xa,ya);
		if(xb<yb) swap(xb,yb);
		if(check(xa-ya+1,suma[xa]-suma[ya-1],xb-yb+1,sumb[xb]-sumb[yb-1]))
			puts("YES");
		else puts("NO");
	}
}

T3

https://hjjoi.com/contest/C494/problem/C

最难的一道题 QwQ。

solution⁡\operatorname{solution}

5050 分

简易二维 dp 即可,状态转移方程:

dpi=∑j=1i−1dpj×f(ai∧aj)dp_i = \sum^{i-1}_{j=1} dp_j \times f(a_i \wedge a_j)

其中 f(i)=popcount⁡(i)f(i)=\operatorname{popcount}(i)

100100 分

若 ai∈0,1a_i \in {0,1} 可以直接使用前缀和优化至 O(n)O(n),状态转移为:

dpi=sum×[ai],sum=dpi+sumdp_i = sum \times [a_i],sum = dp_i +sum

所以对于 ai∈Na_i \in \mathbb{N} 时进行二进制拆分后对于每一位进行前缀和优化即可,最后相乘时间复杂度 o(nlog⁡n)o(n \log n)

状态转移方程为:

dpi=dpi+sumj[ai∧1],sumj=sumj+dpi[ai∧1]dp_i=dp_i+sum_j[a_i \wedge 1],sum_j=sum_j+dp_i[a_i \wedge 1]

简简单单拿下 100100 分。

code⁡\operatorname{code}

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod=998244353;
int dp[200005],sum[35];
signed main(){
	int t;
	cin>>t;
	while(t--){
		int n,a,b,cnt;
		cin>>n;
		memset(sum,0,sizeof sum);
		memset(dp,0,sizeof dp);
		dp[1]=1;
		for(int i=1;i<=n;i++){
			cin>>a;b=a;
			for(int j=0;a;j++,a>>=1)
				dp[i]+=(a&1)*sum[j],dp[i]%=mod;
			for(int j=0;b;j++,b>>=1)
				sum[j]+=(b&1)*dp[i],sum[j]%=mod;
		}
		cout<<dp[n]<<'\n';
	}
	return 0;
}

T4

https://hjjoi.com/contest/C494/problem/D

solution⁡\operatorname{solution}

反向思考一层一层剥去颜色,所以找到 {aaaa}\begin{Bmatrix} a&a \\ a&a \end{Bmatrix} 之后找如下情况 {bb∗baa∗aa}\begin{Bmatrix} b&b&* \\ b&a&a \\ *&a&a \\ \end{Bmatrix} {∗bb∗aa∗aa}\begin{Bmatrix} *&b&b \\ *&a&a \\ *&a&a \\ \end{Bmatrix} 及其轮换,判断每一次是否有价值,然后着色就可以了。

code⁡\operatorname{code}

C++
#include<bits/stdc++.h>
using namespace std;
int a[205][205],s[205][205],n,m;
bool check(int x,int y){
	int cnt=0;
	for(int i=0;i<=1;i++)
		for(int j=0;j<=1;j++){
			if(!s[x+i][y+j]){
				if(!cnt) cnt=a[x+i][y+j];
				else if(a[x+i][y+j]!=cnt) return 0;
			}

		}
	return cnt;
}
void dfs(int x,int y){
	for(int i=0;i<=1;i++)
		for(int j=0;j<=1;j++)
			if(!s[x+i][y+j]) s[x+i][y+j]=a[x+i][y+j];
	if(check(x-1,y-1)&&x-1>=1&&y-1>=1) dfs(x-1,y-1);
	if(check(x,y-1)&&y-1>=1) dfs(x,y-1);
	if(check(x-1,y)&&x-1>=1) dfs(x-1,y);
	if(check(x-1,y+1)&&x-1>=1&&y+2<=m) dfs(x-1,y+1);
	if(check(x+1,y-1)&&y-1>=1&&x+2<=n) dfs(x+1,y-1);
	if(check(x+1,y)&&x+2<=n) dfs(x+1,y);
	if(check(x,y+1)&&y+2<=m) dfs(x,y+1);
	if(check(x+1,y+1)&&x+2<=n&&y+2<=m) dfs(x+1,y+1);
}
signed main(){
	int t;
	cin>>t;
	while(t--){
		memset(s,0,sizeof s);
		cin>>n>>m;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
				cin>>a[i][j];
		for(int i=1;i<n;i++)
			for(int j=1;j<m;j++)
				if(a[i][j]==a[i+1][j]&&a[i][j]==a[i][j+1]&&a[i][j]==a[i+1][j+1])
					dfs(i,j);
		string ans="Yes";
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
				if(a[i][j]!=s[i][j]) ans="No";
		cout<<ans<<'\n';
	}
	return 0;
}
原文