int n; voidsolve() { int n, m, k; cin >> n >> m >> k; int ans = 0; repp(i1, 0, n) { repp(j1, 0, m) { repp(i2, i1, n) { repp(j2, (i2==i1?j1:0), m) { int d1 = abs(i1 - i2); int d2 = abs(j1 - j2); int cnt = 0; if (d1 == 0) { cnt = d2 + 1; } elseif (d2 == 0) { cnt = d1 + 1; } else { cnt = gcd(d1, d2) + 1; } if (cnt == k) { ans++; } } } } } cout << ans ; }
C Right Triangles
Given $N$ points $(x,y)$, each point together with $(0,0)$ and $(x,0)$ forms a right triangle. For every such triangle, count how many points it contains.
Sort by x-coordinate, then count earlier points with a smaller slope.
There is an array from $1$ to $N$ $(N < 10^{18})$. Compute the sum of products of all contiguous subarrays with length at most $K$.
The sum of products of consecutive groups of $K$ numbers from $1$ to $n$ is $$ \frac{(n-i+1)(n-i+2)…(n+1)}{(i+1)} $$ For example 1*2 + 2*3 + 3*4 + 4*5 + … (n-1)*n = (n-1)*n*(n+1)/2
Then I used Python directly for this problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#import sys
defcal(n,x): ans = 1 for i inrange(x + 1): ans *= (n + 1 - i) return ans / (x + 1)
if __name__ == "__main__": n,m,k = map(int,raw_input().split()) ans = 0 for i inrange(1,m+1): ans += cal(n,i) ans %= k print ans