UVA - 10079 Pizza Cutting (直线划分平面问题,公式解决)

17 篇文章 0 订阅
16 篇文章 0 订阅


Time Limit: 8333MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

Status

Description

When someone calls Ivan lazy, he claims that it is his intelligence that helps him to be so. If hisintelligence allows him to do something at less physical effort, why should he exert more? He alsoclaims that he always uses his brain and tries to do some work at less effort; this is not his laziness,rather this is his intellectual smartness.

Once Ivan was asked to cut a pizza into seven pieces to distribute itamong his friends. (Size of the pieces may not be the same. In fact, hispiece will be larger than the others.) He thought a bit, and came to theconclusion that he can cut it into seven pieces by only three straight cutsthrough the pizza with a pizza knife. Accordingly, he cut the pizza in thefollowing way (guess which one is Ivan’s piece):

One of his friends, who never believed in Ivan’s smartness, was startledat this intelligence. He thought, if Ivan can do it, why can’t my computer?So he tried to do a similar (but not exactly as Ivan’s, for Ivan will criticizehim for stealing his idea) job with his computer. He wrote a program thattook the number of straight cuts one makes through the pizza, and output a number representing themaximum number of pizza pieces it will produce.

Your job here is to write a similar program. It is ensured that Ivan’s friend won’t criticize you fordoing the same job he did.


Input

The input file will contain a single integer N (0 ≤ N ≤ 210000000) in each line representing the numberof straight line cuts one makes through the pizza. A negative number terminates the input.


Output

Output the maximum number of pizza pieces the given number of cuts can produce. Each line shouldcontain only one output integer without any leading or trailing space.


Sample Input

5

10

-100


Sample Output

16

56


题目讲得是切Pizze,但是经典的直线分割平面的问题,运用公式 m = n(n+1)/2+1 就可以了

公式推导:(来自百度百科:http://baike.baidu.com/link?url=q-1sNnyEYUuO8ta0vLhwol-Yw6IbDKc9hYhKgoQgbj6St4WwYbw91hCgvsgOHFnC_4SkPDsa0GRxYGSJvEAUy_)

如果没有一条直线,那么平面就可以看作1个部分;

如果有1条直线,那么平面就被分成2个部分;

如果有2条直线,又可分为两种情况:

第2条直线与第1条直线不相交,可分平面3部分,

第2条直线与第1条直线相交,可分平面4部分,

同理,3条直线最多可分平面7部分,

4条直线最多可分平面11部分,

再把这几个数分解,发现1=1,2=1+1,4=1+1+2,7=1+1+2+3,11=1+1+2+3+4。

由此我们可得到直线分平面公式:n条直线最多能把平面分成1+1+2+3+……+n个部分,

即最多能把平面分成(n(n+1)+2)/2个部分,化简以后为(n^2)/2+1/2n+1。



  (以下改变自博客:http://blog.csdn.net/subo86/article/details/4716873)

平面上只要多出现一条直线,就能至少多把平面分出一部分,而若此直线与其他直线有n个交点,就再能把平面多分出n个部分,因此若想把平面划分的部分最多, 新添入的直线必须与前k条直线交k个点,即第二条直线要与第一条直线交1个点,第三条要与前两条交2个点,……,第n条与前n-1条交n-1个 点,这样,第二条直线多划分出1+1=2个部分,第三条直线多划分出1+2=3个部分,……,第n条直线多划分出1+n个部分。而 第一条直线把平面划分出2个部分,因此n条直线能划分平面的块数为: 
2+2+3+4+5+…+(n-1)+n  = 1+(1+2+3+4+5+…+(n-1)+n)  = 1+(1+n)*n/2

 对n条直线最多划分平面数:

    (1)使用递归

         f(n) = n + f(n-1) ,   n > 1     

         f(n) = 2               ,   n = 1

    (2)使用递推

         n = 1,  S1 = 2        

         n > 1,  Sn = 2 + 2 + 3 + …… + n  = 1 + n * (n+1) / 2


AC代码:

#include <iostream>
using namespace std;
int main()
{
    long long n;
    while(cin>>n,n>=0LL)
    {
        cout<<n*(n+1LL)/2LL+1LL<<endl;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值