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.
  这道题DP计数,十分有意义。
  dp[i][j]:表示a∈[1,i],b∈[1,j],sigma(a,b)==1
  sum[i][j]:表示竖直部分穿过点(i,j)的L的个数
  看代码就能懂了……
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=;
typedef long long LL;
LL map[N][N],st[N],S,M;
LL f[N][N],dp[N][N],sum[N][N];
/*f[i][j]:[1,j]中与i互质的数的个数*/
LL dwn[N][N],rht[N][N];int T,cas,k,x,y;
LL Gcd(LL a,LL b){return b?Gcd(b,a%b):a;}
void Prepare(){
for(int i=;i<=;i++)
for(int j=;j<=;j++){
f[i][j]=f[i][j-]+(Gcd(i,j)==);
dp[i][j]=dp[i-][j]+f[i][j];
}
}
void Init(){S=M=;
memset(map,,sizeof(map));
memset(sum,,sizeof(sum));
memset(dwn,,sizeof(dwn));
memset(rht,,sizeof(rht));
}
int main(){
Prepare();
scanf("%d",&T);
while(T--){
Init();scanf("%d",&k);
while(k--){scanf("%d%d",&x,&y);map[x][y]=;}
for(int i=;i>=;i--)
for(int j=;j>=;j--){
if(!map[i][j])continue;
if(map[i+][j])dwn[i][j]=dwn[i+][j]+;
if(map[i][j+])rht[i][j]=rht[i][j+]+;
} for(int i=;i<=;i++)
for(int j=;j<=;j++){
if(!map[i][j])continue;
memset(st,,sizeof(st));
for(int k=;k<=dwn[i][j];k++)
st[k]=f[k][rht[i][j]];
for(int k=dwn[i][j];k>=;k--)
st[k-]+=st[k];
for(int k=;k<=dwn[i][j];k++)
sum[i+k][j]+=st[k];
S+=st[];
} for(int i=;i<=;i++)
for(int j=;j<=;j++){
if(!dwn[i][j])continue;
if(!rht[i][j])continue;
LL tot=dp[dwn[i][j]][rht[i][j]];
LL calc=sum[i][j]-tot;
for(int k=;k<=rht[i][j];k++){
calc+=sum[i][j+k];
M+=*calc*f[k][dwn[i][j]];
}
M+=tot*tot;
}
printf("Case #%d: %lld\n",++cas,S*S-M);
}
return ;
}

动态规划(DP计数):HDU 5116 Everlasting L的更多相关文章

  1. HDU 5116 Everlasting L

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

  2. HDU 4055 The King’s Ups and Downs(DP计数)

    题意: 国王的士兵有n个,每个人的身高都不同,国王要将他们排列,必须一高一矮间隔进行,即其中的一个人必须同时高于(或低于)左边和右边.问可能的排列数.例子有1千个,但是最多只算到20个士兵,并且20个 ...

  3. HDU 4055 Number String(DP计数)

    题意: 给你一个含n个字符的字符串,字符为'D'时表示小于号,字符为“I”时表示大于号,字符为“?”时表示大小于都可以.比如排列 {3, 1, 2, 7, 4, 6, 5} 表示为字符串 DIIDID ...

  4. 算法-动态规划DP小记

    算法-动态规划DP小记 动态规划算法是一种比较灵活的算法,针对具体的问题要具体分析,其宗旨就是要找出要解决问题的状态,然后逆向转化为求解子问题,最终回到已知的初始态,然后再顺序累计各个子问题的解从而得 ...

  5. Tetrahedron(Codeforces Round #113 (Div. 2) + 打表找规律 + dp计数)

    题目链接: https://codeforces.com/contest/166/problem/E 题目: 题意: 给你一个三菱锥,初始时你在D点,然后你每次可以往相邻的顶点移动,问你第n步回到D点 ...

  6. Vijos p1770 大内密探 树形DP+计数

    4天终于做出来了,没错我就是这么蒟蒻.教训还是很多的. 建议大家以后编树形DP不要用记忆化搜索,回溯转移状态个人感觉更有条理性. 大神题解传送门 by iwtwiioi 我的题解大家可以看注释&quo ...

  7. hdu 4630 查询[L,R]区间内任意两个数的最大公约数

    No Pain No Game Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. 动态规划dp

    一.概念:动态规划dp:是一种分阶段求解决策问题的数学思想. 总结起来就一句话:大事化小,小事化了 二.例子 1.走台阶问题 F(10):10级台阶的走法数量 所以:F(10)=F(9)+F(8) F ...

  9. 【POJ1952】逢低吸纳 dp+计数

    题目大意:给定一个有 N 个数的序列,求其最长下降子序列的长度,并求出有多少种不同的最长下降子序列.(子序列各项数值相同视为同一种) update at 2019.4.3 题解:求最长下降子序列本身并 ...

随机推荐

  1. java新手笔记11 类的静态属性、方法(单例)

    1.Person类 package com.yfs.javase; public class Person { String name;//每个对象上分配 与对象绑定 int age; char se ...

  2. bzoj1008: [HNOI2008]越狱

    思路:首先所有情况就是m^n,然后不可能发生越狱的情况就是第一个有m种选择,第二个要与第一个不同就是m-1种选择,第三个要与第二个不同也是m-1种选择,然后不可能发生越狱的情况数就是m*(m-1)^( ...

  3. OpenJudge 2787 算24

    1.链接地址: http://poj.org/problem?id=1631 http://bailian.openjudge.cn/practice/2787/ 2.题目: 总时间限制: 3000m ...

  4. session绑定线程

  5. windows平台 culture name 详细列表

    点击打开链接http://msdn.microsoft.com/zh-cn/goglobal/bb896001.aspx LCID Culture Identifier Culture Name Lo ...

  6. 《sort命令的k选项大讨论》-linux命令五分钟系列之二十七

    本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...

  7. PHP5.5安装php-redis扩展

    windows下开发用的xampp集成的环境,想装个php-redis扩展,扩展的github地址: https://github.com/nicolasff/phpredis 描述里找到window ...

  8. PHP下拉框选择的实现方法

    实现 第一种PHP下拉框实现方法: < ?php //提交下拉框; //直接饱触发onchange事件的结果 $id=$_GET['myselect']; // myselect 为locati ...

  9. NET笔记——IOC详解和Unity基础使用介绍

    说起IOC,可能很多初学者不知道是用来做什么的,今天正好有点时间,就来扫扫盲,顺便巩固下自己. IOC全称是Inversion Of Control,意为控制反转(这些自然百度也有),可什么是控制反转 ...

  10. UIImage拉伸显示

    下面张图片,是设计来做按钮背景的:  button.png,尺寸为:24x60 现在我们把它用作为按钮背景,按钮尺寸是150x50,以下是没有经过技术性拉伸处理的情况: // 得到view的尺寸   ...