Friday, August 22, 2014

TC SRM 630 div2 250 pt - Double Letter solution

TC SRM 630 div2 250 pt - Double Letter solution: 


#include <iostream>
#include <string>

class DoubleLetter {
    public: std::string ableToSolve(std::string s) {
        int flag=0, cnt=0;
        while(!flag) {
            for(int i=1; i<s.length(); i++) if(s[i-1]==s[i]) s.erase(i-1, 2);
            cnt++;
            if(!s.length()) flag=1;
            if(s.length()%2 || cnt==100) break;
        }
        if(flag) return "Possible";
        else return "Impossible";
    }
};

Monday, August 18, 2014

Hackerrank Ad Infinitum August 2014 - Stepping Stones Game solution

Hackerrank Ad Infinitum August 2014 - Stepping Stones Game: https://www.hackerrank.com/contests/infinitum-aug14/challenges/stepping-stones-game

Hackerrank Ad Infinitum August 2014 - Stepping Stones Game editorial: https://www.hackerrank.com/contests/infinitum-aug14/challenges/stepping-stones-game/editorial

Hackerrank Ad Infinitum August 2014 - Stepping Stones Game solution: 


#include <iostream>
#include <cstdio>
#include <cmath>

int main() {
    int t;
    long long int n, a;
    scanf("%d", &t);
    while(t--) {
        scanf("%lld", &n);
        if(floor(sqrt(1+8*n))==sqrt(1+8*n)) a=(sqrt(1+8*n)-1)/2, printf("Go On Bob %lld\n", a);
        else printf("Better Luck Next Time\n");
    }
    return 0;
}

note: The formular for the sum of the first 
n natural numbers is
n(n+1)2
A number x appears in this sequence, iff
n(n+1)2=xn2+n2x=0
has an non-negative integer solution. Since, this is a quadratic equation, you can simply compute the positive solution:
n=1+8x12
This is an integer number, iff 1+8x is a square and 21+8x1. You can see, that the latter is always true, if the former is true. So the answer is:
x occurs in this sequence, if 1+8x is a perfect square.

ref: http://math.stackexchange.com/questions/466649/how-do-i-test-if-a-number-x-is-a-sum-of-consecutive-natural-numbers

Hackerrank Ad Infinitum August 2014 - Reverse Game solution

Hackerrank Ad Infinitum August 2014 - Reverse Game: https://www.hackerrank.com/contests/infinitum-aug14/challenges/reverse-game

Hackerrank Ad Infinitum August 2014 - Reverse Game editorial: https://www.hackerrank.com/contests/infinitum-aug14/challenges/reverse-game/editorial

Hackerrank Ad Infinitum August 2014 - Reverse Game solution: 


#include <iostream>
#include <cstdio>

int main() {
    int n, k, t, cnt, a;
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d", &n, &k);
        if(k<(n-1)/2) {
            cnt=0, a=2;
            while(cnt<k) a+=2, cnt++;
        }
        else {
            cnt=n-1, a=1;
            while(cnt>k) a+=2, cnt--;
        }
        printf("%d\n", a-1);
    }
    return 0;
}

Friday, August 15, 2014

Hackerrank Weekly - Week 8 - Counter Game solution

Hackerrank Weekly - Week 8 - Counter Game: https://www.hackerrank.com/contests/w8/challenges/counter-game

Hackerrank Weekly - Week 8 - Counter Game editorial: https://www.hackerrank.com/contests/w8/challenges/counter-game/editorial

Hackerrank Weekly - Week 8 - Counter Game solution: 


#include <iostream>
#include <cstdio>

int main() {
    long long unsigned int n, tmp;
    int cnt, t;
    scanf("%d", &t);
    while(t--) {
        scanf("%llu", &n), tmp=n;
        cnt=0;
        while(tmp) tmp&=tmp-1, cnt++;
        cnt--;
        while((n&1)==0) n>>=1, cnt++;
        if(cnt%2==0) printf("Richard\n");
        else printf("Louise\n");
    }
    return 0;
}

and my stackoverflow question to the difference between ampersand and equal to signs in c++  & and == operators: http://stackoverflow.com/questions/25256749/difference-between-n1-and-n1

TCHS SRM 49 - Level one - 250 pt Deposit Profit solution

TCHS SRM 49 - Level one - 250 pt Deposit Profit: http://community.topcoder.com/stat?c=problem_statement&pm=8313&rd=10809

TCHS SRM 49 - Level one - 250 pt Deposit Profit editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm49

TCHS SRM 49 - Level one - 250 pt Deposit Profit solution:


#include <iostream>

class DepositProfit{
    public: int depositTerm(int a, int b, int p) {
        int cnt, st=a*100, m=0;
        a*=100, p*=100;
        while(cnt<p) a+=a*b/1200, cnt=a-st, m++;
        return m;
    }
};

Monday, August 11, 2014

Hackerrank Weekly challenges Week 8 - John and GCD list solution

Hackerrank Weekly challenges Week 8 - John and GCD list: https://www.hackerrank.com/contests/w8/challenges/john-and-gcd-list

Hackerrank Weekly challenges Week 8 - John and GCD list solution: http://ideone.com/QZZxNC


#include <iostream>
#include <cstdio>

int gcd(int u, int v) {
    if(u==v) return u;
    if(u==0) return v;
    if(v==0) return u;
    if(~u&1) {
        if(v&1) return gcd(u>>1, v);
        return gcd(u>>1, v>>1)<<1;
    }
    if(~v&1) return gcd(u, v>>1);
    if(u>v) return gcd((u-v)>>1, v);
    return gcd((v-u)>>1, u);
}

int lcm(int u, int v) {
    return u*v/gcd(u, v);
}

int main() {
    int t, n, a[1005], b[1005];
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        for(int i=0; i<n; i++) scanf("%d", &a[i]);
        b[0]=a[0];
        for(int i=1; i<n; i++) b[i]=lcm(a[i-1], a[i]);
        b[n]=a[n-1];
        for(int i=0; i<=n; i++) printf("%d ", b[i]);
        printf("\n");
    }
    return 0;
}


Sunday, August 10, 2014

usaco Broken Necklace solution

usaco Broken Necklace:

Broken Necklace

You have a necklace of N red, white, or blue beads (3<=N<=350) some of which are red, others blue, and others white, arranged at random. Here are two examples for n=29:
                1 2                               1 2
            r b b r                           b r r b
          r         b                       b         b
         r           r                     b           r
        r             r                   w             r
       b               r                 w               w
      b                 b               r                 r
      b                 b               b                 b
      b                 b               r                 b
       r               r                 b               r
        b             r                   r             r
         b           r                     r           r
           r       r                         r       b
             r b r                             r r w
            Figure A                         Figure B
                        r red bead
                        b blue bead
                        w white bead
The beads considered first and second in the text that follows have been marked in the picture.
The configuration in Figure A may be represented as a string of b's and r's, where b represents a blue bead and r represents a red one, as follows: brbrrrbbbrrrrrbrrbbrbbbbrrrrb .
Suppose you are to break the necklace at some point, lay it out straight, and then collect beads of the same color from one end until you reach a bead of a different color, and do the same for the other end (which might not be of the same color as the beads collected before this).
Determine the point where the necklace should be broken so that the most number of beads can be collected.

Example

For example, for the necklace in Figure A, 8 beads can be collected, with the breaking point either between bead 9 and bead 10 or else between bead 24 and bead 25.
In some necklaces, white beads had been included as shown in Figure B above. When collecting beads, a white bead that is encountered may be treated as either red or blue and then painted with the desired color. The string that represents this configuration can include any of the three symbols r, b and w.
Write a program to determine the largest number of beads that can be collected from a supplied necklace.

PROGRAM NAME: beads

INPUT FORMAT

Line 1:N, the number of beads
Line 2:a string of N characters, each of which is r, b, or w

SAMPLE INPUT (file beads.in)

29
wwwbbrwrbrbrrbrbrwrwwrbwrwrrb

OUTPUT FORMAT

A single line containing the maximum of number of beads that can be collected from the supplied necklace.

SAMPLE OUTPUT (file beads.out)

11

OUTPUT EXPLANATION

Consider two copies of the beads (kind of like being able to runaround the ends). The string of 11 is marked.
                Two necklace copies joined here
                             v
wwwbbrwrbrbrrbrbrwrwwrbwrwrrb|wwwbbrwrbrbrrbrbrwrwwrbwrwrrb
                       ******|*****
                       rrrrrb bbbbb  <-- assignments
                       rrrrr#bbbbbb  
                       5 x r  6 x b  <-- 11 total

usaco Broken Necklace solution: http://ideone.com/zdNlhI

/*
ID: [your ID here]
LANG: C++
PROB: beads
*/
#include <iostream>
#include <cstdio>
#include <algorithm>

int n;
char s[355];

int md(int j) {
    while(j<0) j+=n;
    return j%n;
}

int bp(int pos, int dir) {
    int i, j;
    char col='w';
    if(dir==1) j=pos;
    else j=pos-1;
    for(i=0; i<n; i++, j=md(j+dir)) {
        if(col=='w' && s[j]!='w') col=s[j];
        if(col!='w' && s[j]!='w' && s[j]!=col) break;
    }
    return i;
}

int main() {
    freopen("beads.in", "r", stdin);
    freopen("beads.out", "w", stdout);
    int ma=0;
    scanf("%d", &n);
    scanf("%s", s);
    for(int i=0; i<n; i++) ma=std::max(bp(i, -1)+bp(i, 1), ma);
    if(ma>n) ma=n;
    printf("%d\n", ma);
    return 0;
}


alternative solutions for usaco Broken Necklace: http://usacotraining.blogspot.com.tr/2013/09/problem-114-broken-necklace.html