题意

\(n * m(1 \le n, m \le 1000)\)的网格,求顶点在格点上三角形的个数。

分析

假设\(n \le m\)

\(ans = \binom{(n+1) * (m+1)}{3} - L\),其中\(L\)表示三点共线的方案数。

所以

$$
\begin{align}
L
& = \frac{1}{2} \sum_{dx=0}^{n} \sum_{dy=0}^{m} \sum_{fx=0}^{n} \sum_{fy=0}^{m} (gcd(|dx-fx|, |dy-fy|)-1) \\
& = 2 \sum_{x=0}^{n} \sum_{y=0}^{m} \sum_{i=1}^{x} \sum_{j=1}^{y} (gcd(i, j)-1) + (m+1) \binom{n+1}{3} + (n+1) \binom{m+1}{3} \\
& = 2 f(n, m) + (m+1) \binom{n+1}{3} + (n+1) \binom{m+1}{3} \\
\\
f(n, m)
& = \sum_{x=0}^{n} \sum_{y=0}^{m} \sum_{i=0}^{x} \sum_{j=0}^{y} (gcd(i, j)-1) \\
& = \sum_{x=0}^{n} \sum_{y=0}^{m} \left( \sum_{i=0}^{x} \sum_{j=0}^{y} gcd(i, j) - x * y \right) \\
& = \sum_{x=0}^{n} \sum_{y=0}^{m} (g(x, y) - x * y) \\
\\
g(n, m)
& = \sum_{i=0}^{n} \sum_{j=0}^{m} gcd(i, j) \\
& = g(n, m-1) + \sum_{i=0}^{n} gcd(i, m) \\
& = g(n, m-1) + h(n, m) \\
\\
h(n, m)
& = \sum_{i=0}^{n} gcd(i, m) \\
& = h(n-1, m) + gcd(n, m) \\
\end{align}
$$

用这个$O(n^2 log n)$的是可以过的,所以就不用推下去了。

题解

分析已经推出一个\(O(n^2 log n)\)的做法,更优做法请自己推~

#include <bits/stdc++.h>
using namespace std;
const int N=1005;
typedef long long ll;
int gcd(int a, int b) {
return b?gcd(b, a%b):a;
}
int n, m;
ll G[N][N], f[N][N];
int main() {
scanf("%d%d", &n, &m);
ll ans=0, t=(n+1)*(m+1);
ans=(t*(t-1)*(t-2)-(ll)(n+1)*n*(n-1)*(m+1)-(ll)(m+1)*m*(m-1)*(n+1))/6;
for(int i=1; i<=n; ++i) {
for(int j=1; j<=m; ++j) {
f[i][j]=f[i-1][j]+gcd(i, j);
G[i][j]=G[i][j-1]+f[i][j];
}
}
t=0;
for(int x=0; x<=n; ++x) {
for(int y=0; y<=m; ++y) {
t+=G[x][y]-x*y;
}
}
printf("%lld\n", ans-t*2);
return 0;
}

【BZOJ】3505: [Cqoi2014]数三角形的更多相关文章

  1. BZOJ 3505: [Cqoi2014]数三角形 数学

    3505: [Cqoi2014]数三角形 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...

  2. Bzoj 3505: [Cqoi2014]数三角形 数论

    3505: [Cqoi2014]数三角形 Time Limits: 1000 ms  Memory Limits: 524288 KB  Detailed Limits   Description

  3. bzoj 3505: [Cqoi2014]数三角形 组合数学

    3505: [Cqoi2014]数三角形 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 478  Solved: 293[Submit][Status ...

  4. BZOJ 3505: [Cqoi2014]数三角形( 组合数 )

    先n++, m++ 显然答案就是C(3, n*m) - m*C(3, n) - n*C(3, m) - cnt. 表示在全部点中选出3个的方案减去不合法的, 同一行/列的不合法方案很好求, 对角线的不 ...

  5. BZOJ 3505: [Cqoi2014]数三角形 [组合计数]

    3505: [Cqoi2014]数三角形 给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个. 注意三角形的三点不能共线. 1<=m,n<=1000 $n++ m++$ $ans ...

  6. BZOJ 3505 [Cqoi2014]数三角形

    3505: [Cqoi2014]数三角形 Description 给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个.下图为4x4的网格上的一个三角形.注意三角形的三点不能共线. Input ...

  7. bzoj 3505 [Cqoi2014]数三角形(组合计数)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3505 [题意] 在n个格子中任选3点构成三角形的方案数. [思路] 任选3点-3点共线 ...

  8. BZOJ 3505 [Cqoi2014]数三角形(组合数学)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3505 [题目大意] 给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个. 注 ...

  9. bzoj 3505 [Cqoi2014]数三角形——排列组合

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3505 好题!一定要经常回顾! 那个 一条斜线上的点的个数是其两端点横坐标之差和纵坐标之差的g ...

  10. bzoj 3505 [Cqoi2014]数三角形 组合

    ans=所有的三点排列-共行的-共列的-斜着一条线的 斜着的枚举每个点和原点的gcd,反过来也可以,还能左右,上下挪 #include<cstdio> #include<cstrin ...

随机推荐

  1. json 对象 字符串 转换

    json字符串转json对象:jQuery.parseJSON(jsonStr); json对象转json字符串:JSON.stringify(jsonObj);

  2. C++项目中的extern "C" {}

    from:http://www.cnblogs.com/skynet/archive/2010/07/10/1774964.html C++项目中的extern "C" {} 20 ...

  3. CSS隐藏多余文字的几个方法

    通常偏移掉字体的方式是 (1)使用text-indent:-9999px; 可是他有一个局限性 他只适用于块级元素block而我们往往有时候想偏移掉的a上的字体所以问题就来了text-indent:- ...

  4. Node.js Stream - 实战篇

    邹斌 ·2016-07-22 11:04 背景 前面两篇(基础篇和进阶篇)主要介绍流的基本用法和原理,本篇从应用的角度,介绍如何使用管道进行程序设计,主要内容包括: 管道的概念 Browserify的 ...

  5. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  6. 提高PHP代码质量的36个技巧

    1.不要使用相对路径 常常会看到: require_once('../../lib/some_class.php'); 该方法有很多缺点: 它首先查找指定的php包含路径, 然后查找当前目录. 因此会 ...

  7. JBOSS 5 session时间配置

    C:\jboss-5.1.0.GA\server\default\deployers\jbossweb.deployer web.xml <session-config>     < ...

  8. exception catch doesn't work?? (python 3)

    exception catch doesn't work?? (python 3) except u.URLError, e: ^ SyntaxError: invalid syntax in Pyt ...

  9. python __call__ 内置函数的使用

    对象通过提供__call__(slef, [,*args [,**kwargs]])方法可以模拟函数的行为, 如果一个对象x提供了该方法,就可以像函数一样使用它,也就是说x(arg1, arg2... ...

  10. c#.net 生成清晰缩略图

    1 public void imgsize() 2 { 3 //本例中假定了两个变量: 4 5 String src = "c:/myImages/a.jpg"; //源图像文件的 ...