题目大意:见刘汝佳《算法竞赛入门经典——训练指南》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的更多相关文章

  1. UVALive 3295 Counting Triangles

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  2. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  3. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  4. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  5. BZOJ 3295: [Cqoi2011]动态逆序对

    3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3865  Solved: 1298[Submit][Sta ...

  6. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

  7. UVALive 6145 Version Controlled IDE(可持久化treap、rope)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  8. UVALive 6508 Permutation Graphs

    Permutation Graphs Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  9. UVALive 6500 Boxes

    Boxes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Pract ...

随机推荐

  1. http请求返回的数字代表的含义

    一些常见的状态码为: 200 - 服务器成功返回网页 404 - 请求的网页不存在 503 - 服务器超时 下面提供 HTTP 状态码的完整列表.点击链接可了解详情.您也可以访问 HTTP 状态码上的 ...

  2. 【Linux常见命令】mv命令

    mv - move (rename) files mv命令用来为文件或目录改名.或将文件或目录移入其它位置. 语法: mv [OPTION]... [-T] SOURCE DEST mv [OPTIO ...

  3. Linux下实现文件共享配置[samba]

    Linux下实现文件共享配置[samba] 第一步:安装samba软件 1.命令:rpm –q samba #查询是否已安装sambayum install samba #使用yum源安装samba, ...

  4. Codeforce 1102 C. Doors Breaking and Repairing

    Descirbe You are policeman and you are playing a game with Slavik. The game is turn-based and each t ...

  5. ssrf爆破mysql

    php ssrf 代码<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $_GET['url']); #curl_setopt($ch ...

  6. N - Subpalindromes URAL - 1989 哈希+线段树

    N - Subpalindromes URAL - 1989 这个是一个哈希+线段树,这个题目也不算特别难,但是呢,还比较有意思. 这个题目给你两个操作,一个是回答l~r 区间是不是回文,一个是对一个 ...

  7. uniapp自定义简单省市区联动组件

    又双叒一个uniapp组件 最近有一个选择地址的需求,就写了一个省市区联动选择器. 选择日期使用的picker,就照着它简单的整了一个,使用网络请求城市数据,还用到了vuex组件数据共享. 本来自己整 ...

  8. Idea中查看一个类的所有资料及其层级关系

    在Idea中直接Ctrl + t 查看类的子类是可以看到,但是他没有那种层级顺序! 我们可以在类中点击顶部菜单Navigate -----> Type Hierarchy

  9. 【Flink】使用之前,先简单了解一下Flink吧!

    目录 Flink简单介绍 概述 无边界数据流和有边界数据流 技术栈核心组成 架构体系 重要角色 Flink与Spark架构概念转换 Flink简单介绍 概述    在使用Flink之前,我们需要大概知 ...

  10. 【Scala】Actor并发编程实现单机版wordCount

    文章目录 对单个文本文件进行单词计数 对多个文本文件进行单词计数 对单个文本文件进行单词计数 import scala.actors.Actor import scala.io.Source //读取 ...