Time Limit: 10 second
Memory Limit: 2 MB
問題描述
同一平面內有n(n≤500)條直線,已知其中p(p≥2)條直線相交與同一點,則這n條直線最多能將平面分割成多少個不同的區域?
Input
兩個整數n(n≤500)和p(2≤p≤n)。
Output
一個整數,代表最多分割成的區域數目
Sample Input
12 5
Sample Output
73
【題目鏈接】:http://noi.qz5z.com/viewtask.asp?id=9303
【題解】
先考慮那P條相交于一點的線;
它們會形成2*p個平面;
然后再考慮1條一條的增加線段;
設再加一條線段之前線段樹為i;
則最好的情況是這條新加的線段和每條線段都相交;
這樣又會多出i+1個平面來;
則有fi+1=fi+i+1;
這樣就搞出遞推公式了;
【完整代碼】
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se secondtypedef pair<int,int> pii;
typedef pair<LL,LL> pll;void rel(LL &r)
{r = 0;char t = getchar();while (!isdigit(t) && t!='-') t = getchar();LL sign = 1;if (t == '-')sign = -1;while (!isdigit(t)) t = getchar();while (isdigit(t)) r = r * 10 + t - '0', t = getchar();r = r*sign;
}void rei(int &r)
{r = 0;char t = getchar();while (!isdigit(t)&&t!='-') t = getchar();int sign = 1;if (t == '-')sign = -1;while (!isdigit(t)) t = getchar();while (isdigit(t)) r = r * 10 + t - '0', t = getchar();r = r*sign;
}//const int MAXN = x;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);int n,p;int main()
{//freopen("F:\\rush.txt","r",stdin);rei(n);rei(p);LL ans = 2*p;rep1(i,p+1,n)ans = ans+i;cout << ans << endl;return 0;
}