Pages

Thursday 7 March 2013

UVA - 12043 - Divisors

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer();
        long[] sums = new long[100 * 1000 + 1], counters = new long[100 * 1000 + 1];
        for (int i = 1; i < 100 * 1000 + 1; i++) {
            Point p = factors(i);
            counters[i] = (int) p.getX();
            sums[i] = (int) p.getY();
        }
        int cases = Integer.parseInt(br.readLine().trim());
        for (int i = 0; i < cases; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine().trim());
            int a = 0, b = 0, k = 0;
            a = Integer.parseInt(st.nextToken());
            b = Integer.parseInt(st.nextToken());
            k = Integer.parseInt(st.nextToken());
            long sum = 0;
            long counter = 0;
            for (int j = a; j < b + 1; j++) {
                if (j % k == 0) {
                    counter += counters[j];
                    sum += sums[j];
                }
            }
            sb.append(counter).append(" ").append(sum).append("\n");
        }
        System.out.print(sb);
    }

    static Point factors(int x) {
        long sum = 0;
        long counter = 0;
        for (int i = 1; i <= Math.sqrt(x); i++) {
            if (x % i == 0) {
                counter++;
                sum += i;
                if (x / i != i) {
                    sum += x / i;
                    counter++;
                }
            }
        }
        return new Point(counter, sum);
    }
}
class Point {
    private long x;
    private long y;

    public Point(long x, long y) {
        this.x = x;
        this.y = y;
    }

    public long getX() {
        return x;
    }

    public void setX(long x) {
        this.x = x;
    }

    public long getY() {
        return y;
    }

    public void setY(long y) {
        this.y = y;
    }
   
}

No comments:

Post a Comment