您的位置:首页 > 大数据 > 人工智能

uva12373 - Pair of Touching Circles

2015-08-07 19:05 253 查看
Given a rectangular grid of height H and width W. A problem setter wants to draw a pair of circlesinside the rectangle so that they touch each other but do not share common area and both the circlesare completely inside the rectangle. As the problem setter
does not like precision he also wants theircenters on integer coordinates and their radii to be positive integers as well. How many different wayscan he draw such pair of circles? Two drawings are different from each other if any of the circles hasdifferent
center location or radius.InputThe first line of input contains the number of test cases T (T ≤ 500). Each of the next T lines willcontain two integers H and W (0 < H, W ≤ 1000).OutputFor each line of input output the case number and the number of ways of
drawing such pairs of circlesmaintaining the mentioned constraints. See sample output for exact formatting. The output will fitinto 64-bit signed integer.Illustration of case 3:Sample Input54 24 34 44 610 10Sample OutputCase 1: 1Case 2: 2Case 3: 6Case 4: 16Case
5: 496

N*M的矩阵,两个相邻的圆,圆心和半径都要是整数,求这两个圆在矩阵中有多少种放置情况,位置和半径不同都算。

这题坑了我一下午=。= 我竟然拿DP去做,因为一开始想错了,以为这两个圆的关系只能是横着或竖着,然而答案并不对。。怎么看我DP写的也没问题,后来才发现还可能有斜着的情况,比如两个圆心的连线是勾股三角形斜边的时候就可以。。然后我又去特判这种情况,反正最后过了样例交上去WA。。最后一想,用什么DP啊,直接枚举两个圆心的坐标差,如果圆心距离是整数的话,再枚举半径长度,算出包含这两个圆的最小矩阵是多大,再看整个矩阵中能包含多少个这样的小矩阵,特别要注意的是如果两个圆大小相同那么对称之后还是相同,这种要特判一下。交上去TLE。。发现间距只用枚举到N,M的一半,改了后终于AC。。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;

const LL MAXN=1010;
const LL INF=0x3f3f3f3f;
const LL SIGMA_SIZE=28;

LL T,N,M;

LL solve(LL n,LL m){
LL ret=0;
for(LL i=0;i<=N/2;i++)
for(LL j=0;j<=M/2;j++) if(i+j>0){
LL t=i*i+j*j;
LL sq=(LL)(sqrt(t)+0.5);
if(sq*sq==t){
for(LL r=1;r<=sq/2;r++){
LL x=max(i+sq,max(r*2,(sq-r)*2)),y=max(j+sq,max(r*2,(sq-r)*2));
LL tmp=(n-x+1)*(m-y+1);
if(n>=x&&m>=y){
if(i!=0&&j!=0){
if(r*2!=sq) ret+=tmp*4;
else ret+=tmp*2;
}
else{
if(r*2!=sq) ret+=tmp*2;
else ret+=tmp;
}
}
}
}
}
return ret;
}

int main(){
//freopen("in.txt","r",stdin);
LL cas=0;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&M);
printf("Case %lld: %lld\n",++cas,solve(N,M));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: