三加二题解
题意
若干个连续序列拼接,序列形式为
n 个1 ,每个1 左右两边有一个0 ,共n + 1 个0 。\ 给定T 组数据,每组数据包含n 和x ,请问第x 个元素是1 还是0 。\ 数据范围:\ 对于全部测试点:1 \leq T \leq 10^5 ,0 \leq n \leq 10^{18} ,1 \leq x \leq 10^{18} \ 对于30\% 的测试点:1 \leq T \leq 10 ,0 \leq n \leq 10^6 ,1 \leq x \leq 10^6 输入样例 #1
3
1 4
0 2
2 7
输出样例 #1
0
0
1
分析
题意很好理解。不难发现每
代码
参考代码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;
}