Saturday, April 23, 2016

SPOJ Problem ESYRCRTN

EASY RECURSION

Explanation:

Problem requires to find out the sum of F(1) + F(2) + ... + F(n). Before you go for the solution, I would suggest you to find out the values from 1 to 12, and you will have the result.

Solution:

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

typedef long long int LLD;

int main(){
    LLD t, n;
    int arr[] = {1, 4, 6, 5, 2, 0};
    scanf("%lld", &t);
    while(t--){
        scanf("%lld", &n);
        printf("%d\n", arr[(n-1) % 6]);
    }
    return 0;
}

Any suggestions are welcome.

Friday, April 22, 2016

SPOJ Problem DOL

LARGEST ODD DIVISOR

Explanation:

Simple problem. Requires just pen and paper to come up with a solution, or not even that. Coming to the point though. For an odd number 'N', answer will be 'N'. Now, for an even integer, we simply need to keep on dividing by 2 until the quotient is even, and thus, removing the even factors from the number.

Solution:

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

typedef long long int LLD;

LLD findLargestOddDivisor(LLD n){
    while(n % 2 == 0){
        n >>= 1;
    }
    return n;
}

int main() {
    LLD t, n, cas=0;
    scanf("%lld", &t);
    while(t--){
        scanf("%lld", &n);
        printf("Case %lld: %lld\n", ++cas, findLargestOddDivisor(n));
    }
    return 0;
}

Any suggestions are welcome.

SPOJ Problem ONEZERO

Ones and zeros Explanation: A very good problem to practice BFS skills. I think that much hint is good to give the problem a shot. An...