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. Audio / Video Playback

    For Developers‎ > ‎Design Documents‎ > ‎ Audio / Video Playback Interested in helping out?  Ch ...

  2. 去除input的前后的空格

    这里用的是jquery的方法

  3. Intel投入5亿美元提升14nm工艺CPU需求

    虽然14nm行将收尾,但是却有大量的客户在赶“末班车”,导致CPU供货告急. Intel年初宣布增加10亿美元的额外资本支出用于转向更新的.更先进的生产工具,以便增加产能,在本周的第39届纳斯达克投资 ...

  4. HNU 12933 Random Walks Catalan数 阶乘求逆元新技能

    一个Catalan数的题,打表对每个数都求一次逆元会T,于是问到了一种求阶乘逆元的打表新方法. 比如打一个1~n的阶乘的逆元的表,假如叫inv[n],可以先用费马小定理什么的求出inv[n],再用递推 ...

  5. .ashx 实现自动路由和参数填充

    在Mvc中访问控制器,参数填充和路由控制都非常方便,但之前项目用的是webFrom,和js交互的ashx页面,路由非常麻烦要根据传进来关键字来做switch,参数填充更坑,要一个一个去form中取出来 ...

  6. 题解 P2431 【正妹吃月饼】

    假如做这道题想着用如下朴实的模拟,那肯定要WA至少4个点. #include <iostream> #include <cstdio> using namespace std; ...

  7. 【Henu ACM Round#15 F】Arthur and Questions

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] a1+a2+...+ak<a2+a3+...ak+1 ->a1<ak+1 a2+a3+...+ak+1<a3 ...

  8. Spring Cloud学习笔记【四】断路器Hystrix

    雪崩效应 在微服务架构中通常会有多个服务层调用,基础服务的故障可能会导致级联故障,进而造成整个系统不可用的情况,这种现象被称为服务雪崩效应.服务雪崩效应是一种因 “服务提供者” 的不可用导致 “服务消 ...

  9. Android Volley 具体解释 Google公布的一套用于网络通信的工具库

    下载地址:git clone https://android.googlesource.com/platform/frameworks/volley 或 : https://github.com/mc ...

  10. oracle之ROWNUM的查询应用

    1 在ORACLE数据库中,ROWNUM是ORACLE数据库为查询结果加入的一个伪列.起始值为1.经常使用来处理查询结果的分页. 2 因为ROWNUM的特殊性,使用时候一般是分三层: 第一层:先进行查 ...