随笔- 19  文章- 0  评论- 0  阅读- 838 

Codeforces Round 1007 (Div. 2)

题目链接👈

题目描述🥰

题目思路😀

这个题的思维太巧妙了,这里就拿出题人的题解来看(因为这个题解写的太透彻了😭)

我就主要讲一下这个思路的代码的实现

首先我们需要处理前缀按位异或和

vector<int>pre(n+1);
	for(int i=1;i<=n;i++)pre[i]=pre[i-1]^a[i];

其次我们需要判断n的奇偶性,如果n是一个偶数,我们需要让他变为奇数,就相应的根据规则拓展就好了

if(n%2==0)
	{
		n++;
		int res=pre[n/2];
		a.push_back(res);
		pre.push_back(pre.back()^res);
	}

其次我们需要准备前2n的数组来方便后面的计算

for(int i=n+1;i<=2*n;i++)
	{
		int res=pre[i/2];
		a.push_back(res);
		pre.push_back(pre[i-1]^a[i]);
	}

处理l这个数,在除2的过程中答案不断和p异或,在l已经是奇数的时候,我们已经可以确定答案的时候,就可以退出循环了,而如果一直是偶数,在l已经小于等于2n的时候,就可以直接和a[l]异或了(因为这个区间里面的a[x]值是确定的)。

Question:为什么l为奇数时可以确定答案?

因为当我们的l是奇数的时候,那么就一直是和p异或,那么答案就是确定的,可以直接退出循环。

int p=pre[n];
	int ans=0;
	while(1)
	{
		if(l<=n*2)
		{
			ans^=a[l];
			break;
		}
		ans^=p;
		l/=2;
		if(l%2==1)break;
	}
	cout<<ans<<endl;

AC代码🧠

// Problem: D1. Infinite Sequence (Easy Version)
// Contest: Codeforces - Codeforces Round 1007 (Div. 2)
// URL: https://codeforces.com/contest/2071/problem/D1
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>

#define dev1(a) cout << #a << '=' << a << endl;
#define dev2(a, b) cout << #a << " = " << a << "  " << #b << " = " << b << endl;
#define dev3(a, b, c) cout << #a << " = " << a << "  " << #b << " = " << b << "  " << #c << " = " << c << endl;
#define dev4(a, b, c, d) cout << #a << " = " << a << "  " << #b << " = " << b << "  " << #c << " = " << c << "  " << #d << " = " << d << endl;
#define dev5(a, b, c, d, e) cout << #a << " = " << a << "  " << #b << " = " << b << "  " << #c << " = " << c << "  " << #d << " = " << d << "  " << #e << " = " << e << endl;
#define vec(a)                         \
    for (int i = 0; i < a.size(); i++) \
        cout << a[i] << ' ';           \
    cout << endl;
#define darr(a, _i, _n)               \
    cout << #a << ':';                \
    for (int ij = _i; ij <= _n; ij++) \
        cout << a[ij] << ' ';         \
    cout << endl;           
#define cin(a,n)           \
     for(int i=0;i<n;i++) \
      cin>>a[i];          
#define endl "\n"
#define pow pim
int pim(int a,int k)
{
    int res=1;
    if(a==0)return 0;
    while(k) 
    {
        if(k&1)res=(int)res*a;
        k>>=1;
        a=(int)a*a;
    }
    return res;
}
#define fi first
#define se second
#define caseT \
    int T;    \
    cin >> T; \
    while (T--)
#define int long long
// #define int __int128

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 99999999;

// const int N = ;
int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}
 
int lcm(int a, int b)
{
    return a * b / gcd(a, b);
}
inline int read()
{
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
void solve()
{
	int n,l,r;
	cin>>n>>l>>r;
	vector<int>a(n+1);
	for(int i=1;i<=n;i++)cin>>a[i];
	vector<int>pre(n+1);
	for(int i=1;i<=n;i++)pre[i]=pre[i-1]^a[i];
	if(n%2==0)
	{
		n++;
		int res=pre[n/2];
		a.push_back(res);
		pre.push_back(pre.back()^res);
	}
	for(int i=n+1;i<=2*n;i++)
	{
		int res=pre[i/2];
		a.push_back(res);
		pre.push_back(pre[i-1]^a[i]);
	}
	int p=pre[n];
	int ans=0;
	while(1)
	{
		if(l<=n*2)
		{
			ans^=a[l];
			break;
		}
		ans^=p;
		l/=2;
		if(l%2==1)break;
	}
	cout<<ans<<endl;
}

signed main()
{

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
	caseT
    solve();
    return 0;
}
/*
    if ((x / 2 - n) % 2 == 0) {
                break;
            }
    l/2=2*m+n
*/

 

 posted on   熙玺  阅读(65)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】

Shu-How Zの小窝

Loading...
点击右上角即可分享
微信分享提示