We need to compute after K operations: value of Takahashi (A) minus value of Nakahashi (B). Initially integers are A, B, C. Operation: each simultaneously replaces his own integer with sum of the other two. So after one operation:
New A' = B + C
New B' = A + C
New C' = A + B
We need to compute after K steps: A_K - B_K.
We need to be careful about large K up to 1e18, and values can blow up exponentially. But we need to compute only the difference and detect if magnitude > 1e18.
We can derive recurrence. Let’s denote after t steps: a_t, b_t, c_t.
Operation: a_{t+1} = b_t + c_t, b_{t+1} = a_t + c_t, c_{t+1} = a_t + b_t.
We want d_t = a_t - b_t.
Compute d_{t+1} = a_{t+1} - b_{t+1} = (b_t + c_t) - (a_t + c_t) = b_t - a_t = -(a_t - b_t) = -d_t.
So d_{t+1} = - d_t. Therefore d_t = (-1)^t d_0, where d_0 = A - B.
Thus after K steps, d_K = (-1)^K (A - B).
That's simple! Wait, is that true? Let's check with example: Input: 1 2 3 1 -> A=1,B=2, C=3, d0 = -1. After 1 step: a1 = B+C=5, b1 = A+C=4, d1 = 1, which is (-1)^1 * (-1) = 1. matches. Second example: 2 3 2 K=0 -> d0 = -1, output -1. matches. Third example: all equal 1e9, d0=0, so d_K=0. matches. So the answer is simply (-1)^K * (A - B), provided its absolute value doesn't exceed 1e18.
But wait, is it always that simple? Let's double-check the recurrence: a_{t+1} = b_t + c_t, b_{t+1} = a_t + c_t. So difference indeed is a_{t+1} - b_{t+1} = (b_t + c_t) - (a_t + c_t) = b_t - a_t = -(a_t - b_t). So yes, it toggles sign each step.
Thus we don't need to worry about growth of a_t and b_t individually. The difference just flips sign.
But wait, there's a nuance: The operation is simultaneous. The recurrence holds because each updates based on old values. So indeed.
Therefore answer = (-1)^K * (A - B). Compute absolute value, if > 10^18, print "Unfair", else print the integer.
But we must be careful: K up to 1e18, so can't compute (-1)^K directly by exponentiation? We can just check parity: if K is even, sign = +; if odd, sign = -.
Now need to check absolute value condition: Since |A-B| can be as large as up to 1e9 (since A,B ≤ 1e9), difference magnitude ≤ 1e9, far less than 1e18. So answer's magnitude is same as |A-B|, which is ≤ 1e9, thus always ≤ 1e18. Actually if A and B can be up to 1e9, their difference max 1e9-1? Actually maximum difference: 1e9 - 1 = 999,999,999, but also maybe 0? So < 1e9. That's < 1e18. So condition is always satisfied? Wait, but problem statement says: "If the absolute value of the answer exceeds 10^{18}, print Unfair instead." But with given constraints, A and B ≤ 1e9, so |A-B| ≤ 1e9, thus answer magnitude ≤ 1e9, never exceed 1e18. So why would they include Unfair? Possibly because if we misinterpret and consider that after operations, the numbers themselves become huge and we might need to output the difference maybe after many operations? But we derived that d_K = (-1)^K d_0, so it doesn't grow. So it's always within range. But wait, there is nuance: What if K is huge and the numbers themselves exceed 10^18? But we only output the difference, which stays small. So maybe the Unfair condition is never triggered? But let's check: maybe the intention was to output the difference between Takahashi's integer and Nakahashi's integer (the actual values after K operations) and then if that difference's absolute value exceeds 10^18, output Unfair. Since it's always bounded by 1e9, it's fine. However, the constraints say A,B,C ≤ 1e9, but maybe there could be negative? Input says integers, but constraints say 1 ≤ A,B,C ≤ 10^9, so positive. So difference max ~1e9. So always safe. But perhaps they intended to consider that after K operations, the values might be huge and the difference could become huge? But our analysis shows difference doesn't grow. So indeed it's safe. But maybe there is a twist: The operation is repeated K times, and if at any point the numbers exceed 10^18? But the problem statement says: "if the absolute value of the answer exceeds 10^{18}, print Unfair instead." It's about the answer, not intermediate values. So indeed answer magnitude is limited. So maybe Unfair will never be printed. However, example 3 uses huge K but output 0, not Unfair. So consistent.
But wait, why include Unfair then? Possibly because there might be a different interpretation: Maybe after K operations, we compute Takahashi's integer minus Nakahashi's integer, but we must consider that these integers themselves might have grown beyond 10^18? But the answer is a difference of two huge numbers, which could also be huge. But we found it's just (-1)^K (A-B). However, is that always true? Let's check again: a_{t+1} = b_t + c_t, b_{t+1} = a_t + c_t. So a_{t+1} - b_{t+1} = b_t - a_t. That's correct regardless of c_t. So difference flips sign each step. So no growth. So indeed answer magnitude is exactly |A-B|. So for the given constraints, it's ≤ 10^9 - 1? Actually maximum difference: A can be 1, B can be 10^9, difference ≈ -999,999,999 absolute < 10^9. So less than 10^18. So Unfair never occurs.
But maybe the constraints originally allowed A, B, C up to 10^18? But problem statement says up to 10^9. Or maybe they allow negative? But still, if A and B could be up to 10^18, difference could be up to 2*10^18 and could exceed 10^18. But as given, it's safe. However, we should still implement the check.
Thus solution: read A, B, C, K. Compute diff = A - B. If K % 2 == 1: diff = -diff. If abs(diff) > 10**18: print "Unfair" else print diff.
But need to be careful with Python's large integers, but diff fits within int.
Edge case: K=0: diff = A-B.
We don't use C at all.
Double-check with some random test:
A=5, B=3, C=100, K=2: diff0=2, after even K, diff=2. Compute manually: step1: a=3+100=103, b=5+100=105, diff=-2; step2: a=105+? Wait need c1: c1 = a0+b0 = 5+3=8? Actually c1 = a0+b0 = 5+3=8. So a2 = b1 + c1 = 105+8=113, b2 = a1 + c1 = 103+8=111, diff=2. So matches.
Thus solution trivial.
But wait, is there any subtlety about simultaneous update? Already accounted.
Thus answer is straightforward.
But wait, reading problem statement: "Takahashi, Nakahashi and Hikuhashi have integers A, B and C, respectively. After repeating the following operation K times, find the integer Takahashi will get minus the integer Nakahashi will have:" So they want after K operations, compute A_K - B_K. Yes.
Thus implement.
Now constraints: K up to 1e18, can't loop.
Now output: if abs(diff) > 1e18, print "Unfair". But diff = (-1)^K (A-B). Since |A-B| <= 10^9, it's always <= 10^9 < 10^18. So never unfair. But still implement.
But maybe I'm missing something: Could the difference become large due to overflow? But no, it's derived exactly.
Let's also compute c_t maybe but not needed.
Thus answer is simple.
But is there any chance that the operation might be defined differently? "Each of them simultaneously calculate the sum of the integers that the other two people have, then replace his own integer with the result." That's exactly what we used.
Thus final answer: read, compute sign = -1 if K%2 else 1, ans = sign*(A-B). If abs(ans) > 10**18: print "Unfair" else print ans.
Given the constraints, but to be safe, we can use Python's big ints.
Let's check sample 1: A=1,B=2,K=1: sign=-1, ans=-1*( -1?) Actually A-B = -1, sign=-1 -> ans = 1. Print 1. ok.
Sample 2: A=2,B=3,K=0: sign=1, ans=-1. Print -1. ok.
Sample 3: A=1e9,B=1e9 => diff=0, ans=0. Print 0.
Alright.
But wait, sample 3: "1000000000 1000000000 1000000000 1000000000000000000" => diff=0, so 0. Output 0. matches.
Thus done.
But hold on, why is C given? Not used. Possibly to distract.
Thus code is trivial.
Now I'm ready to answer.
But perhaps I must consider: The operation is done K times, but what if K is 0? Then answer is A-B.
Now check Unfair condition: If absolute > 1e18. Since |A-B| ≤ 1e9, it's safe.
Thus solution.
But let's consider extreme: A=1e9, B=1, diff=999,999,999 < 1e9 < 1e18. So safe.
Thus implement.
But maybe there is a trap: In some languages, you might need to handle large K modulo 2, but K fits in 64-bit.
Thus answer.
But I should also note: The problem is from AtCoder? I recall AtCoder ABC 113? Actually there is a problem "A - ..."? Let me think: I recall a problem: "Takahashi and Aizu" etc. There is AtCoder Beginner Contest 113 B? Not sure. But this problem looks like from AtCoder ABC 042? Actually I recall a problem: "A - ..." with three people and operation. The solution is indeed just parity. Many people got tricked by C but it's not needed.
Thus final answer.
Now I'll produce final solution.
But I'll double-check if there's any possibility that the answer could become Unfair due to the values being huge before difference? But difference is small.
What if A and B could be up to 10^18? The constraints say 1e9. So not.
Thus done.
Now writing solution: In Python:
def solve():
import sys
A, B, C, K = map(int, sys.stdin.read().split())
diff = A - B
if K % 2 == 1:
diff = -diff
if abs(diff) > 10**18:
print("Unfair")
else:
print(diff)
That's it.
But wait, note: K can be huge, but we can take K % 2 easily.
Thus final answer.
But maybe we should treat K as int, but Python handles.
Now I'll produce the final answer. ```python
import sys
def solve() -> None:
A, B, C, K = map(int, sys.stdin.read().split())
diff = A - B
if K % 2 == 1:
diff = -diff
if abs(diff) > 10**18:
print("Unfair")
else:
print(diff)
if name == "main":
solve()