Saturday, November 26, 2011

[UVa] 11945 - Financial Management

Too simple. It doesn't even need a search :P. But while solving this I learned of using C Locales. Look at how I printed the value. Didn't even had to put an if for the commas.
/* Faith-M */

//Headers
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <climits>
#include <clocale>
//Defines
#define pow2(i) (1<<i)
#define bit(i) (1<<i)
#define isOdd(i) (i&1)
#define isEven(i) (!(i&1))
#define isPrime(i) ((i==2) || ((i&1) && !pTest[i])) //pTest has to be the bool array's name
#define sz(i) i.size()
#define vec(type,name) vector< type > name
#define rep(i,a,b) for(int i=a ; i<=b ; i++)
#define swap(type,a,b) {type t=a; a=b; b=t;}
#define sum(a,n) ( (n*(n+1)/2) - (a-1)*a/2 )
#define iscap(i) (i>='A'&&i<='Z')
#define issmall(i) (i>='a'&&i<='z')
#define isnum(i) (i>='0'&&i<='9')
#define issymbol(i) (!(i>='a'&&i<='z') && !(i>='A'&&i<='Z') && !(i>='0'&&i<='9'))
#define mk(i,j) make_pair(i,j)
#define ERROR 1e-11
//Type Defs
typedef long long lint;
typedef unsigned long long ulint;
typedef long double ldouble;

using namespace std;

int main()
{
 /*freopen("input.txt","r+",stdin);
 freopen("output.txt","w+",stdout);/**/

 //    TEST CASE     //
 int kase=1, kounter=1;/**/
 int i;
 double sum, blnc;

 scanf("%d",&kase);
    setlocale(LC_ALL, "en_US.UTF-8");

 while (kase--)
 {
     for (i=1, sum=0 ; i<=12 ; i++)
     {
         scanf("%lf",&blnc);
         sum += blnc;
     }
        printf("%d $%'.2lf\n", kounter++, sum / 12.0);

 }
 return 0;
}

Tuesday, November 22, 2011

[UVa] 11614 - Etruscan Warriors Never Play Chess

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    long long n, lim, x1, x2;

    int tst;
    cin >> tst;

    while (tst--)
    {

        cin >> n;
        lim = (int)(ceil(sqrt(double(n*2))));

        x1 = lim * (lim+1) / 2;
        x2 = (lim-1)*lim/2;



        if (x1 <= n)
        {
            cout << lim << endl;
        } else if (x2<=n)
        {
            cout << lim-1 << endl;
        } else
        {
            cout << lim-2 << endl;
        }

    }
    return 0;
}

[UVa] 12364 - In Braile

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>

using namespace std;

class nmb
{
    public:
    string br;
    int ser;
};

nmb nums[10] = { {".***..",0},{"*.....",1},{"*.*...",2},{"**....",3},{"**.*..",4},{"*..*..",5},{"***...",6},{"****..",7},{"*.**..",8},{".**...",9} };

int main()
{
    int n, i, j, k, tst;
    string str, temp;
    string strs[3];


    while (getline(cin,str))
    {
        if (str == "0")
            break;


        getline(cin,str);

        if (str == "S")
        {
            getline(cin,str);
            for (i=0 ; i<3 ; i++)
            {

                for (j=0 ; j<str.size() ; j++)
                    if (!j) cout << nums[str[j]-'0'].br.substr(i*2,2);
                    else cout << " " << nums[str[j]-'0'].br.substr(i*2,2);
                cout << endl;
            }
        }
        else
        {

            getline(cin,strs[0]);
            getline(cin,strs[1]);
            getline(cin,strs[2]);

            for (j=0 ; j<strs[0].size() ; j+=3)
            {
                temp = "";
                for (i=0 ; i<3 ; i++)
                {
                    temp += strs[i].substr( j, 2 );
                }
                for (k=0 ; k<10 ; k++)
                {
                    if (temp == nums[k].br)
                    {
                        cout << k;
                        break;
                    }
                }
            }
            cout << endl;

        }
    }
    return 0;
}

[UVa] 12036 - Stable Grid

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int n, i, j, t, tst, kase=1;

    scanf("%d",&tst);

    while ( tst-- )
    {
        scanf("%d",&n);

        int count[200]={0};
        bool valid = true;
        for (i=1 ; i<=n ; i++)
        {
            for (j=1 ; j<=n ; j++)
            {
                scanf("%d",&t);
                count[t]++;
                if (count[t]>n)
                {
                    valid=false;
                }
            }
        }
        if (valid) printf("Case %d: yes\n",kase++);
        else printf("Case %d: no\n",kase++);

    }
    return 0;
}

Sunday, November 20, 2011

[UVa] 11679 - Sub Prime

At a first glance, in particular after reading the name, at least I thought it's about Primes :P. It's pure ad hoc, simulation.

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>

using namespace std;

class bank
{
    public:
    int tot_money;
    vector< pair<int,int> > dList;
    void init() {tot_money=0; dList.clear();}
};

bank banks[30];

int main()
{

    //freopen("in.txt","r+",stdin);
    //freopen("out.txt","w+",stdout);


    int b, n, d, c, m, i, j, increase, decrease;
    bool valid;

    while (scanf("%d %d",&b,&n)==2)
    {
        if (!b && !n)
            break;
// Input Start
        for (i=1 ; i<=b ; i++)
        {
            banks[i].init();
            scanf("%d",&banks[i].tot_money);

        }

        for (i=1 ; i<=n ; i++)
        {
            scanf("%d %d %d",&d,&c,&m);

            banks[d].dList.push_back( make_pair(c,m) );
        }
// Input End


// Process Start
        for (i=1 ; i<=b ; i++)
        {
            for (j=0 ; j<banks[i].dList.size() ; j++)
            {
                increase = banks[i].dList[j].first;
                decrease = banks[i].dList[j].second;

                banks[increase].tot_money+=decrease;
                banks[i].tot_money-=decrease;
            }
        }


        for (i=1, valid=true ; i<=b ; i++)
        {
            if (banks[i].tot_money<0)
            {
                valid = false;
                break;
            }
        }

        if (valid) printf("S\n");
        else printf("N\n");

// Process End

    }
    return 0;
}

Wednesday, November 16, 2011

[UVa LIVE] 4493 - This is your queue

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>

using namespace std;

deque<int> M;
stack<int> Q;
int main()
{
    int P, C, i, SER, print, m, t, kase=1;
    char CHR[10];
    while (scanf("%d %d",&P,&C)==2)
    {
        if (!P && !C)
            break;
        printf("Case %d:\n",kase++);


        //getchar();
        //cout << "---> " << endl;
        M.clear();
        m = min(P,1000);
        for (i=1 ; i<=m ; i++)
        {
            M.push_back(i);
        }
        for (i=1 ; i<=C ; i++)
        {
            t = scanf("%s",CHR);

            if (!strcmp(CHR,"N"))
            {
                print = M.front();
                M.pop_front();
                printf("%d\n",print);
                M.push_back(print);
            } else if (!strcmp(CHR,"E"))
            {
                scanf("%d",&SER);
                while (!Q.empty()) Q.pop();
                //cout << "------------------------" << endl;
                //printf("-> %d\n",SER);
                while (!M.empty() && M.front() != SER)
                {
                    //cout << M.front() << endl;
                    Q.push(M.front());
                    M.pop_front();
                }
                if(!M.empty()) M.pop_front();
                //cout << "------------------------" << endl;
                while (!Q.empty())
                {
                    M.push_front(Q.top());
                    Q.pop();
                }
                M.push_front(SER);
            }
        }
    }
    return 0;
}

Friday, November 11, 2011

[UVa] 532 - Dungeon Master

With this, I get inside the first 1000 coders on UVa. :)
Submission details:
Submission no: 9461491
Problem no: 532 
Problem title: Dungeon Master
Status: Accepted
Language: C++
Runtime: 0.012
Date of submission: 2011-11-11
Time of submission: 11:12:39

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define INF 9999999
using namespace std;

typedef struct xp
{
    int l, x, y;
} cord;

bool color[100][100][100];
int d[100][100][100];
char inp[100][100][100];

int dirs[10][4]={ {-1,0,0}, {1,0,0}, {0,-1,0}, {0,0,-1}, {0,0,1}, {0,1,0} };

int bfs(cord S, cord E, cord M)
{
    int i, j, k;

    for (i=0 ; i<=M.l+1 ; i++)
    {
        for (j=0 ; j<=M.x+1 ; j++)
        {
            for (k=0 ; k<=M.y+1 ; k++)
            {
                color[i][j][k]=false;
                d[i][j][k]=INF;
            }
        }
    }
    color[S.l][S.x][S.y]=true;
    d[S.l][S.x][S.y]=0;

    cord u, temp;

    queue<cord> q;
    q.push(S);
    while (!q.empty())
    {
        u = q.front();
        q.pop();

        for (i=0 ; i<6 ; i++)
        {
            temp.l = u.l+dirs[i][0];
            temp.x = u.x+dirs[i][1];
            temp.y = u.y+dirs[i][2];

            if (!color[temp.l][temp.x][temp.y] && inp[temp.l][temp.x][temp.y]!='#')
            {
                if (temp.l<1||temp.l>M.l||temp.x<1||temp.x>M.x||temp.y<1||temp.y>M.y)
                    continue;
                color[temp.l][temp.x][temp.y]=true;
                d[temp.l][temp.x][temp.y]=d[u.l][u.x][u.y]+1;
                if (i==E.l && j==E.x && k==E.y) return d[temp.l][temp.x][temp.y];
                    q.push(temp);
            }
        }
    }

    return d[E.l][E.x][E.y];
}


int main()
{
    int l, w, h, i, j, k, res;
    cord S, E, M;
    while (scanf("%d %d %d",&l,&h,&w)==3 && l&&h&&w)
    {
        getchar();

        M.l = l;
        M.x = h;
        M.y = w;

        for (i=1 ; i<=l ; i++)
        {
            for (j=1 ; j<=h ; j++)
            {
                gets(&inp[i][j][1]);
                for (k=1 ; k<=w ; k++)
                {
                    if (inp[i][j][k]=='S')
                    {
                        S.l = i;
                        S.x = j;
                        S.y = k;
                    }
                    if (inp[i][j][k]=='E')
                    {
                        E.l = i;
                        E.x = j;
                        E.y = k;
                    }
                }
            }
            gets(&inp[i][j][1]);
        }

        res = bfs(S,E,M);

        if (res==INF)
            printf("Trapped!\n");
        else
            printf("Escaped in %d minute(s).\n",res);
    }
    return 0;
}

[UVa] 167 - The Sultan's Successors

One thing's for sure, Backtracking is damn fun!!! :D
I liked this one a lot. Though I had to take some help for this one. But it still increased a lot of knowledge about Backtracking.
#include <cstdio>
#include <iostream>
#include <iomanip>
using namespace std;

bool col[100], d1[100], d2[100];

class c
{
    public:
    int x, y;
};

c crd[100];

int knt=0, board[100][100];

c lst[1000];

int qplace(int n)
{
    if (n>8)
    {
        for (int i=1 ; i<=8 ; i++)
        {
            lst[knt++]=crd[i];
        }
        return 0;
    }
    for (int k=1 ; k<=8 ; k++)
    {
        if (!col[k] && !d1[n+k] && !d2[n-k+8])
        {
            col[k]=d1[n+k]=d2[n-k+8]=true;
            crd[n].x = n;
            crd[n].y = k;
            qplace(n+1);
            col[k]=d1[n+k]=d2[n-k+8]=false;
        }
    }
    return 0;
}

int main()
{
    int test, i, j, sum, max;
    knt = 0;
    for (i=0 ; i<=100 ; i++)
        col[i]=d1[i]=d2[i]=false;
    qplace(1);
    //cout << knt << endl;



    scanf("%d",&test);
    while (test--)
    {
        for (i=1 ; i<=8 ; i++)
        {
            for (j=1 ; j<=8 ; j++)
            {
                scanf("%d",&board[i][j]);
            }
        }
        max = 0;
        for (i=0 ; i<knt ; i+=8)
        {
            for (j=i, sum=0 ; (j-i)<8 ; j++)
            {
                sum += board[lst[j].x][lst[j].y];
            }
            if (sum>max)
                max = sum;
        }
        cout << setw(5) << max << endl;
    }
    return 0;
}

Thursday, November 10, 2011

[UVa] 12114 - Bachelor Arithmetic

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define ERROR 1e-11
using namespace std;
typedef double dt;

dt min(dt a, dt b)
{
    return (a<b?a:b);
}

int main()
{
    dt s, b, nx_pro, pr_pro;
    int kase=1;

    while (cin >> b >> s)
    {
        if (fabs(b-0.00)<ERROR && fabs(s-0.00)<ERROR) break;

        cout << "Case " << kase++ << ": ";
        pr_pro = min(s/b,(dt)1);
        if (b==1.00)
        {
            cout << ":-\\" << endl;
            continue;
        }
        nx_pro = min((s-(dt)1)/(b-(dt)1),(dt)1);

        if (nx_pro>pr_pro) cout << ":-)" << endl;
        else if (nx_pro<pr_pro) cout << ":-(" << endl;
        else cout << ":-|" << endl;
    }

    return 0;
}

[UVa] 11958 - Coming Home

#include <stdio.h>

int main()
{
    int test, bn, ch, cm, ah, am, dur, i, cur_time, arr_time, min_time, spend, kase=1;
    scanf("%d",&test);

    while (test--)
    {
        min_time = 1000000;

        scanf("%d %d:%d",&bn,&ch,&cm);

        cur_time = ch*60 + cm;

        for (i=0 ; i<bn ; i++)
        {
            scanf("%d:%d %d",&ah,&am,&dur);

            arr_time = ah*60 + am;

            if (arr_time<cur_time) spend = 1440 - cur_time + arr_time + dur;
            else spend = arr_time - cur_time + dur;

            if (min_time>spend) min_time = spend;
        }

        printf("Case %d: %d\n",kase++,min_time);
    }

    return 0;
}

[UVa] 10948 - The Primary Problem

#include <cstdio>
#include <iostream>
using namespace std;
bool ver[10000500];
int sieve()
{
    int i, j, k=0;

    //lst[k++]=2;
    for (i=3 ; i<=10000000 ; i+=2)
    {
        if (!ver[i])
        {
            //lst[k++]=i;
            for (j=3 ; i*j<=10000000 ; j+=2)
            {
                ver[i*j]=true;
            }
        }
    }
    //lst[0]=2;

    return k;
}

int main()
{
    sieve();
    int n, i;
    bool found;

    while (std::cin >> n && n)
    {
        std::cout << n << ":" << std::endl;

        if ((((n-2)&1) && !ver[n-2]) || (n-2 == 2))
        {
            std::cout << "2+" << n-2 << std::endl;
            continue;
        }
        found = false;
        for (i=3 ; i<=((n+1)/2) ; i+=2)
        {
            if (!ver[i] && !ver[n-i] && (n-i)&1)
            {
                std::cout << i << "+" << n-i << std::endl;
                found = true;
                break;
            }
        }
        if (!found) std::cout << "NO WAY!" << std::endl;
    }
    return 0;
}

[UVa] 10391 - Compound Words

#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
vector<string> words;

class kmap
{
    public:
    kmap (const int &v):defVal (v)
    {
    }
    int &getVal(const string &k)
    {
        if (m.find(k) == m.end())
            return defVal;
        else
            return m[k];
    }
    int &operator[] (const string &k)
    {
        return m[k];
    }
    std::map<string,int> m;
    int defVal;
};

kmap ver(-1);

int main()
{
    string word, s1, s2;
    int i, j;

    i=0;
    while (cin >> word)
    {
        words.push_back(word);
        ver[word]=i++;
    }
    int list_len = words.size();

    for (i=0 ; i<list_len ; i++)
    {
        int wlen = words[i].length();
        //cout << words[i] << endl;
        for (j=1 ; j<wlen ; j++)
        {
            s1 = words[i].substr(0,j);
            s2 = words[i].substr(j,wlen);

            //cout << "\t" << ver[s1] << "--" << ver[s2] << endl;

            if (ver.getVal(s1)!=-1 && ver.getVal(s2)!=-1 && ver[s1]!=i && ver[s2]!=i)
            {
                cout << words[i] << endl;
                break;
            }
        }
    }
    return 0;
}

[UVa] 1230 - MODEX

#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;
typedef long long ll;

ll sqr(ll v)
{
    return v*v;
}

ll bigMod(ll p, ll v, ll m)
{
    if (p==0) return 1;
    else if (p&1) return (v%m * bigMod(p-1,v,m))%m;
    else return (sqr(bigMod(p/2,v,m)))%m;
}

int main()
{
    int tst;
    ll x, y, n;

    cin >> tst;

    while (tst--)
    {
        cin >> x >> y >> n;
        cout << bigMod(y,x,n) << endl;
    }

    cin >> tst;

    return 0;
}

[UVa] 784 - Maze Exploration

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

int dirs[10][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};

char maze[100][100];
int color[100][100];

void dfs_visit(int x, int y)
{
    int i;
    color[x][y]=2;
    maze[x][y]='#';
    for (i=0 ; i<8 ; i++)
    {
        if (color[x+dirs[i][0]][y+dirs[i][1]]==0 && maze[x+dirs[i][0]][y+dirs[i][1]]!='X' && maze[x+dirs[i][0]][y+dirs[i][1]]!='#')
            dfs_visit(x+dirs[i][0],y+dirs[i][1]);
    }
}

void dfs(int x, int y, int mh, int mw)
{
    int i, j;
    for (i=0 ; i<mh ; i++)
        for (j=0 ; j<mw ; j++)
            color[i][j] = 0;

    for (i=0 ; i<8 ; i++)
    {
        if (color[x+dirs[i][0]][y+dirs[i][1]]==0 && maze[x+dirs[i][0]][y+dirs[i][1]]!='X' && maze[x+dirs[i][0]][y+dirs[i][1]]!='#')
            dfs_visit(x+dirs[i][0],y+dirs[i][1]);
    }
}


int end(char *p)
{
    int i;
    for (i=0 ; p[i] ; i++)
    {
        if (p[i]!='_') return 0;
    }
    return 1;
}
int seed(char *p)
{
    int i;
    for (i=0 ; p[i] ; i++)
    {
        if (p[i]=='*') return i;
    }
    return -1;
}
int main()
{
    int test, maze_size, i, sx, sy;

    scanf("%d",&test);
    getchar();
    while (test--)
    {
        for (i=0 ;  ; i++)
        {
            gets(maze[i]);

            if (end(maze[i]))
                break;
        }

        maze_size = i;

        for (i=0 ; i<maze_size ; i++)
        {
            if ((sy = seed(maze[i]))>=0)
            {
                sx = i;
                break;
            }
        }

        dfs(sx,sy,maze_size,80);

        for (i=0 ; i<=maze_size ; i++)
            puts(maze[i]);

    }
    return 0;
}

[UVa] 568 - Just the Facts

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

int fact[20000];

int main()
{
    int i, f, idx;
    fact[0] = fact[1] = 1;
    f = 1;

    for (i=2 ; i<=10000 ; i++)
    {
        f*=i;
        while (f%10 == 0)
            f/=10;

        f = f%100000;
        fact[i] = f%10;
    }

    while (scanf("%d",&idx)==1)
    {
        printf("%5d -> %d\n",idx,fact[idx]);
    }

    return 0;
}

[UVa] 439 - Knight Moves

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <stack>
#include <queue>
#include <algorithm>

#define INF 9999999
#define MAXX 9
#define MAXW 9
using namespace std;
typedef struct v
{
    int row, col;
} vtx;
vtx moves[10]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};

char inp[1000];

bool color[10][10];
int d[10][10];
int bfs(int srow, int scol, int drow, int dcol)
{
    int i, j;
    for (i=0 ; i<=MAXX ; i++)
        for (j=0 ; j<=MAXW ; j++)
        {
            color[i][j]=false;
            d[i][j]=INF;
        }

    color[srow][scol] = true;
    d[srow][scol] = 0;
    queue<vtx> myq;
    vtx u, t, s;
    s.row=srow;
    s.col=scol;

    myq.push(s);
    while (!myq.empty())
    {
        u = myq.front();
        myq.pop();
        for (i=0 ; i<8; i++)
        {
            t.row = u.row+moves[i].row;
            t.col = u.col+moves[i].col;
            if (t.row>=1 && t.row<MAXX && t.col>=1 && t.col<MAXW && color[t.row][t.col]==false)
            {
                //cout << "-> " << t.row << "  " << t.col << endl;
                //getchar();
                color[t.row][t.col]=true;
                d[t.row][t.col]=d[u.row][u.col]+1;
                if (t.row == drow && t.col == dcol) return d[t.row][t.col];
                myq.push(t);
            }
            //cout << "--> " << "lOOPing" << endl;
        }
        //if (myq.empty()) return d[drow][dcol];
        //myq.pop();
    }

    return d[drow][dcol];
}

int main()
{
    int scol, srow, dcol, drow;
    char sc,dc;
    while (gets(inp))
    {
        sscanf(inp,"%c%d %c%d",&sc,&srow,&dc,&drow);
        scol = sc-'a'+1;
        dcol = dc-'a'+1;
        srow; drow;

        //printf("%d %d %d %d\n",srow,scol,drow,dcol);
        //getchar();

        printf("To get from %c%d to %c%d takes %d knight moves.\n",sc,srow,dc,drow,bfs(srow,scol,drow,dcol));
    }
    return 0;
}

[UVa] 195 - Anagrams

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;

char in[1000];
int cmp(const void *a, const void*b)
{
    return *(char*)a-*(char*)b;
}

bool comp(const char &a, const char &b)
{
    int delta = tolower(a) - tolower(b);
    return delta?delta<0:a<b;
}
int main()
{
    int test, len;
    scanf("%d",&test);
    getchar();
    while (test--)
    {
        gets(in);
        len = strlen(in);
        sort(in,in+len,comp);

        do
        {
            cout << in << endl;
        } while (next_permutation(in,in+len,comp));
    }
    return 0;
}

[UVa] 147 - Dollars

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

typedef long long ll;


ll table[30000][100];

ll change(ll n, ll m, int *S)
{

    if (n==0) return 1;
    else if (n<0) return 0;
    else if (m<0 && n>=1) return 0;

    //cout << n << " -- " << table[n][m]<< endl;
    //getchar();

    if (table[n][m]!=-1) return table[n][m];

    return (table[n][m] = change(n,m-1,S)+change(n-S[m],m,S));
}

ll parse(string n)
{
    int len = n.length(), i, j;
    char stor[100];
    for (i=0, j=0 ; i<len ; i++)
    {
        if (n[i]>='0' && n[i]<='9')
            stor[j++]=n[i];
    }
    stor[j]='\0';
    return (ll)atol(stor);
}


int main()
{
    int i, j;
    ll n, m, val;
    string inp;

    int S[100];

    for (i=0 ; i<=30000 ; i++)
    {
        for (j=0 ; j<=20 ; j++)
        {
            table[i][j]=-1;
        }
    }

    S[0] = 5;
    S[1] = 10;
    S[2] = 20;
    S[3] = 50;
    S[4] = 100;
    S[5] = 200;
    S[6] = 500;
    S[7] = 1000;
    S[8] = 2000;
    S[9] = 5000;
    S[10] = 10000;

    for (i=0 ; i<=30000 ; i++)
    {
        for (j=0 ; j<=20 ; j++)
        {
            table[i][j]=-1;
        }
    }

    while (cin >> inp)
    {
        if (inp=="0.00") break;

        n = parse(inp);
        val = change(n,10,S);
        cout.width(6);
        cout << inp;
        cout.width(17);
        cout << val << endl;
    }
    return 0;
}

[UVa] 10161 - Ant on a Chess Board

#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;

ll sum(ll n)
{
    return n*n;
}

int main()
{
    ll i, lev, lim, mid;

    while (cin >> i && i)
    {
        lev = (ll)ceil(sqrt(i));
        if (lev&1)
        {
            lim = lev*lev;
            mid = lim - (lev-1);
            if (i<mid)
            {
                cout << lev << " " << i-(lev-1)*(lev-1) << endl;
            } else
            {
                cout << lim-(i)+1 << " " << lev << endl;
            }
        } else
        {
            lim = lev*lev;
            mid = lim - (lev-1);
            if (i>=mid)
            {
                cout << lev << " " << lim-i+1 << endl;
            } else
            {
                cout << i-(lev-1)*(lev-1) << " " << lev << endl;
            }
        }
    }
    return 0;
}

Wednesday, November 02, 2011

[UVa] 12195 - Jingle Composing

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#define ERROR 1e-11
using namespace std;

char input[1000000];
int main()
{
    int counter, i;
    double sum;
    char *p;
    while (gets(input))
    {
        if (!strcmp(input,"*")) break;


        counter=0;
        p = strtok(input,"/");
        while (p!=NULL)
        {
            for (i=0, sum=0 ; p[i]>='A'&&p[i]<='Z' ; i++)
            {
                if (p[i]=='W') sum += 1.0;
                else if (p[i]=='H') sum += 1.0/2.0;
                else if (p[i]=='Q') sum += 1.0/4.0;
                else if (p[i]=='E') sum += 1.0/8.0;
                else if (p[i]=='S') sum += 1.0/16.0;
                else if (p[i]=='T') sum += 1.0/32.0;
                else if (p[i]=='X') sum += 1.0/64.0;
            }
            if (fabs(sum-1)<ERROR) counter++;
            p = strtok(NULL,"/");
        }
        printf("%d\n",counter);
    }
    return 0;
}

Connect Rapoo MT750S with Linux (Tested on Manjaro)

 I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...