跳到正文

个人记录

3.22 -【位运算】位运算搜索

Void_Rift约 6 分钟
原文

这是 2026年4月2日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年3月29日
收录
2026年4月2日
更新
2026年9月25日
篇幅
约 6 分钟

版本

  1. v2当前
  2. v1

课堂练习

-U438524 sigma-

思路

  • ∑∑∑\sum\sum\sum

  • 对于固定的 ii,内层求和 ∑j=i+1n\sum_{j=i+1}^n jj 是连续整数的等差数列求和

    • 首项:i+1i+1
    • 末项:nn
    • 项数:n−(i+1)+1=n−in-(i+1)+1=n-i

    等差数列求和公式:和 == ((首项 ++ 末项)) ×\times 项数 ÷ 2\div\space2

    代入得内层和:

    ∑j=i+1nj=(i+1+n)×(n−i)2=(n+i+1)×(n−i)2\sum_{j=i+1}^n j=\frac{(i+1+n)\times(n−i)}{2}=\frac{(n+i+1)\times(n−i)}{2}

Code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e7+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
int n,ans;
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cin>>n;
	for(int i=1;i<=n-1;i++) ans+=((n+i+1)*(n-i))/2;
	cout<<ans;
	return 0;
}

-U562064 求和-

思路

  • 这题可以研究一下样例解释

    • 满足要求的所有数对为 :(0,0),(0,1),(0,2),(0,3),(1,1),(1,2),(1,3),(2,2),(2,3),(3,3):(0,0),(0,1),(0,2),(0,3),(1,1),(1,2),(1,3),(2,2),(2,3),(3,3) 0:0: 出现五次
      1:1: 也出现五次
      2:2: 又出现五次?
      3:3: 还出现五次?!!

    其实到这就可以推出规律了 并不是我懒得写公式

ans=n×(n+1)×(n+2)2 mod 998244353 ans=\frac{ n\times(n+1)\times(n+2)}{2}\space\text{mod}\space998244353

Code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e7+10,inf=0x3f3f3f3f3f3f,mod=998244353;
int T,n,ans;
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cin>>T;
	while(T--)
	{
		cin>>n;
		cout<<(n*(n+1)*(n+2)/2)%mod<<"\n";
	}
	return 0;
}

-P10449 费解的开关-

思路

  • 代码最难写的一集
  • 不想写思路直接写注释了

Code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=4e7+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
int n,ans[N];
queue<pair<int,int> > q;
int get_id(int x,int y)//将5×5的坐标(i,j)转换为0~24的二进制位编号
{
	return (x-1)*5+y-1;
}
int set_0(int num,int i,int j)//将num的(i,j)对应位设为0(灯灭)
{
	if(i>5 || i<1 || j>5 || j<1) return num;
	return (num&(~(1<<get_id(i,j))));
}
int set_1(int num,int i,int j)/将num的(i,j)对应位设为1(灯亮)
{
	if(i>5 || i<1 || j>5 || j<1) return num;
	return (num|(1<<get_id(i,j)));
}
int set_f(int num,int i,int j)//翻转num的(i,j)对应位(反转灯状态)
{
	if(i>5 || i<1 || j>5 || j<1) return num;
	return (num^(1<<get_id(i,j)));
}
void bfs()
{
	fill(ans,ans+N,-1);//初始化所有状态为-1(不可达)
	int x=0;
    //目标状态:所有灯全亮(全1)
	for(int i=1;i<=5;i++)
	{
		for(int j=1;j<=5;j++)
		{
			x=set_1(x,i,j);
		}
	}
	q.push({0,x});
	ans[x]=0;
	while(!q.empty())
	{
		auto f=q.front();
		q.pop();
        //遍历所有25个可点击的灯的位置
		for(int i=1;i<=5;i++)
		{
			for(int j=1;j<=5;j++)
			{
				int y=f.second;
                //翻转自身和上下左右5个位置的灯
				y=set_f(y,i,j);
				y=set_f(y,i+1,j);
				y=set_f(y,i-1,j);
				y=set_f(y,i,j+1);
				y=set_f(y,i,j-1);
				if(ans[y]==-1)//未访问过,记录步数
				{
					ans[y]=f.first+1;
					if(f.first+1!=6) q.push({f.first+1,y});//题目中最多6步,6步的状态无需再扩展入队
				}
			}
		}
	}
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cin>>n;
	bfs();//预处理所有可达状态的最少步数
	while(n--)
	{
		int x=0;
        //读取5*5的棋盘,转换为对应的状态整数
		for(int i=1;i<=5;i++)
		{
			for(int j=1;j<=5;j++)
			{
				char c;
				cin>>c;
				if(c=='0') x=set_0(x,i,j);
				if(c=='1') x=set_1(x,i,j);
			}
		}
		cout<<ans[x]<<"\n";//输出结果
	}
	return 0;
}