HDU 4486 Pen Counts

Pen Counts

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 443    Accepted Submission(s): 276


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
 

比赛没有做出来,借鉴的别人的博客!
设三角形的三条边X Y Z,满足 X <= Y <= Z;
Y + Z = N - X;
令Z - Y = N - X = T;
则 0 <= T < X;
带入得: Y = (N-X-T)/2;
化简得: N/2 - X + 1 <= Y <= (N-X) / 2
=>           MAX(X,N/2 - X + 1) <= Y <= (N-X)/2;
枚举最小边X 即可!

还要减去两边相等 或者三边相等的情况,最后总情况乘以2即可!


#include
#include
using namespace std;
int main(){
	int T,cnt,n;
	scanf("%d",&T);
	while(T--){
		int ans = 0,x;
		scanf("%d%d",&cnt,&n);
		for (x = 1; x <= n/3; ++x){
			int tmp = n/2-x+1;
			int y1 = max(tmp,x), y2 = (n-x)/2;
			if (x == y1)--ans;
			if(x != y2 && y2 == n-x-y2)--ans;
			ans += (y2-y1+1)*2;
		}
		printf("%d %d\n",cnt,ans);
	}
	return 0;
}


你可能感兴趣的:(数学,HDU)