Polyline division plane hdu-2050] [[recursion to find the law]

Polyline division plane

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35192    Accepted Submission(s): 23630


Problem Description
We've seen a lot of straight division title plane, today's change the subject slightly, we ask for is the n fold line dividing the maximum number of planes. For example, a fold line may be divided into two planar, two folds up into the planar portion 7, as shown below.
 

Input
The first line of input data C is an integer, indicates the number of test cases, then the C line data, each line contains an integer n (0 <n <= 10000), represents the number of fold lines.

 

Output
Each test for instance, the maximum division number output plane, the output of each row for instance.

 

Sample Input
 
  
2 1 2
 

Sample Output
 
  
2 7

Solution: first discuss the issue straight division planes. If n = 1, a [1] = 2; n = time 2, a [2] = 4; n = 3 when, a [3] = 6; when n = n, order to share up plane, this straight line must intersect a straight line as much as possible, i.e. the n-1 intersects with a straight line, then this straight line is split into two rays and n-2 line segments. Therefore, a [n] = a [n-1] + n-2 + 2; i.e., a [n] = a [n-1] + n;

Then talk about this question, while if n = 1, a [1] = 2; when n = time 2, n = 7; if n = n, the plane in order to share up, the two rays polyline edges to the same as many lines intersect, i.e. for each ray and 2 * (n-1) lines intersecting two rays is 2 * 2 * (n-1); in this case, the newly generated 2 * 2 * (n-1) +1 th surface;

That is: a [n] = a [n-1] + 4 * (n-1) +1;


code show as below:

#include<cstdio>
#include<algorithm>
using namespace std;
int a[10005];
int main()
{
	int t;
	a[1]=2;
	for(int i=2;i<=10000;i++)
		a[i]=a[i-1]+4*(i-1)+1;
	scanf("%d",&t);
	while(t--){
		int n;
		scanf("%d",&n);
		printf("%d\n",a[n]);
		
	}
	
	return 0;
}




发布了79 篇原创文章 · 获赞 5 · 访问量 1万+

Guess you like

Origin blog.csdn.net/DNMTOOBA/article/details/79404457