BZOJ 3505 [Cqoi2014]数三角形(组合数学)
【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=3505
【题目大意】
给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个。
注意三角形的三点不能共线。
【题解】
我们计算三个点组合的情况,去除横竖三共线,以及斜着三点共线的情况即可。
一个矩形斜对角上的整点数为其长宽的最大公约数+1.
【代码】
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
LL c[1001000][4],ans;
int n,m;
void init(){
c[0][0]=1;
for(int i=1;i<=n*m;i++){
c[i][0]=1;
for(int j=1;j<=3;j++)c[i][j]=c[i-1][j-1]+c[i-1][j];
}
}
void solve(){
ans=c[n*m][3]-n*c[m][3]-m*c[n][3];
for(int i=1;i<n;i++)for(int j=1;j<m;j++){
LL t=__gcd(i,j)+1;
if(t>2)ans-=(t-2)*2*(n-i)*(m-j);
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
ans=0;n++;m++;
init(); solve();
printf("%lld\n",ans);
}return 0;
}
BZOJ 3505 [Cqoi2014]数三角形(组合数学)的更多相关文章
- bzoj 3505: [Cqoi2014]数三角形 组合数学
3505: [Cqoi2014]数三角形 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 478 Solved: 293[Submit][Status ...
- 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 ...
- bzoj 3505 [Cqoi2014]数三角形(组合计数)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3505 [题意] 在n个格子中任选3点构成三角形的方案数. [思路] 任选3点-3点共线 ...
- bzoj 3505 [Cqoi2014]数三角形——排列组合
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3505 好题!一定要经常回顾! 那个 一条斜线上的点的个数是其两端点横坐标之差和纵坐标之差的g ...
- bzoj 3505 [Cqoi2014]数三角形 组合
ans=所有的三点排列-共行的-共列的-斜着一条线的 斜着的枚举每个点和原点的gcd,反过来也可以,还能左右,上下挪 #include<cstdio> #include<cstrin ...
随机推荐
- Linux简介——(一)
1. 常见操作系统 - 服务端操作系统 : linux.unix.windows server - 单机操作系统 : windows(dos .ucdos.win95.win98.win2000.xp ...
- [Leetcode Week14]Maximum Binary Tree
Maximum Binary Tree 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/maximum-binary-tree/description/ ...
- Microsoft Security Essential: 微软安全软件
Microsoft Security Essential: 微软安全软件 这个杀毒软件终身免费
- 在linux内核中获得比jiffies精度更高的时间值【转】
转自:http://blog.chinaunix.net/uid-20672257-id-2831219.html 内核一般通过jiffies值来获取当前时间.尽管该数值表示的是自上次系统启动到当前的 ...
- mongo数据库基本操作--python篇
连接数据库 MongoClient VS Connection class MongoClient(pymongo.common.BaseObject) | Connection to MongoDB ...
- redis之(一)redis的简单介绍
[一]:概念 --->Redis是一个开源的,高性能的,基于键值对的缓存与存储系统 --->Redis数据库中的多有数据都存储在内存中,由于内存的读写速度远快于硬盘,一秒读写超过10万键值 ...
- hdu 1671(字典树判断前缀)
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDR文件格式简介及其读写函数
转自:http://blog.csdn.net/lqhbupt/article/details/7828827 1.HDR简介HDR的全称是High-DynamicRange(高动态范围).在此,我们 ...
- Word截图PNG,并压缩图片大小
static void Main(string[] args) { var iso = new ImageSaveOptions(SaveFormat.Png); iso.PrettyFormat = ...
- AC日记——严酷的训练 洛谷 P2430
严酷的训练 思路: 背包: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 5005 int n,m,bi[m ...