P8763 [蓝桥杯 2021 国 ABC] 异或变换

P8763 [蓝桥杯 2021 国 ABC] 异或变换

思路

显而易见这个异或一直进行下去一定是会重复的,然后我们可以达表找规律,发现只要进行最大的 \(\le n\) 的最大的二的整数次幂次的操作,就一定会有一个重复的。这一部分可以用 \(O(\log n)\) 的时间复杂度暴力枚举,最终总时间复杂度是 \(O(n\log n)\)。

ACcode

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define endl '\n'
#define inf 0x7f7f7f7f7f7f7f7f
const int N=10001;
int n,t,x=1;
bool a[N];

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
//↑以上为初始化↑
//------------------------------
	cin >> n >> t;
	for(int i = 1;i<=n;++i){
		char ch;
		cin >> ch;
		a[i]=(ch=='1');
	}
	while(x<n) x<<=1;
	t%=x;
	for(int i = 1;i<=t;++i){
		for(int j = n;j>1;--j){
			a[j]=a[j-1]^a[j];
		}
	}
	for(int i = 1;i<=n;++i) cout << a[i];
	return 0;
}
posted @ 2026-10-22 07:26  elon_giao  阅读(3)  评论(0)    收藏  举报