Solution লেবেলটি সহ পোস্টগুলি দেখানো হচ্ছে৷ সকল পোস্ট দেখান
Solution লেবেলটি সহ পোস্টগুলি দেখানো হচ্ছে৷ সকল পোস্ট দেখান

বুধবার, ৬ ফেব্রুয়ারী, ২০১৩

Solution of Light OJ 1038 - Race to 1 Again

                 শীতের সকালে সুন্দর রোদের আলো থাকে । সেই আলো তির্যকভাবে পড়ে ঘাসের ডগায় জমে থাকা শিশিরের উপর ।  শিশির থেকে সেই আলো বিচ্ছুরিত হয়ে ফিরে আসে আমাদের চোখে এবং মুখে । তখনকার চাঙ্গা অনূভব সবার মধ্যে সর্বদা বজায় থাকুক - এই প্রত্যাশায় চল বন্ধুরা আমরা শুরু করি আমদের নবজীবনের গান ।  

                 শুরুতেই বলে নেই আজকের সমস্যার Topics হল Probability/Expected Value । সমস্যার  সারসংক্ষেপ নিচে প্রদান করা হল ।

      ১। Rimi  integers  সম্পর্কে  নতুন একটা জিনিস জানল আর তা হল 
                  - any positive  integer greater than 1 can be divided by its  
              divisors. 
  

    ২।  Integer এর property  কে নিয়ে সে খেলা শুরু করল ।  

    ৩।  সে যে কুন একটা  number N নিল  এবং এইটার নাম দিল  .

     ৪। In each turn he randomly chooses a divisor of D (1 to D)
    
   ৫। Then he  divides D by the number to obtain new D

   ৬। He repeats this procedure until D becomes 1

What is the expected number of moves required for N to become 1.

Solution hints :

      1.  Suppose we want to calculate E(N) where N is a number,k is the number of it’s distinct prime factors and p is the number of primes < n

                                                                                                 E(N)=1+(1/p)*(E(N/a1) + E(N/a2) + E(N/a3) … + E(N/ak)) + ((p-k)/p) * E(N)


     2. It means after one step we can choose a correct prime numbers(from the factors of N) or one of the wrong ones from (p-k) ones


    3. Such a recurrence can be changed to


       E(N) – ((p-k)/p) * E(N) = 1+(1/p)*(E(N/a1) + E(N/a2) + E(N/a3) … + E(N/ak)) 
        

      E(N)*(k/p) = 1+(1/p)*(E(N/a1) + E(N/a2) + E(N/a3) … + E(N/ak))


    4. We can apply dynamic programming then.

U can Alo try UVA ACM 11762 

শনিবার, ১৭ নভেম্বর, ২০১২

Solution of Light Online Judge : 1043 - Triangle Partitioning

      কেমন আছও বন্ধুরা ? আজকে আমরা এমন একটা সমস্যার সমাধান করব যাতে আমাদের Previous Knowledge (Class 9-10) কাজে লাগবে । তাহলে চল শুরু করা যাক ।  


Problem Statement :
    You are given AB, AC and BC. DE is parallel to BC. You are also given the area ratio between ADE and BDEC. You have to find the value of AD.

Solution Idea :
       1. Let tri-angle ADE / BDEC = m/n .        2 .  কাজেই ADE / ABC = m/(m+n)
       3. From উপপাদ্য 3.10 (Higher Geometry -class 9-10) we know that
 
                 ADE / ABC = DE^2 / BC ^ 2

                 বা , sqrt(m)/sqrt((m+n))  = DE/BC
          
                 বা , x = DE/BC 

       where x = sqrt(m)/sqrt((m+n))


      4.  As ADE and ABC are সদৃশকোণী ত্রিভুজ so

                      AD/AB = DE /BC

                   বা , AD = DE/BC* AB

                   বা , AD =   x * AB . 

 ৫ । Simply use this formula .

Code :

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

int main()
{
    int i,test;
    double AB,BC,CA,AD,DE,ratio;
    cin>>test;
    for(i=1;i<=test;i++)
    {
        cin>>AB>>CA>>BC>>ratio;
        ratio = ratio/(ratio+1);
        AD = AB*sqrt(ratio);
        printf("Case %d: %lf\n",i,AD);
    }
    return 0;
}




             
           

বৃহস্পতিবার, ১৩ সেপ্টেম্বর, ২০১২

Solution Of Light Online Judge : 1065 - Number Sequence

In problem 1065 at Light Online Judge, Modular Fibonacci, our task is to calculate huge Fibonacci numbers, modulo some given integer.

Interpretation

We are given two integers, 0 \leq n \leq 2147483647 and 0 \leq m \leq 20. We are then asked to calculate and output the nth Fibonacci number, modulo 2^m.
As you can see, n can be pretty big. Let’s have a look at a few different methods to compute the nth Fibonacci number.
The naive method, where we do two recursive calls without any optimizations, runs in exponential time. Computing the largest allowed Fibonacci number with this method would take ages, literally.
The dynamic programming method, where we save each computed value (or at least the last two), runs in linear time. We only need a few seconds to compute the largest allowed Fibonacci number using this method, but that’s still too slow. We need to be able to run all test cases in under 2 seconds.

      There is no specified maximum amount of test cases, but they could be as many as ten, or even hundred, and all of them could possibly be the maximum value.
 
         There is also a closed-form formula for calculating the nth Fibonacci number, which of course runs in constant time. But it assumes that we are using Real numbers (with infinite precision), which we do not have access to in computers, and thus we cannot use it here.
     
         So what have we learned? A linear-time algorithm is too slow, so we need something faster. A constant-time algorithm would be nice, but seems to be a bit optimistic. We are definitely looking for something there in between, maybe something that takes \log{n} time?

Matrix Exponentiation

          There is another way to calculate the nth Fibonacci number, that is, using matrix exponentiation.
          If you don’t know much about matrices, now would be a good time to head over to Khan Academy and take a peek at videos about linear algebra (specifically “Introduction to matrices” and “Matrix multiplication”).
         The Fibonacci numbers have the following matrix identity:
\displaystyle \left(  \begin{array}{cc}  \text{fib}_{n 2} & \text{fib}_{n 1} \\  \text{fib}_{n 1} & \text{fib}_n  \end{array}  \right)=\left(  \begin{array}{cc}  1 & 1 \\  1 & 0  \end{array}  \right)\times \left(  \begin{array}{cc}  \text{fib}_{n 1} & \text{fib}_n \\  \text{fib}_n & \text{fib}_{n-1}  \end{array}  \right)
Now if we put the first Fibonacci numbers into the right-most matrix, and instead of multiplying it once with the other matrix, we multiply it n times, we get:
Or equivalently:
\displaystyle \left(  \begin{array}{cc}  \text{fib}_{n 1} & \text{fib}_n \\  \text{fib}_n & \text{fib}_{n-1}  \end{array}  \right)=\left(  \begin{array}{cc}  1 & 1 \\  1 & 0  \end{array}  \right)^n
Now we just have to find a fast way to raise a matrix to the nth power.
Let’s assume we’re trying to raise a number x to the nth power (where n \in \mathbb{Z}, meaning that it is an integer). One way would be to multiply x with itself n times. This is pretty slow and needs n multiplications.
A faster algorithm can be derived with the following identity in mind.
\displaystyle  x^n=  \begin{cases}  1, & \mbox{if } n = 0 \\  x \cdot x^{n - 1}, & \mbox{if } n \mbox{ is odd} \\  \left( x^{\frac{n}{2}} \right)^2, & \mbox{if } n \mbox{ is even}  \end{cases}
The key thing is to notice that we use x^{n/2} twice, but we only need to calculate it once.
Here is a function that calculates x^n using our fast algorithm:

Here is a function that calculates x^n using our fast algorithm:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static int pow(int x, int n) {
    // our base case: x^0 = 1
    if (n == 0)
        return 1;
    // if n is odd
    if (n % 2 != 0)
        return x * pow(x, n - 1);
    // if n is even, calculate x^(n / 2)
    int sqrt = pow(x, n / 2);
    // and return it squared
    return sqrt * sqrt;
}
This algorithm, often known as exponentiation by squaring, also works for matrix exponentiation. The only change is that when n = 0, we return the identity matrix instead of 1.
I’ve included a small matrix library in the complete source code (below). There, the exponentiation method looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// raise the matrix to the n-th power
public Matrix pow(int n) {
    if (n == 0) {
        // if n is 0, we return the identity matrix
        Matrix res = new Matrix(getRows(), getCols());
        for (int i = 0; i < getRows() && i < getCols(); i++)
            res.set(i, i, 1);
        return res;
    } else if (n % 2 == 0) {
        // if n is even, return the square root, squared
        Matrix res = pow(n / 2);
        return res.multiply(res);
    } else {
        // if n is even, return the matrix multiplied by the matrix to the (n - 1)th power
        return multiply(pow(n - 1));
    }
}
This algorithm only uses about \log{n} multiplications. Isn’t this the running time we were trying to achieve?

Implementation

Now we can calculate the nth Fibonacci number, in a fast way, using matrix exponentiation. The main code now looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public void run(Scanner in, PrintWriter out) {
    // initialize the matrix to:
    // [ 1  1 ]
    // [ 1  0 ]
    Matrix fib = new Matrix(2, 2);
    fib.set(0, 0, 1);
    fib.set(0, 1, 1);
    fib.set(1, 0, 1);
    fib.set(1, 1, 0);
    // loop through each test case
    while (in.hasNextInt()) {
        // read n and m
        int n = in.nextInt(),
            m = in.nextInt();
        // calculate fib^n
        Matrix fib2 = fib.pow(n);
        // output the n-th Fibonacci number
        out.println(fib2.get(1, 0));
    }
}
But there is still one thing we have yet to do. The problem asks us to output the nth Fibonacci number, modulo 2^m. We can easily modify our code to do that. We just need to do every computation modulo 2^m. Look inside the complete source for details.

CODE

 
















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

class MAT
{
    public:
        long int a,b,c,d;
}temp,req,cal,base;

int Mod;

MAT& multiply(MAT &A,MAT &B)
{
    temp.a = A.a*B.a+A.b*B.c;
    temp.a=temp.a%Mod;

    temp.b = A.a*B.b+A.b*B.d;
    temp.b=temp.b%Mod;

    temp.c = A.c*B.a+A.d*B.c;
    temp.c=temp.c%Mod;

    temp.d = A.c*B.b+A.d*B.d;
    temp.d=temp.d%Mod;

    return temp;
}

void POW(int n)
{
    if(n==1)
    {
        req = cal;
    }
    else if(n%2)
    {
        POW(n-1);
        req = multiply(cal,req);
    }
    else
    {
        POW(n/2);
        req = multiply(req,req);
    }
}

int main()
{
    int m,i1,test,a1,b1;
    long long int N;
    cin>>test;
    for(i1=1;i1<=test;i1++)
    {
        cin>>a1>>b1>>N>>m;
        Mod=int(pow(10,m));
        cal.a=1,cal.b=1,cal.c=1,cal.d=0;
        base.a=a1+b1,base.b=b1,base.c=b1,base.d=a1;

        if(N==0)
        {
            printf("Case %d: %d\n",i1,a1);
        }
        else if(N==1)
        {
            printf("Case %d: %d\n",i1,b1);
        }
        else if(N==2)
        {
            printf("Case %d: %d\n",i1,a1+b1);
        }
        else
        {
            POW(N-1);
            req = multiply(req,base);
            printf("Case %d: %ld\n",i1,req.b);
        }
    }
    return 0;
}
We were ask


Conclusion

We were asked to calculate huge Fibonacci numbers, and we figured out a fast way to do it with matrix exponentiation. Our algorithm is very fast, calculating the 2147483647th Fibonacci number (modulo 2^{20}) in less than 100 milliseconds. That’s pretty good, considering that the 2147483647th Fibonacci number has 448797540 digits.
So what do you think? Did/would you solve it differently? Leave a comment and let me know.

Test Case :

Sample Input

 
6
2 1 11 3
2 1 0 3
0 1 11 3
0 1 42 4
0 1 22 4
0 1 21 4



Sample Output
Case  1: 199
Case  2: 2
Case 3: 89
Case 4: 4296
Case 5: 7711
Case 6: 946

শুক্রবার, ৩১ আগস্ট, ২০১২

1090 - Trailing Zeroes (II)

Number theory এর খুব সুন্দর একটি সমস্যা । আগ্রহীরা আগে নিজে চেষ্টা করে দেখতে পার ।

Problem Statement:
       Find the number of trailing zeroes for the following function: mCn * p^q

Solution :

     ১। যেহেতু mCn = m!/(n!)/(m-n) ! কাজেই প্রথমে m!  ,n! এবং (m-n)! এ কতটি ২ আছে তা বের কর    
      ২ ।  মনে কর তাদের মধ্যে ২ আছে যথাক্রমে a2,b2,c2
      ৩। তাহলে mCn এ ২ আছে no_of_2 = a2-(b2+c2)
      ৪।  একই ভাবে  mCn এ ৫ আছে no_of_5 = a5-(b5+c5)
      ৫। নিচের Algorithm follow করে P এর মধ্যে ২ কতবার আছে তা বের কর । মনে কর P তে ২ আছে n1 বার ।

Algorithm:
     while( n>0 && !(n%2) )
    {
            n=n/n1;
            ++no;
    }

   ৬।  কাজেই mCn*p^q এ total 2 আছে  no_of_2 += n1 * q

    ৭। একই ভাবে  mCn*p^q এ total 5 আছে  no_of_5 .

   ৮।   এইবার নিচের step টি খেয়াল করে দেখ ।

Algorithm :

if(no_of_2<no_of_5)
            zero=no_of_2;
        else
            zero=no_of_5;

৯। বাহ এইতো খুব সুন্দর মতই শেষ করতে পেরেছ এই সমস্যার সমাধান ।

Code :

#include<iostream>
using namespace std;

int no_of_2,no_of_5;

int  no_of_zero_in_factorial(int  n,int n1)
{
    int  number=0;
    while(n>0)
    {
        number+=n/n1;
        n/=n1;
    }
    return number;
}

void  calculate_zero(int  m,int  n)
{
    int a2,a5,b2,b5,c2,c5;
    a2 = no_of_zero_in_factorial(m,2);
    a5 = no_of_zero_in_factorial(m,5);
    b2 = no_of_zero_in_factorial(n,2);
    b5 = no_of_zero_in_factorial(n,5);
    c2 = no_of_zero_in_factorial(m-n,2);
    c5 = no_of_zero_in_factorial(m-n,5);

    no_of_2=a2-(b2+c2);
    no_of_5=a5-(b5+c5);

}

int prime_factorize(int n,int n1)
{
    int no=0;
    while( n>0 && !(n%n1) )
    {
            n=n/n1;
            ++no;
    }
    return no;
}

int  main()
{
     int  i,test,n,m,p,q,zero;
    cin>>test;
    for(i=1;i<=test;i++)
    {
        cin>>m>>n>>p>>q;

        calculate_zero(m,n);

        no_of_2+=prime_factorize(p,2)*q;
        no_of_5+=prime_factorize(p,5)*q;

        if(no_of_2<no_of_5)
            zero=no_of_2;
        else
            zero=no_of_5;
        cout<<"Case "<<i<<": ";
        cout<<zero<<endl;

        no_of_2 = 0;
        no_of_5 = 0;
    }
    return 0;
}

সবশেষে তোমার জন্য রইল অনেক শুভকামনা । ভাল থেক সব-সময় ।

বুধবার, ৬ জুন, ২০১২

Solution Of Lifht Online Judge 1294 - Positive Negative Sign

            সমস্যাটি আসলেই খুব-ই সোজা । একবার ধরে ফেলতে পারলে মনে হবে এইরকম সমস্যাও ACM এ দেয় ? তাহলে চল দেখা যাক আসলেই কি জানতে চাওয়া হচ্ছে এই সমস্যায় ?

         চল আমরা প্রথম Input/output এর দিকে নজর দেই । যখন n=12 এবং m = 3 তখন সমস্যাটি অনেকটা হয়ে যায় এই রকমঃ
                    -1 -2 -3 +4 +5 +6 -7 -8 -9 +10 +11 +12 
     
       যার উত্তর হলঃ ১৮ ।  n এর মান ছোট তখন আমরা সহজেই এর মান বের করে ফেলতে পারি । কিন্তু n এর মান বড় হলে ?

    সময় এখন উপরের সমস্যাটিকে ভেঙ্গে দেখার ।

           -1 +4 = 3               -7 + 10 =3
           -2+ 5 = 3               -8 + 11 =3
            -3 + 6 =3              -9 + 12 =3

দেখতেই পাচ্ছ যোগ-বিয়োগের ফলে n সংখ্যক terms হয়ে গেছে n/2 সংখ্যক terms যাদের প্রত্যেকের মান হল m সুতরাং   যোগফল  =  m + m + m + .............................( n/2 সংখ্যক terms )
                               =  m * ( 1 + 1+ 1 + .................    ( n/2 সংখ্যক terms )   )
                               = m* ( n/2 )

কাজেই সমাধান হলঃ  result = m* ( n/2 ) .

             শেষে শুধুই একটি warning : result variable এর type long long রাখতে ভুল না  । শুভ কামনার সবটুকু তোমার জন্য রেখে আজকে না হয় এখানেই বিদায় নিলাম । ভাল থেক সবসময় ।   

বৃহস্পতিবার, ২৪ মে, ২০১২

Solution of Light Online Judge 1249 - Chocolate Thief

        Problem টির Category হল Beginers Level . So যারা programming এর জগতে উঁকি দিতে আরম্ভ করেছ তাদের জন্য এই সমস্যাটি অনায়াসে Solve করে ফেলার মত । চল দেখা যাক আসলে এই সমস্যাটিতে কি জানতে চাওয়া হচ্ছে ।
        প্রথমে আমরা নজর দিব Sample Input/Output এর দিকে । আমি যদি সবার চকলেট এর আয়তন বের করি তাহলে অনেকটা নিচের মত দেখাবে ।
      
                                                 চকলেটের আয়তন
     atq 3 4 3  -----   ৩৬       
     mun 10 4 1 -----   ৪০
     sam1 6 6 1 -----   ৩৬
     sam2 18 2 1 -----  ৩৬
     mub 1 36 1 ------  ৩৬
     tan 1 4 9  ------  ৩৬
     sha 4 3 3  ------  ৩৬
     di 3 12 1  ------  ৩৬
     nab 2 2 9  ------  ৩৬
     all 8 4 1  ------  ৩৬
     fah 3 2 6  ------  ৩৬

তাহলে  চকলেট এর আয়তন অনুযায়ী যদি আমি সবাইকে Sort করে ফেলি তবে তা অনেকটা নিচের মত দেখাবে ।

                                              চকলেটের আয়তন

     all 8 4 1  ------  ৩২
     atq 3 4 3  -----   ৩৬       
     sam1 6 6 1 -----   ৩৬
     sam2 18 2 1 -----  ৩৬
     mub 1 36 1 ------  ৩৬
     tan 1 4 9  ------  ৩৬
     sha 4 3 3  ------  ৩৬
     di 3 12 1  ------  ৩৬
     nab 2 2 9  ------  ৩৬
     fah 3 2 6  ------  ৩৬
    
mun 10 4 1 -----   ৪০

      এ হতে দেখা যাচ্ছে যে সর্বনিন্ম মান হল ৩২ এবং সর্বোচ্চ মান হল ৪০ । যেহেতু মান দুইতি সমান নয় কাজেই mun
চকলেট চুরি করেছে  all হতে ।

     এখনও যারা বুজতে পারনি সমস্যাটি কিভাবে সমাধান করবে জন্যই বলছি সমস্যাটিতে আসলে জানতে চাওয়া হচ্ছে একটি array এর
সর্বোচ্চ ও সর্বনিন্ম মান বের করতে হবে । যদি তারা সমান হয় তাহলে চুরি হয়নি । আর তা না হলে চুরি হয়েছে । Simple , তাই না ?

Code:

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

class People
{
public:
    int a;
    string nme;
    People(string a ,int n):nme(a),a(n){}
    People();
};

bool cmp(People A,People B)
{
    return A.a < B.a ;
}


int main()
{
    int j,n,t1,t2,t3,i,test;
    string a;
    vector<People> pp;
    cin>>test;
    for(i=1;i<=test;i++)
    {
        cin>>n;
        for(j=0;j<n;j++)
        {
            cin>>a>>t1>>t2>>t3;
            pp.push_back( People(a,t1*t2*t3) ) ;
        }
        sort(pp.begin(),pp.end(),cmp);
        if( pp[0].a == pp[pp.size()-1].a )
            cout<<"Case "<<i<<": no thief"<<endl;
        else
            cout<<"Case "<<i<<": "<<pp[0].nme<<" took chocolate from "<<pp[pp.size()-1].nme<<endl;
    }

    return 0;
}
     
    

শনিবার, ৫ মে, ২০১২

Solution of Light Online Judge - 1078 - Integer Divisibility

We will try the general condition of this problem . Let us consider that the problem description is as follows : " Given any integer 0 <= n <= 10000 not divisible by 2 or 5, some multiple of n is a number which in decimal notation is a sequence of 1's. How many digits are in the smallest such a multiple of n? "

This is a very easy but tricky problem. If you want to calculate the sequence of one by increasing ONE, then two thing can be happened.

            1) If your data type is even long double (in C it is the highest data type), it can not hold the sequence and because of overflow you will get WA.
             2) If your data type is string then you will get “time limit exceeded”. To avoid these problems we need to follow a trick here.


The Algorithm

input (it will be the input of the problem) 
N=1
one=1 (in this variable finally we get how many one will be in the sequence)

loop until we find the desired answer {
  if N < input
    append a ’1′.  (this the way -> N=N*10+1)
    one = one + 1
  k = N mod input  ( k is a variable )
  do this until k = 0, otherwise N = k and repeat everything
}

Example

Let the input is 3
At first N = 1 and one  = 1

Now,
3 | 1 | but here N < input so, append a ’1′
one = one + 1 = 2
3 | 11| 3
     9
   —–
     2
Now, k = 2, so, N=k;
again,
3 | 2 | but here N < input so append a ’1′
one = one + 1 = 3
3 | 21| 7
    21
  —–
     x

So the output is variable ‘one’ which contains the value 3

Basically, this problem is a reverse version of dividing a series of ’1′s with N. In the example above. The step by step of dividing “111″ with “3″ is like this:
3 |111| 37
    9   => the same ’9′ that we see above
  —–
    21
    21  => the same ’21′ that we see above
   —-
     x
So, basically we just simulate this division but without spanning out the actual string of ’1′s.


-----------------------------------------------------------------------

Now let us consider the solution procedure of this problem : 

Algorithm
------------
input description :
-----------------------
input (it will be the input of the problem) 
number =  2nd input

N=number 
one=1  (in this variable finally we get how many one will be in the sequence) 


while(1) {
  if N < input
    N=N*10+number
    one = one + 1
  k = N mod input  ( k is a variable )
  if( k == 0)

      terminate loop ;     
 otherwise 
        N = k and repeat this loop
}



Good Bye . Hope this procedure will help you in solving this problem . Have a better day . 

শনিবার, ২৮ এপ্রিল, ২০১২

Solution Of LIGHT ONLINE JUDGE 1259

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

#define MAX 10000000
#define MAX1 667743

bool a[MAX];
long int b[MAX1+1];
long int nprime;

void seieve()
{
    int imax,i,j=0,jmax;

    for(i=0;i<=MAX;i++)
        a[i]=true;
     imax=sqrt(MAX)+1;
    for(i=2;i<=imax;i++)
    {
        jmax=(MAX/i)+1;
        if(a[i]==1)
            for(j=2;j<=jmax;j++)
                if(i*j<=MAX)
                    a[i*j]=false;
    }

    a[0]=0;
    a[1]=0;

    for(i=2;i<=MAX;i++)
        if( a[i] )
            b[j++]=i;
    nprime=j;
}

int main()
{
    int i,j,test,k;
    long int count=0,n;
    seieve();
    cin>>test;

    for(j=1;j<=test;j++){
        cin>>n;

        for(i=0;i<nprime && n/2>=b[i] ;i++)
            if( a[n-b[i] ] )
                count++;


      printf("Case %d: %ld\n",j,count);
      count=0;
    }
    return 0;
}

রবিবার, ২২ এপ্রিল, ২০১২

light Online Judge 1006

Problem Link : http://lightoj.com/volume_showproblem.php?problem=1006

      The concept is just like Fibonacci series except that it has six terms. Convert the recursion method into iterative method and it will work. Now the problem lies with the overflow.
 
     The first 6 numbers are given . You have to find nth term of this series . Simple a loop is enough for this .      

      Use Modular arithmatic for intermdeiate calculation . The modular arithmetic says
                   i) (A + B ) % M = ( (A% M) + (B % M) ) % M.
                   ii) (A x B ) % M = ( (A% M) x (B % M) ) % M.
                

    Use long long int for data type . Hope it helps

Code :
 #include<iostream>
#include<cstdio>
using namespace std;

int main() {
    long long int a,i,fn[10001],n,j, cases;
    cin>>cases;

    for(j=1;j<=cases;j++){
        cin>>a>>b>>c>>d>>e>>f>>n;

        fn[0]= a ;
        fn[1]= b ;
        fn[2]= c ;
        fn[3]= d ;
        fn[4]= e ;
        fn[5]= f ;

        for(i=6;i<=n;i++)
                fn[i]=( fn[i-1] + fn[i-2] + fn[i-3] + fn[i-4] + fn[i-5] + fn[i-6] )  % 10000007 ;

        printf("Case %lld: ", j);
        cout<<fn[n]% 10000007<<endl;
    }
    return 0;
}

Solution Of Light Online Judge 1166

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

int main()
{
    int temp,count=0,i,n,j,k,test,array[101];
    cin>>test;

     for(i=1;i<=test;i++)
     {
         cin>>n;
         for(j=1;j<=n;j++)
             cin>>array[j];

         for(k=1;k<=n;k++)
         {
             if( array[k]!=k )
                 for(j=k+1;j<=n;j++)
                 {
                     if( array[j] == k )
                     {
                         temp=array[j];
                         array[j]=array[k];
                         array[k]=temp;

                         ++count;
                         break;
                     }
                 }
         }

         printf("Case %d: %d\n",i,count);
         count=0;
     }

    return 0;
}