Pen Counts
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 224 Accepted Submission(s): 127
Problem Description
Chicken farmer
Xiaoyan is getting three new chickens, Lucy, Charlie and CC. She wants to build a chicken pen so that each chicken has its own, unobstructed view of the countryside. The pen will have three straight sides; this will give each chicken its own side so it can pace back and forth without interfering with the other chickens. Xiaoyan finds a roll of chicken wire (fencing) in the barn that is exactly N feet long. She wants to figure out how many different ways she can make a three sided chicken pen such that each side is an integral number of feet, and she uses the entire roll of fence.
Different rotations of the same pen are the same, however, reflections of a pen may be different (see below).
Input
The first line of input contains a single integer P,(1<= P <=1000), which is the number of data sets that follow. Each data set should be processed identically and independently.
Each data set consists of a single line of input. It contains the data set number, K, and the length of the roll of fence, N, (3 <= N <= 10000).
Output
For each data set there is a single line of output. It contains the data set number, K, followed by a single space which is then followed by an integer which is the total number of different three-sided chicken pen configurations that can be made using the entire roll of fence.
Sample Input
5 1 3 2 11 3 12 4 100 5 9999
Sample Output
1 1 2 5 3 4 4 392 5 4165834
Source
Greater New York 2012
这个题我写了大部分,线性时间内求得三角形的数目没问题,却在判断三角形中等腰三角形的数目时出了问题,无奈之下百度一下解题报告,看完后恍然大悟,对于每一个确定的a,它的等腰三角形数目最多为两个,对于等边三角形则要注意不要多减1次
#include
int main()
{
int t,n,i,m,ans,temp1,temp2,a,b,c,temp;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
temp1=m/3;
ans=0;
for(a=1;a<=temp1;a++)
{
temp2=(m-a)/2;//b上限
temp=0;
b=(m/2-a+1)>a?(m/2-a+1):a;//b下限
if(a==b)
{
ans--;
temp=a;
}
if(temp!=temp2&&temp2==m-a-temp2)//判断b==c,并防止一个等腰三角形三角形减去两次
ans--;
ans+=2*(temp2-b+1);//不管等腰三角形先都加2,在上面判断减一
}
printf("%d %d\n",n,ans);
}
return 0;
}