Ultraviolet A 12508-Triangles in the grid (ry + count)

Source: Internet
Author: User

Link: Ultraviolet A 12508-Triangles in the grid

N, m, A, and B are given, which must be calculated in (n + 1 )? On the (m + 1) matrix, you can find the number of triangles with an area between AB.

Solution: first, enumerate the matrix, and then calculate the number of triangles using this matrix as an external matrix, and the size must be between AB. Then, for each matrix, you can determine a few within a large range.

The inner triangle of the enumeration matrix can be divided into three types:
1. The two points of the triangle shape are located at the vertex of a matrix edge, and the other point is located at the peer edge of the edge (excluding the vertex)


2. Side of a triangle with diagonal lines


In this way, we can enumerate X and then obtain the boundary values of L and R.

3. The triangle points are on the vertex of the rectangle, and the other two points are on the corresponding edge.


X is also enumerated, but this time x cannot include 0 and N (calculated in Case 2), corresponding to the red triangle and the blue triangle, the area is reduced by X, therefore, we can calculate the number of triangles.

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;inline ll max(ll a, ll b) {    return a > b ? a : b;}inline ll min(ll a, ll b) {    return a < b ? a : b;}ll N, M, A, B;ll solve (ll k) {    if (k < 0)        k = 0;    if (N > M)        swap(N, M);    ll ans = 0;    for (ll n = 1; n <= N; n++) {        for (ll m = 1; m <= M; m++) {            ll cnt = 0;            if (n * m <= k)                cnt += 2 * (n + m - 2);            ll l, r;            for (ll x = 0; x <= n; x ++) {                r = (m * x + k) / n;                if (r > m)                    r = m;                ll t = m * x - k;                if(t <= 0)                     l = 0;                else                     l = (t - 1) / n + 1;                if(l <= r)                     cnt += 2 * (r - l + 1);            }            for (ll x = 1; x < n; x++) {                ll tmp = n * m - x;                if (tmp <= k)                    cnt += 4 * (m - 1);                else {                    tmp = tmp - k;                    ll u = m-1 - min(tmp / x + (tmp % x != 0), m-1);                    cnt += 4 * u;                }            }            ans += cnt * (N - n + 1) * (M - m + 1);        }    }    return ans;}int main () {    int cas;    scanf("%d", &cas);    while (cas--) {        scanf("%lld%lld%lld%lld", &N, &M, &A, &B);        printf("%lld\n", solve(B*2) - solve(A*2-1));    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.