[BZOJ][CQOI2014]数三角形
Description
给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个。下图为4x4的网格上的一个三角形。
注意三角形的三点不能共线。
Input
输入一行,包含两个空格分隔的正整数m和n。
Output
输出一个正整数,为所求三角形数量。
Sample Input
Sample Output
数据范围
1<=m,n<=1000
因为就在做这道题之前看的一道题……下意识以为只有直线和对角线的情况,然后搞成O(n)算法似乎自己比其他写题解的神犇厉害??事实上我错了,,,orz(雾(跪地(无奈(orz(%%%(逃
首先,显然的,不考虑三点共线共有C(n*m,3)种方法,所以问题转化为三点共线有几种情况?
在(x1,y1) (x2,y2)两点构成的线段(不含端点)上有gcd(x1-x2,y1-y2)-1个整点。
所以我们可以在logn的时间求出一条线段中的整点个数,但枚举两个点显然要超时,所以我们只枚举一个点的坐标,它与(0,0)显然构成了唯一的一条线段,然后我们发现这条线段其实可以移动,所以就将一条线段的解*(n-x)*(m-y)
相减就得到答案啦~~
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
ll n,m;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
int main(){
scanf("%lld%lld",&n,&m);
n++;m++;
ll ans=(n*m)*(n*m-)*(n*m-)/;
for(int i=;i<n;i++)
for(int j=;j<m;j++){
ll c;
if(!i)c=j-;
if(!j)c=i-;
if(i&&j)c=gcd(i,j)-;
if(c<)c=;
if(i&&j)ans-=c*(n-i)*(m-j)*;else ans-=c*(n-i)*(m-j);
}
printf("%lld",ans);
}
[BZOJ][CQOI2014]数三角形的更多相关文章
- 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]数三角形 组合数学
3505: [Cqoi2014]数三角形 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 478 Solved: 293[Submit][Status ...
- 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 ...
- 3505: [Cqoi2014]数三角形
3505: [Cqoi2014]数三角形 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1324 Solved: 807[Submit][Statu ...
- [CQOI2014]数三角形
[CQOI2014]数三角形 给定\(n\times m\)的网格,求三个点在其格点上的三角形个数,1<=m,n<=1000. 解 法一:直接 显然为组合计数问题,关键在于划分问题,注意到 ...
随机推荐
- [pandas] SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
转载自https://blog.csdn.net/blackyuanc/article/details/77892784 问题场景: 在读取CSV文件后,在新增一个特征列并根据已有特征修改 ...
- [数据处理] LabelEncoder编码
原创博文,转载请注明出处! # LabelEncoder介绍 LabelEncoder是对不连续的数字或文本编号. # LabelEncoder例子 # -*- coding: utf-8 ...
- Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?
- 我在ubuntu14.04安装使用的软件
搜狗拼音sougoupinyin:sudo add-apt-repository ppa:fcitx-team/nightly && sudo apt-get updatesudo a ...
- Ubuntu和win双系统删除ubuntu开机出错
Ubuntu和win双系统删除ubuntu开机出错问题. 报错error:unknown filesystem. grub rescue>_ 很简单: 进入pe 打开diskgenius 选择你 ...
- Codeforces 828C String Reconstruction【并查集巧妙运用】
LINK 题目大意 给你n个串和在原串中的出现位置,问原串 思路 直接跑肯定是GG 考虑怎么优化 因为保证有解,所以考虑过的点我们就不再考虑 用并查集维护当前每个点之后最早的没有被更新过的点 然后就做 ...
- Windows7 下python3和python2同时 安装python3和python2
1.下载python3和python2 进入python官网,链接https://www.python.org/ 选择Downloads--->Windows,点击进入就可以看到寻找想要的pyt ...
- ios 视图切换翻页效果
本文写的是视图切换,涉及到的内容有 1.实现代码添加Navigation Bar Toolbal: 2.实现在Navigation Bar和Toolbar上用代码添加Bar Button Item: ...
- 淘宝npm镜像使用
淘宝NPM镜像使用 镜像使用方法 1.config命令 npm config set registry https://registry.npm.taobao.org npm info undersc ...
- [Luogu4233]射命丸文的笔记
luogu description 对于\(x\in[1,n]\)求\(x\)点强联通竞赛图中的哈密顿回路的期望个数膜\(998244353\). \(n\le10^5\) sol 首先\(n\)点竞 ...