[CQOI2014] explanations triangular numbers (to find the law promiscuity)

Face questions

This question is not in fact the number of combinations! Without the inclusion-exclusion!

Only one gcd and no brains to find the law (funny

At first glance the title, if you simply seek legal triangle, then too many cases too complicated, we can start from the local, and ultimately extended to the whole.

First, consider the case:

Similarly, we are at the three vertices of the triangle mesh boundary, and any one of the lines in a triangular grid can be cut into two parts, called completely covered.

This is not just following:

Difficult to find on each vertex of the triangle lattice point, and only one has been completely covered by its grid.
It may be converted to the original problem: obtaining all rectangular sub-rectangle completely cover triangular number.

And because the number of triangles completely covered with only the size of the sub-rectangle, regardless of its position,

And what can be found Shoumo

A \ (n * m \) within the rectangle, the size \ (i * j \) the number of sub-rectangles \ ((n-I-+. 1) * (m +-J. 1) \) .

Then the number so long as to completely cover the triangular solve the length and width of a rectangle within a

Then observe available (So ​​far I seem to be of no use except to observe methods proven thing)

If the triangle XYZ rectangular ABCD completely covered, then it has at least one end point of ABCD on the corner.

Then the next can have several endpoints classified discussions on the corner of the rectangle in accordance with XYZ.
It is set long rectangle i, width j.

  • An endpoint at the corners

There are four selector angle triangle the other two ends will be on either side of the point, a total of \ ((i-1) * (j-1) \) species.

So this part of the answer is \ (4 * (i-1 ) * (j-1) \)

  • Two end points at the corners

The first:

The answer: \ (i-1 \)

The second:

The answer: \ (J-1 \)

Third:

Triangle has a diagonal of the rectangle coincides with the edge.

此时三角形剩下那个端点除了四个角以及它的对边上的格点之外,可以随便放。
那么这条对边(即矩形的一条对角线)上有几个格点呢?

\(gcd(i,j)-1\)个。(不包括对边的两个端点)

答案:\((i+1)*(j+1)-4-gcd(i,j)+1\)

  • 三个端点在角上

显然4种。

另外,以上三种情况都可以对称过去得到不同的方案,所以\(*2\)

化简可得\(ans=6*i*j-2*gcd(i,j)\)

复杂度:\(O(mnlog^{m+n})\)

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
ll m,n;
int gcd(int x,int y)
{
    if(!y)return x;
    return gcd(y,x%y);
}
int main()
{
    scanf("%lld%lld",&m,&n);
    ll ans=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            ans+=(6*i*j-2LL*gcd(i,j))*(n-i+1)*(m-j+1);
    cout<<ans<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/Rorschach-XR/p/11233780.html