BZOJ 3505: [Cqoi2014]数三角形 数学
2015-09-01 16:39
447 查看
3505: [Cqoi2014]数三角形
Time Limit: 1 SecMemory Limit: 256 MB
题目连接
http://www.lydsy.com/JudgeOnline/problem.php?id=3505Description
给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个。下图为4x4的网格上的一个三角形。注意三角形的三点不能共线。
Input
输入一行,包含两个空格分隔的正整数m和n。Output
输出一个正整数,为所求三角形数量。Sample Input
2 2Sample Output
76HINT
1<=m,n<=1000题意
题解:
任意选择三个不在同一条直线上的三个点即是满足题意的点
考虑补集,随意选择3个点,然后删除同一直线,同一斜线的就好了
直线上的比较简单,只用想斜线的就好了
枚举矩形,矩形的长宽分别为i,j,那么这个矩形的对角线就会经过gcd(i,j)-1个点
然后这个图里面可以放(n-i+1)*(m-j+1)个矩形
然后搞一搞就好了
代码:
//qscqesze #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <bitset> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define maxn 510000 #define mod 10007 #define eps 1e-9 int Num; //const int inf=0x7fffffff; //§ß§é§à§é¨f§³ const int inf=0x3f3f3f3f; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //************************************************************************************** ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); } int main() { ll n=read(),m=read(); n++,m++; ll ans = (n*m)*(n*m-1)*(n*m-2LL)/6LL; ans -= n*(m*(m-1LL)*(m-2LL)/6LL); ans -= m*(n*(n-1LL)*(n-2LL)/6LL); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { ll tmp = gcd(i,j)+1LL; if(tmp>2LL) ans-=(tmp-2LL)*2LL*(n-i*1LL)*(m-j*1LL); } } printf("%lld\n",ans); }
相关文章推荐
- ajax跨域请求
- Android项目开发流程
- Nouveau源码分析(四):NVIDIA设备初始化之nouveau_drm_load (1)
- 避免python Popen阻塞
- 线程通信方法
- ActiveMQ的插件开发介绍
- try、finally问题讨论
- python 切片(slice)
- Nouveau源码分析(二):Nouveau结构体的基本框架
- MySQL的biglog文件操作
- 订机票
- MyBatis学习笔记(七)Clob与Blob数据类型与多参数传入
- handlebars-----由于if的条件不足引出的helper
- JS设计模式——命令模式
- Linux匿名管道操作
- Vbs脚本执行不同版本的Office激活任务(MAK&KMS激活)
- oracle imp,exp使用详解
- deque双向队列
- Linux shell 交互式编程、TCL/TK 和 Expect 编译与安装、expect 编程
- linux 安装gtest 单元测试