913 - Joana and the Odd Numbers(UVA)

Run brute force , you'll get elements like below:

Line
  0-------------   1

  1-------------   3      5      7

  2-------------   9     11    13   15    17

  3-------------  19   21    23   25    27   29    31

  4-------------  33   35    37  39     41   43    45  47  49

Notice carefully : last element in each line propagates though the sequence given below :

6     (7-1)

10   (17-7)

14   (31-17)

18   (49-18)

You need to iterate propagation  N /2  (number of lines) times



If you face any problem handling the code you may see the code given below: 

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
    int n;
    while(scanf("%lld",&n)==1)
    {
        ll line=n/2;
        ll ans=1;
        ll extension=6;
        for(ll ln=1;ln<=line;ln++)
        {
            ans=ans+extension;
            extension+=4;
        }
        printf("%lld\n",ans+(ans-2)+(ans-4));
    }
    return 0;
}


Comments

Popular posts from this blog

PopIt Serially support page

10405 - Longest Common Subsequence (UVA)