fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define int long long
  5. #define yes cout << "YES\n";
  6. #define no cout << "NO\n";
  7.  
  8.  
  9. void FastIO(){
  10. ios_base::sync_with_stdio(false);
  11. cin.tie(nullptr);
  12. cout.tie(nullptr);
  13. }
  14.  
  15. void solve(){
  16. int x,n;
  17. cin >> x >> n;
  18.  
  19. if(n == 0){
  20. cout << x << '\n';
  21. return;
  22. }
  23.  
  24. __int128 q = n/4, m = (n+3)/4;
  25. __int128 sum = (__int128)n * (n+1) / 2;
  26.  
  27. __int128 p1 = 2*q*(q+1) + m*(2*m - 1);
  28. __int128 p2 = sum - p1;
  29.  
  30. __int128 ans;
  31.  
  32. if(x % 2 == 0)
  33. ans = (__int128)x - p1 + p2;
  34. else
  35. ans = (__int128)x + p1 - p2;
  36.  
  37. cout << (long long)ans << '\n';
  38. }
  39.  
  40.  
  41. signed main(){
  42. FastIO();
  43.  
  44. int t = 1;
  45. cin >> t;
  46.  
  47. while (t--){
  48. solve();
  49. }
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0.01s 5320KB
stdin
9
0 1
0 2
10 10
10 99
177 13
10000000000 987654321
-433494437 87178291199
1 0
-1 1
stdout
-1
1
11
110
190
9012345679
-87611785637
1
0