ZOJ-3647 Gao the Grid
来源:互联网 发布:电信网络字母 编辑:程序博客网 时间:2024/05/02 01:26
Gao the Grid
Time Limit: 2 Seconds Memory Limit: 65536 KB
A n * m grid as follow:
a n*m grid(n=4,m=3)
Count the number of triangles, three of whose vertice must be grid-points.
Note that the three vertice of the triangle must not be in a line(the right picture is not a triangle).
a triangle not a triangle
Input
The input consists of several cases. Each case consists of two positive integers n and m (1 ≤ n, m ≤ 1000).
Output
For each case, output the total number of triangle.
Sample Input
1 1
2 2
Sample Output
4
76
hint
hint
for 2nd case: C(9, 3) - 8 = 76
题目大意:给出n,m计算在格顶上的三角形有多少个。
解题关键:先算出C(全部格点数,3)然后减去直线的情况,gcd(i,j) - 1 计算出i,j(直角三角形的两条边)其斜边落在格点上的点数有几个。
题目链接:ZOJ 3647
以下是代码:
#include <vector>#include <map>#include <set>#include <algorithm>#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>#include <string>#include <cstring>using namespace std;long long gcd(int a,int b){ return b == 0 ? a : gcd(b,a % b);} long long com(long long n, long long r){ if (n < r) return 0; if( n-r < r ) r = n-r; long long i, j, s = 1; for( i=0, j=1; i <r; ++i ){ s *= (n-i); for( ; j <= r&& s%j == 0; ++j ) s /= j; } return s; }int main(){ int n,m; while(cin >> n >> m) { long long ans = com((n + 1) * (m + 1),3) - com(n + 1,3) * (m + 1) - com (m + 1,3) * (n + 1); for (int i = 2; i <= n; i++) { for (int j = 2; j <= m; j++) { ans = ans- (gcd(i,j) - 1) * (n - i + 1) * (m - j + 1) * 2; } } cout << ans << endl;} return 0;}
0 0
- ZOJ 3647 Gao the Grid
- ZOJ 3647 Gao the Grid
- zoj 3647 Gao the Grid
- ZOJ-3647 Gao the Grid
- ZOJ 3647 Gao the Grid
- Zoj 3647 Gao the Grid (数论?)
- ZOJ 3647 Gao the Grid(居然是暴力)
- ZOJ 3647 Gao the Grid (计数 + pick定理)
- ZOJ 3648 Gao the Grid II
- Zoj 3647 Gao the Grid ( 2012年浙大9月月赛)组合数学
- zoj 3647 Gao the Grid(矩阵对角线三点共线)
- zoj 3647 Gao the Grid (求三角形的个数,困难)
- zoj 3672 Gao The Sequence
- zoj - 3672 - Gao The Sequence
- ZOJ 3672 Gao The Sequence
- Gao the Grid zoj3647(数论)
- ZOJ 3535 Gao the String II
- zoj3647 Gao the Grid----方格中的三点共线
- 关于B-K tree与k-d tree一些自己想法
- 4.21-4.22
- Discrete Mathematics and Its applications - 离散数学及其应用 读书笔记 ( 一 )
- linux——3.文件操作
- 编程珠玑(第二版)读书笔记【1、2】
- ZOJ-3647 Gao the Grid
- Jedis 增删改查
- CSS3属性text-overflow
- VIM会话-窗口布局
- 实验三
- jquery frre
- uva 11997 k个最小和(优先队列实现多路归并)题解
- http://blog.csdn.net/qjzl2008/article/details/7707465
- Tomcat的管道