(第十二届蓝桥杯国赛)G:异或变换(模拟+找规律)

在这里插入图片描述

在这里插入图片描述

这道题目当时在考场写了个暴力,所以现在过来补一下

看一下数据范围,发现t是小于1e18的,所以肯定不能直接模拟,换句话说我们必然要想到一种方法来减小t且不能影响答案,然后就可以打表找一下规律,发现这种异或变换是呈现一定的循环性的,假如01串长度为n,那么循环节大小就是第一个大于等于n的2的幂次,当然这个规律并不是很好发现,这就启示我们可能在一道题目没有思路的时候可以选择打表去找下规律,说不定会有所收获。知道了循环节的大小后我们就可以用t对循环节大小取余,然后直接模拟即可,因为长度不大于10000,则循环结大小一定小于等于16384=2^14,我们直接模拟即可。

先附上暴力求循环节代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<bitset>
using namespace std;
const int N=10003;
string a;
bool check(bitset<N> s)
{
	for(int i=0,j=a.size()-1;i<a.size();i++,j--)
		if((a[j]-'0')!=s[i]) return false;
	return true;
}
int main()
{
	long long len,t,cnt=0;
	cin>>len>>t>>a;
	bitset<N>s(a);
	while(1)
	{
		cnt++;
		if(cnt>t) break;//说明直接进行交换即可,没必要取余 
		s^=s<<1;
		s>>=1;
		if(check(s)) break;
	}
	t%=cnt;
	cout<<cnt<<endl;
	while(t--)	s^=s<<1,s>>=1;
	for(int i=a.size()-1;i>=0;i--)
		printf("%d",s[i]?1:0);
	return 0; 
}

下面附上代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<bitset>
using namespace std;
const int N=10003;
int a[N];
int main()
{
	int n,t,cnt=1;
	string s;
	cin>>n>>t>>s;
	for(int i=1;i<=n;i++)
		a[i]=s[i-1]-'0';
	while(cnt<=n) cnt<<=1;
	t%=cnt;
	while(t--)
	{
		for(int i=n;i>1;i--)
			a[i]^=a[i-1];
	}
	for(int i=1;i<=n;i++)
		printf("%d",a[i]);
	return 0; 
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值