bzoj 3505 数三角形 - 组合数学
给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个。下图为4x4的网格上的一个三角形。
注意三角形的三点不能共线。
Input
输入一行,包含两个空格分隔的正整数m和n。
Output
输出一个正整数,为所求三角形数量。
Sample Input
2 2
Sample Output
76
Hint
数据范围
1<=m,n<=1000
不难得到一个思路。最终的答案 = 任选3点的方案数 - 三点共线的方案数。
前者很好求直接组合数就好了。后者可以枚举线段两端点,然后计算第三个点在这个线段上(不含端点)的方案数,这要用到在网格图中一个神奇的结论,两个格点点的连线穿过的格点数等于这两点的横纵坐标之差的最大公约数减一,至于它的证明可以用相似再加一点数论的东西。但是枚举任意两点会超时。但发现很多地方其实这个横纵坐标的差是相等的,所以直接枚举这个横纵坐标之差,然后乘法乘一乘就好了。
Code
/**
* bzoj
* Problem#3505
* Accepted
* Time:344ms
* Memory:1288k
*/
#include<iostream>
#include<cstdio>
#include<ctime>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#include<stack>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean;
const signed int inf = (signed)((1u << ) - );
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} template<typename T>
T gcd(T a, T b) {
if(!b) return a;
return gcd(b, a % b);
} int n, m;
int pro;
long long res; inline void init() {
readInteger(n);
readInteger(m);
pro = (n + ) * (m + );
res = pro * 1LL * (pro - ) * (pro - ) / ;
} inline void solve() {
for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
if(i == && j == ) continue;
long long t = (gcd(i, j) - 1LL) * (n - i + ) * (m - j + );
if(!i || !j) res -= t;
else res -= (t << );
}
}
printf(Auto, res);
} int main() {
init();
solve();
return ;
}
bzoj 3505 数三角形 - 组合数学的更多相关文章
- bzoj 3505: [Cqoi2014]数三角形 组合数学
3505: [Cqoi2014]数三角形 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 478 Solved: 293[Submit][Status ...
- [BZOJ][CQOI2014]数三角形
Description 给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个.下图为4x4的网格上的一个三角形. 注意三角形的三点不能共线. Input 输入一行,包含两个空格分隔的正整数m和 ...
- Luogu P3166 [CQOI2014]数三角形 组合数学
好题鸭.. 不好直接求三角形个数,那就用全集-补集,转化为求三点共线的数量. 具体求法是求出水平共线数量与竖直共线数量和斜线共线数量. 用排列组合的知识可知为水平和竖直的为$C_n^3$与$C_m^ ...
- BZOJ 3505: [Cqoi2014]数三角形 数学
3505: [Cqoi2014]数三角形 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...
- Bzoj 3505: [Cqoi2014]数三角形 数论
3505: [Cqoi2014]数三角形 Time Limits: 1000 ms Memory Limits: 524288 KB Detailed Limits Description
- BZOJ 3505: [Cqoi2014]数三角形( 组合数 )
先n++, m++ 显然答案就是C(3, n*m) - m*C(3, n) - n*C(3, m) - cnt. 表示在全部点中选出3个的方案减去不合法的, 同一行/列的不合法方案很好求, 对角线的不 ...
- BZOJ 3505: [Cqoi2014]数三角形 [组合计数]
3505: [Cqoi2014]数三角形 给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个. 注意三角形的三点不能共线. 1<=m,n<=1000 $n++ m++$ $ans ...
- BZOJ 3505 [Cqoi2014]数三角形
3505: [Cqoi2014]数三角形 Description 给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个.下图为4x4的网格上的一个三角形.注意三角形的三点不能共线. Input ...
- [CQOI2014]数三角形 题解(组合数学+容斥)
[CQOI2014]数三角形 题解(数论+容斥) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1328780 链接题目地址:洛谷P3166 BZOJ 350 ...
随机推荐
- bin/hdfs dfs命令存在WARN util.NativeCodeLoader问题消除方法
例如:[hadoop@db01 hadoop-2.5.0]$ bin/hdfs dfs -ls17/03/01 21:50:33 WARN util.NativeCodeLoader: Unable ...
- linux:查找搜索文件
学习内容来源: 实验楼链接: https://www.shiyanlou.com/ 与搜索相关的命令常用的有 whereis,which,find 和 locate . whereis 简单快速 $w ...
- 不移除通知的话,出现的bug
没销毁的私聊控制器仍然监听到了通知,发送表情
- OKEx货币对价格数量长度及精度
长度 precisions = [["bch_btc","0.001","0.00000001"], ["ltc_btc" ...
- Python开发【笔记】:什么是RESTful框架
RESTful框架 前言: 一句话总结:用URL定位资源,用HTTP描述操作 越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件"采用客户端/服务器模式 ...
- 启动HDFS
$cd /app/hadoop/hadoop-2.2.0/sbin $./start-dfs.sh
- [py]可迭代对象-求最值
for .. in ..方式遍历可迭代对象 而非下标 ## 判断是否可迭代 from collections import Iterable print(isinstance(123,Iterable ...
- shell脚本变量$#,$*,$$,$@,$0,$1,$2,$?的含义
本文转录:https://blog.csdn.net/kejiaming/article/details/51859503 参数说明: $# 是传给脚本的参数个数 $ 是脚本本身的名字 $ 是传递给该 ...
- python 根据路径导入模块
Import python module NOT on path http://stackoverflow.com/questions/10161568/import-python-module-no ...
- (转)Elasticsearch NoNodeAvailableException None of the configured nodes are available
问题背景:将es部署到内网中两台服务器,其Ip地址分别为:192.111.222.5,192.111.222.1(部署方式完全一样,是将192.111.222.1服务器上es整个部署包,拷贝到了192 ...