折线分割平面
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 36109 Accepted Submission(s): 24197
Problem Description
我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要求的是n条折线分割平面的最大数目。比如,一条折线可以将平面分成两部分,两条折线最多可以将平面分成7部分,具体如下所示。
Input
输入数据的第一行是一个整数C,表示测试实例的个数,然后是C 行数据,每行包含一个整数n(0<n<=10000),表示折线的数量。
Output
对于每个测试实例,请输出平面的最大分割数,每个实例的输出占一行。
Sample Input
2 1 2
Sample Output
2 7
先考虑直线分割, 第n条线与前n-1条线最多有n-1个交点, 那么就会多出(n-1) + 1部分;
再看折线, 第n条线与前n-1条线最多有4*(n-1)个交点, 那么就会多出4*(n-1)+1部分;
直线分割有公式:S=n*(n+1)/2+1;
当然了, 折线分割也有公式滴:S=(2*n-1)*n+1;
递推:
#include <iostream>
#include <stack>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
long long dp[10005];
int main(){
int c;
cin >> c;
dp[0]=1;
dp[1]=2;
for(int i=2; i<=10000; i++){
dp[i]=dp[i-1]+4*(i-1)+1;
}
while(c--){
int n;
cin >> n;
cout << dp[n] << endl;
}
return 0;
}
套公式:
#include <iostream>
#include <stack>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
long long dp[10005];
int main(){
int c;
cin >> c;
while(c--){
int n;
cin >> n;
cout << (2*n-1)*n+1 << endl;
}
return 0;
}