### 913 - Joana and the Odd Numbers

Posted:

**Sun Oct 08, 2006 10:06 pm**Do i need to precalculate??

how i determine the 1st integer of the line N ????

how i determine the 1st integer of the line N ????

The Online Judge board

https://uva.onlinejudge.org/board/

https://uva.onlinejudge.org/board/viewtopic.php?f=34&t=12335

Page **1** of **3**

Posted: **Sun Oct 08, 2006 10:06 pm**

Do i need to precalculate??

how i determine the 1st integer of the line N ????

how i determine the 1st integer of the line N ????

Posted: **Sun Oct 08, 2006 11:00 pm**

No need to precalculate anything. You can find a general formula to find the last integer in the line N. First list all the numbers -

Line 1 - 1

Line 3 - 7

Line 5 - 17

Line 7 - 31

Line 9 - 49

Line 11 - 71

...

Line N - ?

The formula is not so tough. Try it.

Line 1 - 1

Line 3 - 7

Line 5 - 17

Line 7 - 31

Line 9 - 49

Line 11 - 71

...

Line N - ?

The formula is not so tough. Try it.

Posted: **Mon Oct 09, 2006 8:56 am**

Thanks J@N.... I got the formula

Posted: **Mon Nov 27, 2006 9:15 pm**

can anyone tell me whats the wrong with my code?

Posted: **Mon Nov 27, 2006 9:34 pm**

Try the samples

**Input:**
**Output:**
Use 'long long' istead of 'long double'. Dont forget to remove your code. And remember that if you want to post a code then paste your code using 'code' from the editor. Hope these help.

Code: Select all

```
99999999
1000000001
```

Code: Select all

```
14999999999999991
1500000005999999997
```

Posted: **Mon Dec 04, 2006 12:05 pm**

can anyone expain why i am getting TLE for this code

plzzzzzzzzzz help me!!!

thanx

Code: Select all

```
CUT
```

thanx

Posted: **Mon Dec 04, 2006 12:10 pm**

Read the previous posts and run your code with those samples. You will find the reason yourself.

Posted: **Mon Dec 04, 2006 12:45 pm**

thanx for reply..but i am still confused ...i used long long...will u explain my bug plzz?

Posted: **Mon Dec 04, 2006 12:54 pm**

'long long' is not the problem.
If the value of 'n' is big, like 1000000001, then you have to use this loop for (1000000001/2) times. Which is too big. Try to figure out the last odd number in a certain line. You dont have to use any loop. You can find a general formula. Then the result can be found in just O(1) time, which means no loop. Hope you can generate the formula.

Code: Select all

```
while(n!=1)
{
sum=sum+(n*2);
n-=2;
}
```

Posted: **Mon Dec 04, 2006 1:19 pm**

thanx jan i got the formula and got AC

Posted: **Thu May 24, 2007 9:12 pm**

functoin pow() returns double, and you are assigning it to a 'long long' variable. So, precision problems can occur easily. Try to get rid of floating point calculations.

Posted: **Thu May 24, 2007 9:53 pm**

thank u very much mr jan.

you are a great helper.

you are a great helper.

Posted: **Thu Oct 25, 2007 1:46 pm**

Code: Select all

```
deleted after AC
```

Posted: **Thu Oct 25, 2007 2:07 pm**

The input set contains valid cases. So, no need to check n. And the function should be

Hope these help.

Code: Select all

`long long odd_num(long long num)`