bzoj3505

枚举对角线。。。

 1 #include<bits/stdc++.h>
 2 #define lowbit(a) ((a)&(-(a)))
 3 #define clr(a,x) memset(a,x,sizeof(a))
 4 #define rep(i,l,r) for(int i=l;i<(r);i++)
 5 typedef long long ll;
 6 using namespace std;
 7 int read()
 8 {
 9     char c=getchar();
10     int ans=0,f=1;
11     while(!isdigit(c)){
12         if(c=='-') f=-1;
13         c=getchar();
14     }
15     while(isdigit(c)){
16         ans=ans*10+c-'0';
17         c=getchar();
18     }
19     return ans*f;
20 }
21 int n,m;
22 ll gcd(ll a,ll b){
23     return b?gcd(b,a%b):a;
24 }
25 ll f(ll x){
26     return x*(x-1)*(x-2)/6;
27 }
28 int main()
29 {    
30     n=read();m=read();++n;++m;
31     ll ans=f(n*m)-ll(n)*f(m)-ll(m)*f(n);
32     rep(i,1,n){
33         rep(j,1,m){
34             ans-=(gcd(i,j)-1)*(n-i)*(m-j)<<1;
35         }
36     }
37     cout<<ans<<endl;
38     return 0;
39 }
View Code

3505: [Cqoi2014]数三角形

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 771  Solved: 471
[Submit][Status][Discuss]

Description

给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个。下图为4x4的网格上的一个三角形。

注意三角形的三点不能共线。

Input

输入一行,包含两个空格分隔的正整数m和n。

Output


输出一个正整数,为所求三角形数量。

Sample Input


2 2

Sample Output

76


数据范围
1<=m,n<=1000

HINT

 

Source

posted @ 2015-09-04 20:21  ChenThree  阅读(121)  评论(0编辑  收藏  举报