Bzoj 3505: [cqoi2014] Number-triangle combination mathematics

Source: Internet
Author: User
3505: [cqoi2014] triangle time limit: 10 sec memory limit: 128 MB
Submit: 478 solved: 293
[Submit] [Status] Description

For a given nxm mesh, calculate the total number of triangles on the three points. It is a triangle on a 4x4 mesh.

Note that the three points of a triangle cannot be collocated.

Input

Enter a line that contains two spaces to separate the positive integers m and n.

Output


Returns a positive integer representing the number of triangles.

Sample Input
2 2
Sample output76


Data range
1 <= m, n <= 1000
Hint Source

I was so idiotic that I got stuck in the number of three-point collinearity groups. At first, I thought I could enumerate the straight lines of each slope and calculate their number and intersection, so far, I still don't know how to do this. The correct method should be to enumerate the Coordinate Difference between the two points of the Three-Point collinearity, and then we can easily find the logarithm of the endpoint and the number of the center point, you can solve this problem.

#include<iostream>#include<cstring>#include<algorithm>#include<cstdio>using namespace std;#define MAXN 10000typedef long long qword;int gcd(int x,int y){        return (x%y==0)?y:gcd(y,x%y);}int main(){        freopen("input.txt","r",stdin);//        freopen("output.txt","w",stdout);        int i,j,k,x,y,z;        int n,m;        scanf("%d%d",&n,&m);        n++;m++;        int tot=0;        qword ans0,ans1=0;        ans0=(qword)(m*n-2)*(m*n-1)*m*n/6;        for (i=1;i<n;i++)        {                for (j=1;j<m;j++)                {                        ans1+=(qword)(gcd(i,j)-1)*(n-i)*(m-j);                }        }        ans1*=2;        ans1+=(qword)(n-2)*(n-1)*n/6*m;        ans1+=(qword)(m-2)*(m-1)*m/6*n;        printf("%lld\n",ans0-ans1);}

 

 

Bzoj 3505: [cqoi2014] Number-triangle combination mathematics

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.