天天看点

NYOJ--319-Splitting plane

Splitting plane

1000 ms  |  内存限制:

65535

2

We have seen many problems about straight line splitting plane.Today ours problem has little difference with before.Because this time we don't use a straight line to splitting plane,we use a broken line to splitting plane.

For example , a broken line can divided a plane to two parts,  two broken line can divided a plane to seven parts.

The first line input an integer N(1<N<10000)

Then N trip test data

Each test data have a integer M. M represents the number of broken lines.(0<M<=10000)

输出

For each test case, please output plane's biggest segmentation number, the output of each instance of one line.

样例输入

21

2

样例输出

27

来源

​​HDOJ​​

推规律题:

分割平面的个数=交点个数+顶点个数+1。

令f(n-1)为前n-1条折线分割的平面数,当添加第n条折线时。

因为每一条边与前n-1条折线的两条边都相交,故增加的交点数为2*2*(n-1),顶点增加1,故

f(n)=f(n-1)+4(n-1)+1,再累加。

参考代码;

/************
NYOJ319
Result:Accepted
Times:164ms;
Author:jiabeimuwei
*************/
#include<stdio.h>
int f(int n)
{
    int i,j=1;
    for(i=1; i<=n; i++)
    {
        j+=4*i-3;
    }
    return j;
}
int main()
{
    int t, n, m, i = 0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&m);
            printf("%d\n",f(m));
    }
    return 0;
}      

公式套用:

#include<stdio.h>
int main()
{
    long long  n,m;
    scanf("%lld",&m);
    while(m--)
    {
        scanf("%lld",&n);
        printf("%lld\n",(4*n*n+2*n)/2+1-2*n);
    }
    return 0;
}