ZOJ 3647 Gao the Grid (计数 + pick定理)
2015-04-15 17:16
337 查看
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3647
题意:给一个n * m的矩形,求出三个点都落在该矩形格点上的三角形的个数
思路:先算出任取三点的数目,再减去两种共线的情况:
1.三个点落在同一行或者同一列
2.三个点落在斜线上,可以枚举矩形,然后固定原点和(i, j)点(相当于这条线段的始末点)再用pick定理算出这条线段上整点数目,统计和这条线段平行的线段在整个矩形中出现的次数即可
题意:给一个n * m的矩形,求出三个点都落在该矩形格点上的三角形的个数
思路:先算出任取三点的数目,再减去两种共线的情况:
1.三个点落在同一行或者同一列
2.三个点落在斜线上,可以枚举矩形,然后固定原点和(i, j)点(相当于这条线段的始末点)再用pick定理算出这条线段上整点数目,统计和这条线段平行的线段在整个矩形中出现的次数即可
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <vector> #include <utility> #include <functional> #include <string> #include <set> #include <map> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; const int maxn = 50010; typedef long long ll; int n, m; int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } ll C(ll x) { if (x <= 2) return 0; return x * (x - 1) * (x - 2) / 6; } int main() { while (cin >> n >> m) { ll ans = C((n + 1) * (m + 1)); ans -= (m + 1) * C(n + 1) + (n + 1) * C(m + 1); for (int i = 2; i <= n; i++) { for (int j = 2; j <= m; j++) { int t = gcd(i, j) - 1; ll ttt = t * (m + 1 - j) * (n + 1 - i); ans -= ttt * 2; } } cout << ans << endl; } return 0; }
相关文章推荐
- zoj 3647 Gao the Grid
- ZOJ 3647 Gao the Grid dp,思路,格中取同一行的三点,经典 难度:3
- 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
- Gao the Grid ZOJ 3647 数三角形
- ZOJ 3647 Gao the Grid
- zoj 3647 Gao the Grid (求三角形的个数,困难)
- ZOJ 3648 Gao the Grid II
- zoj - 3672 - Gao The Sequence
- ZOJ 3781 Paint the Grid Reloaded 题解 (dfs缩点+BFS)
- ZOJ 3781 Paint the Grid Reloaded (缩点,bfs)
- ZOJ-3781-Paint the Grid Reloaded【11th浙江省赛】【连通块缩点】【spfa】【好题】
- ZOJ Paint the Grid Reloaded
- zoj 3535 Gao the String II(AC自动机+DP)
- ZOJ 3780 Paint the Grid Again
- ZOJ 3780 Paint the Grid Again 模拟
- ZOJ 3672 Gao The Sequence