Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows:
- f(0)=a;
- f(1)=b;
- f(n)=f(n−1)⊕f(n−2) when n>1, where ⊕ denotes the bitwise XOR operation.
You are given three integers a, b, and n, calculate f(n).
You have to answer for T independent test cases.
Input
The input contains one or more independent test cases.
The first line of input contains a single integer T (1≤T≤103), the number of test cases.
Each of the T following lines contains three space-separated integers a, b, and n (0≤a,b,n≤109) respectively.
Output
For each test case, output f(n).Exampleinput
3 3 4 2 4 5 0 325 265 1231232
output
7 4 76
Note
In the first example, f(2)=f(0)⊕f(1)=3⊕4=7.
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio( false ); cin.tie(0); int tt; cin >> tt; while (tt--) { int a, b, n; cin >> a >> b >> n; n %= 3; if (n == 0) cout << a << '\n' ; else if (n == 1) cout << b << '\n' ; else cout << (a ^ b) << '\n' ; } return 0; } |
Related posts:
Challenging Balloons
Old Peykan
Artem and Array
Minus and Minus Give Plus
Finding a negative cycle in the graph
Middle Class
Frog Jumping
New Year and Naming
PolandBall and Polygon
Kaavi and Magic Spell
Height All the Same
Fountains
Lucky Days
Read Time
D´Esopo-Pape algorithm
Point location in O(log n)
Cow and Snacks
Packets
Spaceship Solitaire
Berland SU Computer Network
New Year and the Treasure Geolocation
Leaf Partition
Number Transformation
Bear and Paradox
Primality tests
Irreducible Anagrams
Grandfather Dovlet’s calculator
Permutation Game
Minimum-cost flow - Successive shortest path algorithm
Farewell Party
Big Data
Package Delivery