LightOJ 1366 - Pair of Touching Circles (统计矩形内外切圆对)

1366 - Pair of Touching Circles
Time Limit: 3 second(s) Memory Limit: 32 MB

You are given a rectangular grid of height H and width W. A problem setter wants to draw a pair of circles inside the rectangle so that they touch each other but do not share common area and both the circles are completely inside the rectangle. As the problem setter does not like precision problems, he also wants their centers to be on integer coordinates and their radii should be positive integers as well. How many different ways can he draw such pair of circles? Two drawings are different from each other if any of the circles has different center location or radius.

Input

Input starts with an integer T (≤ 500), denoting the number of test cases.

Each case starts with a line containing two integers H and W (0 < H, W ≤ 1000).

Output

For each case, print the case number and the number of ways of drawing such pairs of circles maintaining the mentioned constraints. Each output will fit into a 64-bit signed integer.

Sample Input

Output for Sample Input

5
4 2
4 3
4 4
4 6
10 10

Case 1: 1

Case 2: 2

Case 3: 6

Case 4: 16

Case 5: 496

Note

For case 3, the possible results are:


PROBLEM SETTER: MD. TOWHIDUL ISLAM TALUKDER
SPECIAL THANKS: MD. ARIFUZZAMAN ARIF, JANE ALAM JAN

链接:http://lightoj.com/volume_showproblem.php?problem=1366

枚举两个圆心的相对坐标,然后枚举其中的一个半径。

 /* ***********************************************
Author :kuangbin
Created Time :2013-10-18 17:27:32
File Name :E:\2013ACM\专题强化训练\计算几何\LightOJ1366.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
int w,h;
int iCase = ;
scanf("%d",&T);
while(T--)
{
iCase++;
scanf("%d%d",&w,&h);
long long ans = ;
//枚举两个圆心的相对坐标
for(int i = ;i <= w/;i++)
for(int j = ;j <= h/;j++)
{
if(i == && j == )continue;
int tmp = sqrt(i*i + j*j);
if(tmp*tmp != i*i + j*j)continue;
//枚举其中一个圆的半径
for(int k = ;k < tmp;k++)
{
int y1 = min(-k,j-(tmp-k)), y2 = max(k,j+(tmp-k));
int x1 = min(-k,i-(tmp-k)), x2 = max(k,i+(tmp-k));
int x = x2-x1;
int y = y2-y1;
if(x > w || y > h)continue;
long long tt = (w - x + )*(h - y + );
if(i*j)tt*= ;
ans += tt;
}
}
printf("Case %d: ",iCase);
cout<<ans<<endl;
}
return ;
}
上一篇:hbase 1.2.1 分布式安装


下一篇:17082 两个有序数序列中找第k小