跳到正文

个人记录

三加二题解

aTawa约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年1月17日
收录
2026年2月9日
更新
2026年11月14日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1

题意

若干个连续序列拼接,序列形式为 nn 个 11 ,每个 11 左右两边有一个 00,共 n+1n + 1 个 00。
给定 TT 组数据,每组数据包含 nn 和 xx ,请问第 xx 个元素是 11 还是 00。
数据范围:
对于全部测试点:1≤T≤1051 \leq T \leq 10^5,0≤n≤10180 \leq n \leq 10^{18},1≤x≤10181 \leq x \leq 10^{18}
对于 30%30\% 的测试点:1≤T≤101 \leq T \leq 10,0≤n≤1060 \leq n \leq 10^6,1≤x≤1061 \leq x \leq 10^6

输入样例 #1

代码
3
1 4
0 2
2 7

输出样例 #1

代码
0
0
1

分析

题意很好理解。不难发现每 n×2+1n \times 2 + 1 次是一个循环,所以我们先对 xx 取模 n×2+1n \times 2 + 1 ,设结果为 kk。
然后我们发现当 k=0k = 0 时,对应上一组序列的最后一位,应是 00 ;否则,应是 k mod 2 k \bmod 2 ,对应这组序列的 11 到 n×2n \times 2 位。
上述为第一种方法,对应下文中参考代码a。
为了防止 k=0k = 0 时对应上一组序列,我们可以使得 k=(x−1) mod (n×2+1)k = (x - 1) \bmod (n \times 2 + 1)。此时答案为 k mod 2k \bmod 2。
上述为第二种方法,对应下文中参考代码b。
此外,本题还有许多种换汤不换药的实现办法。本文不再介绍。
由于本题数据范围,应选择合适的数据类型。

代码

参考代码a

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
int t,n,x;
signed main(){
	cin >> t;
	while (t--){
		cin >> n >> x;
		x = x % (2 * n + 1);
		if (x == 0) cout << 0;
		else cout << (x + 1) % 2;
		cout << "\n";
	}
	return 0;
}

参考代码b

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
int t;
int n,x;
signed main(){
	cin >> t;
	while (t--){
		cin >> n >> x;
		cout << (x-1) % (n * 2 + 1) % 2<< endl;
	}
	return 0;
}
原文