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

  1. 2
  2. 6
  3. 1 1
  4. 1 2
  5. 2 1
  6. 3 3
  7. 3 4
  8. 4 3
  9. 9
  10. 1 1
  11. 1 2
  12. 1 3
  13. 2 1
  14. 2 2
  15. 2 3
  16. 3 1
  17. 3 2
  18. 3 3

Sample Output

  1. Case #1: 2
  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的个数
  看代码就能懂了……
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. using namespace std;
  5. const int N=;
  6. typedef long long LL;
  7. LL map[N][N],st[N],S,M;
  8. LL f[N][N],dp[N][N],sum[N][N];
  9. /*f[i][j]:[1,j]中与i互质的数的个数*/
  10. LL dwn[N][N],rht[N][N];int T,cas,k,x,y;
  11. LL Gcd(LL a,LL b){return b?Gcd(b,a%b):a;}
  12. void Prepare(){
  13. for(int i=;i<=;i++)
  14. for(int j=;j<=;j++){
  15. f[i][j]=f[i][j-]+(Gcd(i,j)==);
  16. dp[i][j]=dp[i-][j]+f[i][j];
  17. }
  18. }
  19. void Init(){S=M=;
  20. memset(map,,sizeof(map));
  21. memset(sum,,sizeof(sum));
  22. memset(dwn,,sizeof(dwn));
  23. memset(rht,,sizeof(rht));
  24. }
  25. int main(){
  26. Prepare();
  27. scanf("%d",&T);
  28. while(T--){
  29. Init();scanf("%d",&k);
  30. while(k--){scanf("%d%d",&x,&y);map[x][y]=;}
  31. for(int i=;i>=;i--)
  32. for(int j=;j>=;j--){
  33. if(!map[i][j])continue;
  34. if(map[i+][j])dwn[i][j]=dwn[i+][j]+;
  35. if(map[i][j+])rht[i][j]=rht[i][j+]+;
  36. }
  37.  
  38. for(int i=;i<=;i++)
  39. for(int j=;j<=;j++){
  40. if(!map[i][j])continue;
  41. memset(st,,sizeof(st));
  42. for(int k=;k<=dwn[i][j];k++)
  43. st[k]=f[k][rht[i][j]];
  44. for(int k=dwn[i][j];k>=;k--)
  45. st[k-]+=st[k];
  46. for(int k=;k<=dwn[i][j];k++)
  47. sum[i+k][j]+=st[k];
  48. S+=st[];
  49. }
  50.  
  51. for(int i=;i<=;i++)
  52. for(int j=;j<=;j++){
  53. if(!dwn[i][j])continue;
  54. if(!rht[i][j])continue;
  55. LL tot=dp[dwn[i][j]][rht[i][j]];
  56. LL calc=sum[i][j]-tot;
  57. for(int k=;k<=rht[i][j];k++){
  58. calc+=sum[i][j+k];
  59. M+=*calc*f[k][dwn[i][j]];
  60. }
  61. M+=tot*tot;
  62. }
  63. printf("Case #%d: %lld\n",++cas,S*S-M);
  64. }
  65. return ;
  66. }

动态规划(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. jQuery 源码分析3: jQuery.fn/ jQuery.prototype

    // 建立方法实例,提高方法访问的速度(避免在原型链上搜索) var deletedIds = []; var slice = deletedIds.slice; var concat = delet ...

  2. greenplum学习

    公司TM蛋疼,动不动让你学习新东西,就是不让你闲下来,本着胳膊拧不过大腿定律,忍了,这是背景. 好吧哥端起一本厚厚的<GreenPlum企业应用实战>,打开百度开始GP的学习之路: GP只 ...

  3. 利用iptables来配置linux禁止所有端口登陆和开放指定端口

    from:http://www.myhack58.com/Article/sort099/sort0102/2011/31781.htm 1.关闭所有的 INPUT FORWARD OUTPUT 只对 ...

  4. Fedora 21 安装QQ国际版

    首先安装依赖包 sudo yum install freetype.i686 libpng.i686 libgcc.i686 libXau.i686 点击下载wine-2012qq国际版 unzip ...

  5. 使用Fiddler提高前端工作效率 (介绍篇)

    1. Fiddler 是什么? Fiddler是用C#编写的一个免费的HTTP/HTTPS网络调试器.英语中Fiddler是小提琴的意思,Fiddler Web Debugger就像小提琴一样,可以让 ...

  6. Delphi实用小function

    Write Log // strLog : the log content need write to log file; // strFile: the compliete file path(in ...

  7. python 数据字典应用

    一.什么是字典? 字典是Python语言中唯一的映射类型. 映射类型对象里哈希值(键,key)和指向的对象(值,value)是一对多的的关系,通常被认为是可变的哈希表. 字典对象是可变的,它是一个容器 ...

  8. POJ 3122 Pie 二分枚举

    题目:http://poj.org/problem?id=3122 这个题就好多了,没有恶心的精度问题,所以1A了.. #include <stdio.h> #include <ma ...

  9. 用JS实现避免重复加载相同js文件

    我们在日常开发过程中,可能有重复加载同一个资源例如:1.js,为了提高性能和用户体验这里我们用原生JS实现同一个资源只加载一次. 下面是 common.js里的JS代码 //使用沙箱模式防止污染外面的 ...

  10. silverlight5开发的翻牌游戏

    扑克牌用了一个自定义控件,代码如下 public class CardButton : Button { public int state;//是否翻转0是未翻转,1是已翻转 private stat ...