-
Notifications
You must be signed in to change notification settings - Fork 7
/
crophard.py
17 lines (17 loc) · 1.18 KB
/
crophard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
check = [((0, 0), (0, 0), (0, 0)), ((0, 0), (0, 1), (0, 2)), ((0, 0), (1, 0), (2, 0)), ((0, 0), (1, 1), (2, 2)), ((0, 0), (1, 2), (2, 1)), ((0, 1), (0, 1), (0, 1)), ((0, 1), (1, 0), (2, 2)), ((0, 1), (1, 1), (2, 1)), ((0, 1), (1, 2), (2, 0)), ((0, 2), (0, 2), (0, 2)), ((0, 2), (1, 0), (2, 1)), ((0, 2), (1, 1), (2, 0)), ((0, 2), (1, 2), (2, 2)), ((1, 0), (1, 0), (1, 0)), ((1, 0), (1, 1), (1, 2)), ((1, 1), (1, 1), (1, 1)), ((1, 2), (1, 2), (1, 2)), ((2, 0), (2, 0), (2, 0)), ((2, 0), (2, 1), (2, 2)), ((2, 1), (2, 1), (2, 1)), ((2, 2), (2, 2), (2, 2))]
for t in range(int(input())):
n, a, b, c, d, x, y, m = map(int, input().split())
v = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
v[x%3][y%3] += 1
for _ in range(n-1):
x, y = (a*x+b)%m, (c*y+d)%m
v[x%3][y%3] += 1
ans = 0
for (e, f), (g, h), (i, j) in check:
p, q, r = v[e][f], v[g][h], v[i][j]
if (e, f) == (g, h) == (i, j): ans += p*(p-1)*(p-2)//6
elif (e, f) == (g, h) != (i, j): ans += p*(p-1)//2*r
elif (e, f) != (g, h) == (i, j): ans += q*(q-1)//2*p
elif (g, h) != (e, f) == (i, j): ans += p*(p-1)//2*q
else: ans += p*q*r
print(f'Case #{t+1}:', ans)