https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1296

动态规划

我们先计算任意三个点组成的可能,然后排除同一水平,同一垂直的,同一斜线的,前两个比较好计算,同一斜线的稍复杂.要用容斥原理,首先我们动手计算一下,可能发现每次多的是gcd(i, j)-1,然后再去重,dp[i][j]代表从左上角[0,0] 到这个点[i,j]并以这两个点为端点枚举三点共线的个数,最后还要递推一次,得到n*m的网格三点共线的个数,当然这也要*2.

参考http://blog.csdn.net/u011345136/article/details/38736595

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define ios() ios::sync_with_stdio(false)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll dp[][],n,m;
ll C(ll n,ll m)
{
if(n<m) return ;
ll ans=;
for(int i=;i<m;i++)
{
ans=ans*(n-i)/(i+);
}
return ans;
}
ll gcd(ll x,ll y)
{
return y==?x:gcd(y,x%y);
}
void init()
{
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
dp[i][j]=gcd(i,j)-;
}
}
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
dp[i][j]+=dp[i-][j]+dp[i][j-]-dp[i-][j-];
}
}
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
dp[i][j]+=dp[i-][j]+dp[i][j-]-dp[i-][j-];
}
}
printf("%d\n",dp[][]);
}
int main()
{
init();
int t=;
while(scanf("%lld%lld",&n,&m)&&(n && m))
{
n++;m++;
printf("Case %d: %lld\n",++t,C(n*m,)-m*C(n,)-n*C(m,)-dp[n-][m-]*);
}
return ;
}

UVA 12075 Counting Triangles的更多相关文章

  1. UVA 12075 - Counting Triangles(容斥原理计数)

    题目链接:12075 - Counting Triangles 题意:求n * m矩形内,最多能组成几个三角形 这题和UVA 1393类似,把总情况扣去三点共线情况,那么问题转化为求三点共线的情况,对 ...

  2. UVA 1393 Highways,UVA 12075 Counting Triangles —— (组合数,dp)

    先看第一题,有n*m个点,求在这些点中,有多少条直线,经过了至少两点,且不是水平的也不是竖直的. 分析:由于对称性,我们只要求一个方向的线即可.该题分成两个过程,第一个过程是求出n*m的矩形中,dp[ ...

  3. hdu 1396 Counting Triangles(递推)

    Counting Triangles Problem Description Given an equilateral triangle with n thelength of its side, p ...

  4. Counting Triangles(hd1396)

    Counting Triangles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. uva 1436 - Counting heaps(算)

    题目链接:uva 1436 - Counting heaps 题目大意:给出一个树的形状,如今为这棵树标号,保证根节点的标号值比子节点的标号值大,问有多少种标号树. 解题思路:和村名排队的思路是一仅仅 ...

  6. 1307 - Counting Triangles

    1307 - Counting Triangles    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...

  7. UVA 10198 Counting

    Counting The Problem Gustavo knows how to count, but he is now learning how write numbers. As he is ...

  8. UVA - 10574 Counting Rectangles

    Description Problem H Counting Rectangles Input: Standard Input Output:Standard Output Time Limit: 3 ...

  9. UVA 10574 - Counting Rectangles(枚举+计数)

    10574 - Counting Rectangles 题目链接 题意:给定一些点,求可以成几个边平行于坐标轴的矩形 思路:先把点按x排序,再按y排序.然后用O(n^2)的方法找出每条垂直x轴的边,保 ...

随机推荐

  1. Navicat for Oracle

    1.先解压Navicat for Oracle到任意目录 2.将instantclient-basic-nt-12.1.0.2.0解压到1中目录的instantclient_10_2文件夹下(推荐,可 ...

  2. UI Framework-1: Aura Event Handling

    Event Handling A diagram of the architecture of this system:     HWNDMessageHandler owns the WNDPROC ...

  3. TP5 上传文件

    直接贴上一个完整的代码 /** * 图片上传方法 * @return [type] [description] */ /** * 1 获取到文件 * 2 验证文件的形状是不是符合上传的规则 * 3 i ...

  4. parted---磁盘分区

    parted命令是由GNU组织开发的一款功能强大的磁盘分区和分区大小调整工具,与fdisk不同,它支持调整分区的大小.作为一种设计用于Linux的工具,它没有构建成处理与fdisk关联的多种分区类型, ...

  5. Unity 调用 Android Native 方法(一) 获得Android系统音量

    学习雷锋,好榜样,接下来的这一系类教程里,将通过unity来实现Android端的一些常用功能, 不需要在 Asset/Plugins/Android 目录下引用jar包或者aar包,这是重点. us ...

  6. HTML学习----------DAY1 第三节

    本章通过实例向您演示最常用的 HTML 标签. 提示:不要担心本章中您还没有学过的例子,您将在下面的章节中学到它们. 提示:学习 HTML 最好的方式就是边学边做实验.我们为您准备了很好的 HTML ...

  7. No enclosing instance of type E is accessible.

    No enclosing instance of type E  is accessible. 静态方法(main)中调用内部类,会出现这样的问题: 学习了:https://www.cnblogs.c ...

  8. Android——4.2.2 文件系统文件夹分析

    近期公司要整android内部培训,分配给我写个培训文档.这里记录例如以下: 撰写不易,转载请注明出处:http://blog.csdn.net/jscese/article/details/4089 ...

  9. 怎样给UINavigationBar加入button?

    Mads Mobæk:给UINavigationBar加入button的演示样例代码 1 2 3 4 5 6 7 8 UIBarButtonItem *rightButton = [[UIBarBut ...

  10. 基于express+redis高速实现实时在线用户数统计

    作者:zhanhailiang 日期:2014-11-09 本文将介绍怎样基于express+redis高速实现实时在线用户数统计. 1. 在github.com上创建项目uv-tj.将其同步到本地: ...