题意
若干个连续序列拼接,序列形式为 个 ,每个 左右两边有一个 ,共 个 。
给定 组数据,每组数据包含 和 ,请问第 个元素是 还是 。
数据范围:
对于全部测试点:,,
对于 的测试点:,,
输入样例 #1
3
1 4
0 2
2 7输出样例 #1
0
0
1分析
题意很好理解。不难发现每 次是一个循环,所以我们先对 取模 ,设结果为 。
然后我们发现当 时,对应上一组序列的最后一位,应是 ;否则,应是 ,对应这组序列的 到 位。
上述为第一种方法,对应下文中参考代码a。
为了防止 时对应上一组序列,我们可以使得 。此时答案为 。
上述为第二种方法,对应下文中参考代码b。
此外,本题还有许多种换汤不换药的实现办法。本文不再介绍。
由于本题数据范围,应选择合适的数据类型。
代码
参考代码a
#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
#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;
}