Trang

Thứ Năm, 6 tháng 1, 2011

3254. Rock-Paper-Scissors

import java.io.*;

public class Main {

    /**
     * http://acm.tju.edu.cn/toj/showp3254.html
     * @author hunglee
     */
    public static void main(String[] args) throws IOException {
        
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(
                new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(System.out);
        
        int R, N;
        String sven, friend[];
        in.nextToken();
        R = (int) in.nval;
        in.nextToken();
        sven = in.sval;
        in.nextToken();
        N = (int) in.nval;
        friend = new String[N];
        for (int i = 0; i < N; ++i) {
            in.nextToken();
            friend[i] = in.sval;
        }
        
        int scores = 0, maxScores = 0;
        int[] r, p, s;
        r = new int[R];
        p = new int[R];
        s = new int[R];
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < R; ++j) {
                scores += getPoint(sven.charAt(j), friend[i].charAt(j));
                switch (friend[i].charAt(j)) {
                case 'S':
                    ++s[j]; break;
                case 'P':
                    ++p[j]; break;
                default:
                    ++r[j]; break;
                }
            }
        }
        for (int j = 0; j < R; ++j)
            maxScores += Math.max(s[j] + 2 * p[j], Math.max(
                    p[j] + 2 * r[j], r[j] + 2 * s[j]));
        out.println(scores + "\n" + maxScores);
        out.flush();
    }
    
    private static int getPoint(char ch, char ch2) {
        switch (ch) {
        case 'S':
            switch (ch2) {
            case 'R': return 0;
            case 'P': return 2;
            default:  return 1;
            }
        case 'P':
            switch (ch2) {
            case 'R': return 2;
            case 'P': return 1;
            default:  return 0;
            }
        default:
            switch (ch2) {
            case 'R': return 1;
            case 'P': return 0;
            default:  return 2;
            }
        }
    }

}

3293. A Sequence of Numbers

import java.io.*;

public class Main {

    /**
     * http://acm.tju.edu.cn/toj/showp3293.html
     * @author hunglee
     */
    public static void main(String[] args) throws IOException {
        
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(
                new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(System.out);
        
        long N, a, b, c, d, k, i, m = 200907, n, res;
        
        in.nextToken();
        N = (long) in.nval;
        while (N-- > 0) {
            in.nextToken();
            a = (long) in.nval;
            in.nextToken();
            b = (long) in.nval;
            in.nextToken();
            c = (long) in.nval;
            in.nextToken();
            k = (long) in.nval;
            if (c - b == b - a) {
                res = ((a % m) + (((k - 1) % m) * 
                        ((b - a) % m) % m)) % m;
            } else {
                d = (b / a) % m;
                n = k - 1;
                res = a % m;
                for (i = 0; (1l << i) <= n; ++i) {
                    if ((n & (1l << i)) != 0)
                        res = (res * d) % m;
                    d = (d * d) % m;
                }
            }
            out.println(res);
        }
        out.flush();
    }

}

Thứ Hai, 3 tháng 1, 2011

3472. Chopin

import java.io.*;

public class Main {

    /**
     * http://acm.tju.edu.cn/toj/showp3472.html
     * @author hunglee
     */
    public static void main(String[] args) throws IOException {
        
        BufferedReader in = new BufferedReader(
                new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);
        
        int t = Integer.parseInt(in.readLine());
        String[] s;
        while (t-- > 0) {
            s = in.readLine().split(" ");
            if (valid(s))
                out.println("yes");
            else
                out.println("no");
        }
        out.flush();
    }

    private static boolean valid(String[] s) {

        int i = 0, j = 0;
        while (i < s[0].length() && j < s[1].length())
            if (s[0].charAt(i) == s[1].charAt(j)) {
                ++i; ++j;
            } else
                ++i;
        return (j == s[1].length());
    }

}

3498. Differences

import java.io.*;
import java.math.BigInteger;

public class Main {

    /**
     * http://acm.tju.edu.cn/toj/showp3498.html
     * @author hunglee
     */
    public static void main(String[] args) throws IOException {
        
        BufferedReader in = new BufferedReader(
                new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);
        
        int ca, n;
        String[] s;
        ca = Integer.parseInt(in.readLine());
        while (ca-- > 0) {
            n = Integer.parseInt(in.readLine());
            s = in.readLine().split(" ");
            
            BigInteger min = new BigInteger(s[0]);
            BigInteger max = new BigInteger(s[0]);
            BigInteger j;
            for (int i = 1; i < n; ++i) {
                j = new BigInteger(s[i]);
                if (j.compareTo(max) > 0)
                    max = j;
                else if (j.compareTo(min) < 0)
                    min = j;
            }
            out.println(max.subtract(min));
        }
        out.flush();
    }

}

3457. Cow Pinball

import java.io.*;

public class Main {

    /**
     * http://acm.tju.edu.cn/toj/showp3457.html
     * @author hunglee
     */
    public static void main(String[] args) throws IOException {
        
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(
                new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(System.out);
        
        int r, a[][];
        in.nextToken();
        r = (int) in.nval;
        a = new int[r][];
        for (int i = 0; i < r; ++i) {
            a[i] = new int[i + 1];
            for (int j = 0; j <= i; ++j) {
                in.nextToken();
                a[i][j] = (int) in.nval;
            }
        }
        for (int i = 1; i < r; ++i) {
            a[i][0] += a[i - 1][0];
            a[i][i] += a[i - 1][i - 1];
            for (int j = 1; j < i; ++j)
                a[i][j] += Math.max(a[i - 1][j - 1], a[i - 1][j]);
        }
        int res = -1;
        for (int j = 0; j < r; ++j)
            if (res < a[r - 1][j])
                res = a[r - 1][j];
        out.println(res);
        out.flush();
    }

}

3484. Stones

import java.io.*;

public class Main {

    /**
     * http://acm.tju.edu.cn/toj/showp3484.html
     * @author hunglee
     */
    public static void main(String[] args) throws IOException {
        
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(
                new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(System.out);
        
        int t, n, i, sum, max;
        in.nextToken();
        t = (int) in.nval;
        while (t-- > 0) {
            in.nextToken();
            n = (int) in.nval;
            max = -1; sum = 0;
            for (int j = 0; j < n; ++j) {
                in.nextToken();
                i = (int) in.nval;
                sum += i;
                if (max < i)
                    max = i;
            }
            out.println(sum - max);
        }
        out.flush();
    }

}

3483. Common Divisor

import java.io.*;

public class Main {

    /**
     * http://acm.tju.edu.cn/toj/showp3483.html
     * @author hunglee
     */
    public static void main(String[] args) throws IOException {
        
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(
                new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(System.out);
        
        int t, n, m, a, i, j, k, res;
        in.nextToken();
        t = (int) in.nval;
        while (t-- > 0) {
            in.nextToken();
            n = (int) in.nval;
            in.nextToken();
            m = (int) in.nval;
            
            a = gcd(n, m);
            res = 1;
            k = 0;
            while (a % 2 == 0) {
                ++k;
                a = a >> 1;
            }
            res *= (k + 1);
            k = 0;
            while (a % 3 == 0) {
                ++k;
                a /= 3;
            }
            res *= (k + 1);
            i = 5; j = 2;
            while (a > 1 && i * i <= a)
                if (a % i == 0) {
                    k = 0;
                    while (a % i == 0) {
                        ++k;
                        a /= i;
                    }
                    res *= (k + 1);
                } else {
                    i += j;
                    j = 6 - j;
                }
            if (a > 1)
                res = res << 1;
            out.println(res);
        }
        out.flush();
    }
    private static int gcd(int a, int b) {
        int r;
        while (b != 0) {
            r = a % b; a = b; b = r;
        }
        return a;
    }

}