Reprinted from: http://blog.csdn.net/a576323437/article/details/6163850
Title: http://acm.hdu.edu.cn/showproblem.php?pid=2050
We have seen many problems of dividing planes by straight lines. Today's problem is slightly changed. What we require is the maximum number of planes divided by n polylines. For example, a polyline can divide a plane into two parts, and two polylines can divide a plane into up to 7 parts.
Problem solving ideas:
1 Recursion, first analyze the situation of dividing the plane by a straight line. When adding the nth straight line, there are at most n-1 intersections with the previous straight line, and the divided part is more than (n-1)+1. ;
The same is true for 2 polylines, f(1)=2, f(2)=7, first draw the first n-1 polylines, when adding the nth strip, the new intersection with the graph is at most 2* 2(n-1), so the divided part is more than 2*2(n-1)+1, so f(n)=f(n-1)+4*(n-1)+1, n>=3
- #include <stdio.h>
- intmain ()
- {
- int n,i,k,j;
- __int64 a[10010];
- scanf("%d",&n);
- for(i=1;i<=n;i++)
- {
- scanf("%d",&k);
- a[1]=2;
- for(j=2;j<=k;j++)
- a[j]=a[j-1]+4*(j-1)+1;
- printf("%I64d\n",a[k]);
- }
- return 0;
- }
import java.util.Scanner; public class HDU2050 { static Long[] f = new Long[10000+10]; public static void main(String[] args) { Scanner in = new Scanner(System.in); dfs(); int t = in.nextInt(); while (t-- != 0) { int n = in.nextInt(); System.out.println(f[n]); } } public static void dfs() { f[1] = 2L; f[2] = 7L; for (int i = 3; i < 10000+10; i++) { f[i] = f[i - 1] + 4*(i-1) + 1; } } }