Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add kattis prob cropeasy in python3 + notes
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
We do not care about individual points. We only care about those point coordinates mod 3. This is because a valid triangle is one where the sums of X coordinates and the sums of Y coordinates are both equal to zero mod 3. | ||
|
||
Thus, we only keep track of the counts of coordinates with given coordinate values mod 3. | ||
|
||
For each loop from 0 to 9, we map each of these values to an X and Y mod 3. | ||
|
||
In the outermost loop, we check if we can combine 3 points with the same mod values in their coordinates. If N points have these exact coordinate mod values, then there are (N) * (N-1) * (N-2) combinations of these points. Divide this number by 6 (ie. 3 factorial) to get the number of unique combinations of these points. | ||
|
||
Then in the next loop, we check if we can combine 2 points with the same mod values, and one other point. We check if we have enough points, and if the sums of their coordinates are equal to zero mod 3. We create a similar product as above, and divide by 2 (ie. 2!), as this is the number of ways of arranging them, to get the number of unique combinations. | ||
|
||
Finally, in the innermost loop, we check if we can combine 3 points with unique coordinate values mod 3. | ||
|
||
`cropeasy` runs in 0.2s in python3 with this method, and `crophard` in 0.22s. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
t = int(input()) | ||
|
||
for test in range(1, t+1): | ||
|
||
n, a, b, c, d, x, y, m = map(int, input().split()) | ||
|
||
pts = [[0 for _ in range(3)] for _ in range(3)] | ||
for _ in range(n): | ||
pts[x%3][y%3] += 1 | ||
x = (a*x + b) % m | ||
y = (c*y + d) % m | ||
|
||
ans = 0 | ||
|
||
for a in range(9): | ||
i = a % 3 | ||
j = a // 3 | ||
if pts[i][j] >= 3: | ||
ans += (pts[i][j] * (pts[i][j] - 1) * (pts[i][j] - 2)) // 6 | ||
okFor2 = pts[i][j] >= 2 | ||
for b in range(a+1, 9): | ||
k = b % 3 | ||
l = b // 3 | ||
if okFor2 and ((i + i + k) % 3) == 0 and ((j + j + l) % 3) == 0: | ||
ans += (pts[i][j] * (pts[i][j] - 1) * (pts[k][l])) // 2 | ||
for c in range(b+1, 9): | ||
m = c % 3 | ||
n = c // 3 | ||
if ((i + k + m) % 3) == 0 and ((j + l + n) % 3) == 0: | ||
ans += pts[i][j] * pts[k][l] * pts[m][n] | ||
|
||
|
||
print('Case #{}: {}'.format(test, ans)) |