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. js对浏览器产生的影响

     Js 是单线程执行引擎.在我们动态修改一些属性时会产生两种效果:    1.Repaint ----- 一部分重画,修改 div 的颜色呀,但是尺寸没有改变.    2.Reflow ---- 元素 ...

  2. 推荐《SQL基础教程(第2版)》中文PDF+源代码+习题答案

    我认为<SQL基础教程(第2版)>非常适合数据库学习的初学者.论述的角度是读者的角度,会换位思考到读者在看到这一段时候会发出怎样的疑问,非常难得:原始数据的例题只有一道,但是可以反复从不同 ...

  3. etTimeout来实现setInterval

    etTimeout来实现setInterval <script type="text/javascript"> function interval(func, w, t ...

  4. SPOJ 694 Distinct Substrings

    Distinct Substrings Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on SPOJ. O ...

  5. [Python] Plotting multiple stocks

    import os import pandas as pd import matplotlib.pyplot as plt def test_run(): start_date='2017-01-01 ...

  6. 大话设计模式C++实现-第15章-抽象工厂模式

    一.UML图 二.概念 抽象方法模式(Abstract Factory):提供一个创建一系列相关或互相依赖对象的接口,而无需指定他们详细的类. 三.包括的角色 (1)抽象工厂 (2)详细工厂:包含详细 ...

  7. 4.angularJS-指令(directive)

    转自:https://www.cnblogs.com/best/p/6225621.html 指令(directive)是AngularJS模板标记和用于支持的JavaScript代码的组合.Angu ...

  8. JWT使用1

    JSON Web Token - 在Web应用间安全地传递信息 转载收藏于:http://blog.leapoahead.com/2015/09/06/understanding-jwt/ JSON ...

  9. js实现日期转换方法

    //方法1function timeStamp1String(time) { var datetime = new Date(); datetime.setTime(time); var year = ...

  10. 创建一个基于ViSP的图片显示程序

    创建一个图片读取,并在windows下窗口显示的程序. #include <visp/vpDisplayD3D.h> #include <visp/vpDisplayGDI.h> ...