Thursday, September 02, 2010

[UVa] 913 - Joana and the Odd Numbers

The problem is actually inside the generation of the last number in the given line. I followed the method described in the Algorithmist page of it.

To get the number of numbers in a particular line "i" you can use 2(i)-1. So you can easily obtain the number of numbers in the line that you are given to process. But you have to obtain the number of numbers before that line too. As we know that can be obtained as SUM[2(i)-1] with the limits i=1 to N-1. With the acquired result you just add 2(n)-1 and voila. But this is much simpler, with the limit changed to i=1 to N. So we get N(N+1)-N. Which actually yields to N^2. As we know, the last number is also odd so we obtain it using 2(N^2)-1, putting the equation in the braces.
Now what do we need is, judging the 2(N^2)-1 as LAST, we need, LAST-4 + LAST-2 + LAST.
Simplified 3*LAST-6. And you have the answer.


#include <iostream>
#include <cmath>
using namespace std;


int main() {

long long int n;

while (scanf("%lld",&n)==1) {
n++; /*Having an offset problem without this, a crazy workaround*/
n=ceil(n/2); /*You have to go backwards to find the line you gotta work on*/
printf("%lld\n",3*(2*n*n-1)-6);
}

return 0;
}

No comments:

Post a Comment

Post your comment here. If you want to say something about programming problems, scripts, software etc, please try to be as descriptive as possible.

Connect Rapoo MT750S with Linux (Tested on Manjaro)

 I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...