UVA Problem 10079 – Pizza Cutting Solution

UVA Problem 10079 – Pizza Cutting Solution:


Click here to go to this problem in uva Online Judge.

Solving Technique:

This is a very simple mathematics problem. It can be solved with Gauss‘es arithmetic series formula. Which is the sum 1 to nth number. The formula is,

n * ( n + 1 )/2

The problem just requires us find the number of maximum possible pieces for given the number of cuts. At first the pattern may not be visible. Use pencil and paper draw and verify outputs.

We need add 1 since doing 1 cut in pizza makes two pieces. So the arithmetic series formula is 1 less. Again if we make two cuts using arithmetic progression formula it is 1 less again. It happens for all inputs. So we can add 1 with result of this formula and that is the answer.

This problem can also be solved using simple loop and sum from 1 to n.

A corner case may be that the number may not fit int range.

Important:  Be sure to add or print a new line after each output unless otherwise specified. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.


Input:

5
10
-100

Output:

16
56

Code:

/**
 * @author  Quickgrid ( Asif Ahmed )
 * @link    https://quickgrid.wordpress.com
 * Problem: UVA 10079 Pizza Cutting
 */

#include<stdio.h>

int main(){
    long long n;
    while(scanf("%lld", &n)){
        if(n < 0)
            return 0;

        /**
         * Arithmetic series 1 to n with extra 1
         */
        printf("%lld\n", 1 + n * (n + 1) / 2);
    }
    return 0;
}

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s