結果
問題 | No.1930 XOR of Two Range |
ユーザー | Lê Hoàng Ngô |
提出日時 | 2024-06-29 12:00:18 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 35 ms / 2,000 ms |
コード長 | 3,201 bytes |
コンパイル時間 | 1,426 ms |
コンパイル使用メモリ | 167,644 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-29 12:00:21 |
合計ジャッジ時間 | 2,255 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 25 ms
6,944 KB |
testcase_02 | AC | 35 ms
6,944 KB |
testcase_03 | AC | 35 ms
6,940 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #define ll long long #define all(x) x.begin(),x.end() #define rep(i,a,b) for(int i=a;i<=b;i++) #define rep_r(i,a,b) for(int i=a;i>=b;i--) #define each(a,x) for (auto& x : a) using pi = pair<int,int>; using pl = pair<ll,ll>; using vi = vector<int>; using vl = vector<ll>; #define sz(x) int(x.size()) #define so(x) sort(all(x)) #define so_r(x) sort(all(x),greater<int>()) #define lb lower_bound #define ub upper_bound const char nl = '\n'; int dx[4] = {1,-1,0,0}; int dy[4] = {0,0,1,-1}; int bit_cnt(int x){ return __builtin_popcount(x); } ll bex(ll a, ll b, ll mod = 1e9 + 7){ll res = 1LL; while(b){ if (b&1) res = res * a % mod; a = a * a % mod; b >>= 1;} return res;} template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;} template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;} const int MOD = 998244353; const int N = 5e5 + 5; const ll INF = 1e16; // https://yukicoder.me/problems/no/1930 // T test case // each test consist L,R // output: xor of all (i + j) that i + j <= L + R and L <= i <= j <= R // case L == R => ans = 2 * L = (L << 1) // case L + 1 == R => ans = (2L) ^ (2L + 1) = 1 // case L + 2 == R => ans = (2L) ^ (2L + 1) ^ (2L + 2) ^ (2L + 2) = 1 // case L + 3 == R => ans = (2L) ^ (2L+1) ^ (2L+2) ^ (2L+3) // ^ (2L+2) ^ (2L+3) = 1 // case L + 4 == R => ans = (2L) ^ (2L+1) ^ (2l+2) ^ (2L+3) ^ (2L+4) // ^ (2L+2) ^ (2L+3) ^ (2L+4) // ^ (2L+4) = 1 ^ (2L + 4) = 2L + 5 // 5 -> 3 -> 1 = 0 ^ 1 ^ (2L+4) // case L + 5 == R => ans = 6 -> 4 -> 2 = 0 // case L + 6 == R => ans = 7 -> 5 -> 3 -> 1 = 0 // case L + 7 == R => ans = 8 -> 6 -> 4 -> 2 = 0 // 10 -> 8 -> 6 -> 4 -> 2 = // 9 -> 7 -> 5 -> 3 -> 1 = 2L + 8 (diff == 9) // 11 -> 9 -> 7 -> 5 -> 3 -> 1 = 1 ^ 1 ^ 1 = 1 (diff == 11) // 13 -> 11 -> 9 -> 7 -> 5 -> 3 -> 1 = 1 ^ 1 ^ 1 // diff = R - L + 1 // if diff % 8 == 1,3,5,7 // = 1 => L + R // = 3 => 1 // = 5 => L + R + 1 // = 7 => 0 void solve(){ int T; cin >> T; while(T--){ ll L,R; cin >> L >> R; ll diff = R - L + 1; ll r = diff % 8; if (r == 1) cout << L + R << nl; else if (r == 2) cout << 1 << nl; // 1 else if (r == 3) cout << 1 << nl; else if (r == 4) cout << 1 << nl; // 4 -> 2 else if (r == 5) cout << L + R + 1 << nl; // in this case: 1 ^ (L + R) = L + R + 1 (because l + r % 2 == 0) else if (r == 6) cout << 0 << nl; // 6-> 4 -> 2 else if (r == 7) cout << 0 << nl; // example L + 6 == R => 7 -> 5 -> 3 -> 1 => 1 ^ 1 = 0 (if we add more 8 to R then we still have (1 ^ 1) = 0 ^ old = old) else if (r == 0) cout << 0 << nl; } } ll calc(ll l, ll r){ ll ans = 0; rep(i,l,r){ rep(j,i,r){ if (i+j<=l+r) { ans ^= (i + j); } } } return ans; } void test() { rep(i,4,3+11) { cout << calc(3,i) << nl; } } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; // cin >> t; // test(); while (t--){ solve(); } }