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