UVALive 3295
题目大意:见刘汝佳《算法竞赛入门经典——训练指南》P173
解题思路:
每一个合法的三角形的三个顶点都不在同一直线上,那么问题其实就是在求所有不全在同一直线上的三点的组合数。
我们可以利用容斥原理,先求出所有的三个顶点的组合数C[(n+1)*(m+1)][3]。全在同一直线上的三个网格顶点有三种:三点在同一水平线上的,三点在同一竖直线上的,三点在同一斜线上的。前两种不难求,因此不再赘述,这里重点讲解第三种。
设三点坐标为(x1,y1),(x2,y2),(x3,y3),且x1 < x2 < x3,y1 < y2 < y3,设 x2-x1 = i,x3-x2 = j,当且仅当 i*(y3-y2) = j*(y2-y1)时,三点在同一斜线上。那么我们可以枚举 i 和 j ,求出 g = gcd(i,j),对于每一个(i,j),再从 1 开始枚举 k ,则此时 y3-y2 = k*j/g,y2-y1 = k*i/g(当 y3 - y1 >= m+1时退出循环),这样就可以知道 x3-x1 和 y3-y1 的值了,接下来就只需要求出放得下多少条这样的斜线就可以。
AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm> using namespace std;
typedef long long ll;
const int maxn=+;
ll C[maxn][];
void init(){
C[][]=;
C[][]=C[][]=;
for(int n=;n<maxn;n++){
C[n][]=;
if(n<=) C[n][n]=;
for(int m=;m<min(,n);m++){
C[n][m]=C[n-][m-]+C[n-][m];
}
}
}
int gcd(int a,int b){
if(b==) return a;
return gcd(b,a%b);
}
int main(){
init();
int n,m;
int kase=;
while(scanf("%d%d",&n,&m)==&&n&&m){
n++,m++;
if(n>m) swap(n,m);
ll ans=C[n*m][];
ans-=C[n][]*m;
ans-=C[m][]*n;
ll t=;
for(int i=;i<n;i++){
for(int j=;i+j<n;j++){
int gd=gcd(i,j);
for(int k=;;k++){
int id=k*j/gd,ij=k*i/gd;
if(id+ij>=m) break;
t+=(n-(i+j))*(m-(k*(i+j)/gd));
}
}
}
ans-=t*;
printf("Case %d: %lld\n",kase++,ans);
}
return ;
}
UVALive 3295的更多相关文章
- UVALive 3295 Counting Triangles
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- BZOJ 3295: [Cqoi2011]动态逆序对
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3865 Solved: 1298[Submit][Sta ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive 6508 Permutation Graphs
Permutation Graphs Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- UVALive 6500 Boxes
Boxes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Pract ...
随机推荐
- Python语言类型
Python是一门动态解释型的强类型语言. 对这句话进行解析,语言分为动态的和静态的,编译型和解释型的,强类型的和弱类型的语言之分. 下面对三种不同维度的类型的语言进行解释: 1.编译型和解释型 差别 ...
- values/colors.xml
<color name="abcd">#FFC4C4C4</color> <color name="white">#FFFF ...
- Qt之QListWidget:项目的多选与单选设置
2019独角兽企业重金招聘Python工程师标准>>> #include "widget.h" #include <QApplication> #in ...
- MySQL Change Data Directory
为什么80%的码农都做不了架构师?>>> Stop MySQL using the following command: sudo /etc/init.d/mysql stop ...
- 学数据库你竟然不用用JAVA写代码,可惜你遇到了我! JAVA连接数据库(JDBC)的安装使用教程
Step 1 你得有Eclipse 没有出门右拐,我教不了你. Step 2 你得有Mysql MySQL的详细安装过程,我在另一篇博客中给出.戳我 Step 3 安装JDBC 可以去官网下,如果用的 ...
- python selenium(键盘事件 Keys 类)
1.导入Keys类 from selenium.webdriver.common.keys import Keys Keys.BACK_SPACE 删除输入框内结尾的单个字符 Keys.SPACE ...
- VSCode 安装 React 项目
1 下载nodejs 安装 (此时npm 和 node环境都已经装好) 2 安装淘宝镜像 npm install -g cnpm --registry=https://registry.npm.tao ...
- 25-Java-Spring框架(三)
Spring框架的了解.SpringIOC的部分内容请阅读23-Java-Spring框架(一) SpringwebMVC的了解.请求流程.运用等请阅读24-Java-Spring框架(二) 四.Sp ...
- .NET Core+QQ第三方授权登录
安装包 dotnet add package AspNet.Security.OAuth.QQ 接上文GitHub第三方授权登录 申请过程不介绍了,申请者资料,个人也是可以申请成功的. 这时候有二个参 ...
- Code::Blocks20.03 编译报错
Code::Blocks最近出了新版20.03,进入官网选择下载了附带MinGW版的安装包后,编译HelloWorld就报错(CB一直以来都有问题,新版还是这样...) 主要有两个问题: ld.exe ...