题目链接

Problem Description
Matt loves letter L.

A point set P is (a, b)-L if and only if there exists x, y satisfying:

P = {(x, y), (x + 1, y), . . . , (x + a, y), (x, y + 1), . . . , (x, y + b)}(a, b ≥ 1)

A point set Q is good if and only if Q is an (a, b)-L set and gcd(a, b) = 1.

Matt is given a point set S. Please help him find the number of ordered pairs of sets (A, B) such that:

 
Input
The first line contains only one integer T , which indicates the number of test cases.

For each test case, the first line contains an integer N (0 ≤ N ≤ 40000), indicating the size of the point set S.

Each of the following N lines contains two integers xi, yi, indicating the i-th point in S (1 ≤ xi, yi ≤ 200). It’s guaranteed that all (xi, yi) would be distinct.

 
Output
For each test case, output a single line “Case #x: y”, where x is the case number (starting from 1) and y is the number of pairs.
 
Sample Input
2
6
1 1
1 2
2 1
3 3
3 4
4 3
9
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
 
Sample Output
Case #1: 2
Case #2: 6
 
Hint

n the second sample, the ordered pairs of sets Matt can choose are:

A = {(1, 1), (1, 2), (1, 3), (2, 1)} and B = {(2, 2), (2, 3), (3, 2)}
A = {(2, 2), (2, 3), (3, 2)} and B = {(1, 1), (1, 2), (1, 3), (2, 1)}
A = {(1, 1), (1, 2), (2, 1), (3, 1)} and B = {(2, 2), (2, 3), (3, 2)}
A = {(2, 2), (2, 3), (3, 2)} and B = {(1, 1), (1, 2), (2, 1), (3, 1)}
A = {(1, 1), (1, 2), (2, 1)} and B = {(2, 2), (2, 3), (3, 2)}
A = {(2, 2), (2, 3), (3, 2)} and B = {(1, 1), (1, 2), (2, 1)}
Hence, the answer is 6.
 
题意:对于点集P 如果存在a,b使得P = {(x, y), (x + 1, y), . . . , (x + a, y), (x, y + 1), . . . , (x, y + b)}(a, b ≥ 1),并且a,b互质,则P is good 。可以发现对于符合要求(good)的集合P ,其构成一个L 型,且以(x,y)为拐点,从(x,y)向上长度和向右长度互质。现在给了N个点,求有多少对符合要求的L型集合不相交(集合交集为空)?
 
思路:先找到所有符合要求的L个数S,那么用S*S-相交的L对数  即为结果。
        怎么算相交的所有L对数呢? 容斥,很妙的思想,遍历每一个点,如果当前的点是输入的点之一,那么是一个拐点,令这个拐点向右延伸最长为k,那么算出所有其它L的竖着部分与(x,y+k)相交的对数,乘以2,另外要考虑以(x,y)为拐点的L与自身相交的情况,把这两种相交情形减掉后既是结果。
 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=;
const int M=;
int R[M][M],U[M][M];
bool mp[M][M];
int dp[M][M],cnt[M][M];
int t[M][M]; int gcd(int a,int b) { return (b==)?a:gcd(b,a%b); } void init()
{
for(int i=;i<M;i++)
for(int j=;j<M;j++)
{
dp[i][j]=dp[i][j-]+((gcd(i,j)==)?:);
cnt[i][j]=cnt[i-][j]+dp[i][j];
}
}
int main()
{
init();
int T,Case=;
cin>>T;
while(T--)
{
int n; scanf("%d",&n);
memset(mp,,sizeof(mp));
memset(U,,sizeof(U));
memset(R,,sizeof(R));
memset(t,,sizeof(t));
for(int i=;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
mp[x][y]=;
}
for(int i=;i>=;i--)
{
for(int j=;j>=;j--)
{
if(mp[i][j]){
if(mp[i+][j]) U[i][j]=U[i+][j]+;
if(mp[i][j+]) R[i][j]=R[i][j+]+;
}
}
}
LL s=;
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
if(mp[i][j]){
s+=cnt[U[i][j]][R[i][j]];
int d=;
for(int k=U[i][j];k>=;k--)
{
d+=dp[k][R[i][j]];
t[i+k][j]+=d;
}
}
}
}
LL ans=;
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
if(mp[i][j]){
LL p=t[i][j];
LL pp=cnt[U[i][j]][R[i][j]];
p-=pp;
for(int k=;k<=R[i][j];k++)
{
p+=t[i][j+k];
ans+=*p*dp[k][U[i][j]];
}
ans+=pp*pp;
}
}
}
s=s*s-ans;
printf("Case #%d: %lld\n",Case++,s);
}
return ;
}

hdu 5116--Everlasting L(计数DP)的更多相关文章

  1. 动态规划(DP计数):HDU 5116 Everlasting L

    Matt loves letter L.A point set P is (a, b)-L if and only if there exists x, y satisfying:P = {(x, y ...

  2. HDU 5116 Everlasting L

    题目链接:HDU-5116 题意:给定若干个整数点,若一个点集满足P = {(x, y), (x + 1, y), . . . , (x + a, y), (x, y + 1), . . . , (x ...

  3. HDU 4055 Number String (计数DP)

    题意:由数字1到n组成的所有排列中,问满足题目所给的n-1个字符的排列有多少个,如果第i字符是‘I’表示排列中的第i-1个数是小于第i个数的. 如果是‘D’,则反之. 析:dp[i][j] 表示前 i ...

  4. HDU 6377 度度熊看球赛 (计数DP)

    度度熊看球赛 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  5. 题解报告:hdu 1028 Ignatius and the Princess III(母函数or计数DP)

    Problem Description "Well, it seems the first problem is too easy. I will let you know how fool ...

  6. HDU 4632 CF 245H 区间DP(回文)

    先说HDU 4632这道题,因为比较简单,题意就是给你一个字符串,然后给你一个区间,叫你输出区间内所有的回文子序列,注意是回文子序列,不是回文字串. 用dp[i][j]表示区间[i,j]内的回文子序列 ...

  7. HDU 5693 D Game 区间dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5693 题解: 一种朴实的想法是枚举选择可以删除的两个或三个数(其他的大于三的数都能凑成2和3的和), ...

  8. hdu 2829 Lawrence(斜率优化DP)

    题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...

  9. hdu 3480 Division(斜率优化DP)

    题目链接:hdu 3480 Division 题意: 给你一个有n个数的集合S,现在让你选出m个子集合,使这m个子集合并起来为S,并且每个集合的(max-min)2 之和要最小. 题解: 运用贪心的思 ...

随机推荐

  1. Python中的线程详解

    线程 常用的方法 import threading import time def hello(name): print('Hello %s' % name) # 阻塞 time.sleep(5) p ...

  2. Tomcat常用设置及安全管理规范

    前言 随着公司内部使用Tomcat作为web应用服务器的规模越来越大,为保证Tomcat的配置安全,防止信息泄露,恶性攻击以及配置的安全规范,特制定此Tomcat安全配置规范.注意:  本文章从htt ...

  3. input text 只能输入数字

    添加 onkeyup="value=value.replace(/[^\d]/g,'')"

  4. 450. Delete Node in a BST 删除bst中的一个节点

    [抄题]: Given a root node reference of a BST and a key, delete the node with the given key in the BST. ...

  5. AndFix注意事项

    1.生成补丁,修改前后的apk包都必须签名. 2.AndFix 不支持修改布局文件. 3.文件的路径必须正确. 4.AndFix 不支持添加匿名内部类(就是点击事件). 5.AndFix 不支持添加新 ...

  6. c#关于Mysql MySqlBulkLoader 批量上传

    有个list表有几万数据 用insert插入,速度跟蜗牛爬行, 几十个表,传起来可就需要时间了. 搜搜,发现有  MySqlBulkLoader  这个人家mysql 的dll 里边已经提供了这个方法 ...

  7. GUI学习之一——PyQt5初识

    我们在第〇篇里先演示了GUI的功能,其实Python有多个库是支持GUI编程的,python官网列出了大量的说明,其中包括了原生的tkinter 还有许多第三方库 Pyqt PySide wxPyth ...

  8. tcp、ip、http

    tcp是传输层协议,ip是网络层协议,http是应用层协议,简单说就是tcp是传输数据,而http是封装数据. rpc与http的区别是项目大的话,接口间调用变多的话,采用rpc的话,不用像http那 ...

  9. PowerDesigner 缺省值 引号 问题

    在使用PowerDesigner做为MySQL数据库建模的时候,总是有这样的问题,例如我需要一个字段 createTime 类型是 Timestamp,要求这个字段默认值为系统当前时间,于是我给这个字 ...

  10. mysql空值排序

    SELECT * FROM lzh_topic_channel_product ORDER BY order_id is null , order_id  其中的ORDER BY order_id i ...