Problem - A - Codeforces
当字符串为回文时,两种变换中最终都是同一种,所以只需要判断s是否为回文字符串即可确定未来变换的趋势。 非回文在一次操作之后也会变成回文,所以就比回文多一步。
特判k==0时他还存在于原字符串一种形式中
#include<iostream>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
int t; cin>>t;
while(t--)
{
int n,k; cin>>n>>k;
string s; cin>>s;
bool flag=true;
if(k==0) cout<<"1"<<endl;
else{
for(int i=0;i<n;i++)
if(s[i]!=s[n-i-1]) flag=false;
if(flag==false) cout<<"2"<<endl;
else cout<<"1"<<endl;
}
}
return 0;
}
Problem - B - Codeforces
通过x与x+3可以得知两人的奇偶性不同,每次对奇数经行一次异或操作都会更改其奇偶性,所以我们可以先假设alice所持为奇数,并与数组a进行异或运算,如果Alice在和整个数组异或完毕之后和y的奇偶性相同,那么就输出Alice ,否则输出Bob。
异或 ^ :110 000 101 010
按位与 & :111 100 010 000
#include<iostream>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long LL;
LL a[200200];
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
LL t,n,x,y;
cin>>t;
while(t--)
{
cin>>n>>x>>y;
for(int i=1;i<=n;i++)
{
cin>>a[i];
x^=a[i];
}
if(((x^y)&1)==0)cout<<"Alice"<<endl;
else cout<<"Bob"<<endl;
}
return 0;
}
Problem - C - Codeforces
可以直接简化到两个数字来思考,/2要为整,那必须是同奇偶,延伸至长串数字来看也必须是奇偶相同才能得整。那么控制奇偶分家就成了。
#include<iostream>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
int t,n,m;
cin>>t;
while(t--)
{
cin>>n>>m;
if(m==1)
{
cout<<"YES"<<endl;
for(int i=1;i<=n;i++)
cout<<i<<endl;
}
else if(n&1) cout<<"NO"<<endl;//如果是奇数行必有一行会出奇偶共存的现象
else
{
cout<<"YES"<<endl;
for(int i=1,k=1;i<=n*m;i+=2,k++)
{
cout<<i<<" ";
if(k%m==0) cout<<endl;
}
for(int i=2,k=1;i<=n*m;i+=2,k++)
{
cout<<i<<" ";
if(k%m==0) cout<<endl;
}
}
}
return 0;
}