题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4486
题目:
Pen Counts
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 571 Accepted Submission(s): 371
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
题目大意:
用总长为 n 的栅栏围成一个三角形的鸡圈(必须全部用完),即三角形周长为 n 。
本题中:旋转所得与本身按同一种,但也有种特殊情况:
一般三角形(非等腰/等边),上下翻转后得到的三角形与其本身按两种计算(等腰/等边三角形翻转后仍按同一种)。
给出 n,求最多可以组成多少种三角形
思路:
枚举第一条边,找第二条边上下界,先都按普通三角形处理(都乘2),出现等腰/等边再减去。
求第二边上下界:
设三边为x , y , z(x <= y <= z),枚举 x (即 x 和 n 已知)。
因为两边之差小于第三边,所以 0 <= z - y < x 。
又 x + y + z = n ,得(n - 2x) / 2 < y <= (n - x) / 2.
即 n / 2 - x + 1 <= y <= (n - x) / 2。
AC代码:
#include<iostream>枚举第一条边,找第二条边上下界,先都按普通三角形处理(都乘2),出现等腰/等边再减去。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
int p;
int n;
int main()
{
scanf("%d",&p);
int i,j,k,val;
while(p--)
{
scanf("%d%d",&j,&n);
int y1,y2;
int ans=0;
for(i=1;i<=n/3;i++)
{
y1=max(i,(n-2*i)/2);
if(y1==(n-2*i)/2)y1++;//要注意,两者不相等
y2=(n-i)/2;
ans+=2*(y2-y1+1);
if(y1==i){
ans--;
}
if(y2!=i&&y2==(n-i-y2)){//要避免等边三角形时多减去一
ans--;
}
}
printf("%d %d\n",j,ans);
}
return 0;
}