LCM问题(素数,约数处理, UVA 10791)

注意:
1. 2147483647,也就是(1 << 31) - 1,是素数,所以结果要用long
2. 1 要特殊处理一下
3. LCM 的一些性质

import
java.io.*; import java.util.*; import java.math.*; public classMain { static final int MAXN = 100005; int p[], sup, n; boolean flag[]; void getPrimes() { p = new int[MAXN]; flag = new boolean[MAXN]; for (int i = 2; i < MAXN; i++) { if (flag[i] == false) { p[sup++] = i; for (int j = i << 1; j < MAXN; j += i) flag[j] = true; } } } void run() { getPrimes(); int Case = 1; while (true) { n = cin.nextInt(); if (n == 0) break; System.out.print("Case " + Case++ + ": "); if (n == 1) System.out.println(2); else { int count = 0, m = (int) Math.sqrt(n); long sum = 0; for (int i = 0; i < sup && p[i] <= m; i++) { if (n % p[i] == 0) { count++; int temp = 1; while (n % p[i] == 0) { temp *= p[i]; n /= p[i]; } sum += temp; } }
                    
                    // 两段相同功能
                    for
(int i = 2; i <= m; i++) { if (n % i == 0) { int temp = 1; count++; while (n % i == 0) { temp *= i; n /= i; } sum += temp; } }
                    
				if (n > 1) {
					sum += n;
					count++;
				}
				if (count == 1) sum++;
				System.out.println(sum);
			}
		}
	}               
	
	public static void main(String[] args) {
		Mainsolved = new Main();
		solved.run();
	}
	
//	static InputStream inputStream = System.in;
//	static InputReader cin = new InputReader(inputStream);
	
	Scannercin = new Scanner(new BufferedInputStream(System.in));
	
}


classInputReader { 

    private InputStreamstream; 
    private byte[] buf = new byte[1024]; 
    private int curChar; 
    private int numChars; 

    public InputReader(InputStreamstream) { 
        this.stream = stream; 
    } 

    public int read() { 
        if (numChars == -1) 
            return -1; 
            //throw new InputMismatchException(); 
        if (curChar >= numChars) { 
            curChar = 0; 
            try { 
                numChars = stream.read(buf); 
            } catch (IOExceptione) { 
                throw new InputMismatchException(); 
            } 
            if (numChars <= 0) 
                return -1; 
        } 
        return buf[curChar++]; 
    } 

    public int nextInt() { 
        int c = read(); 
        if (c == -1) 
            return -1; 
        while (isSpaceChar(c)) 
            c = read(); 
        int sgn = 1; 
        if (c == '-') { 
            sgn = -1; 
            c = read(); 
        } 
        int res = 0; 
        do { 
            if (c < '0' || c > '9') 
                throw new InputMismatchException(); 
            res *= 10; 
            res += c - '0'; 
            c = read(); 
        } while (!isSpaceChar(c)); 
        return res * sgn; 
    } 
     
    public Stringnext() {   
        StringBuilderstr = new StringBuilder();   
        int ch;   
        while (isSpaceChar(ch = read()));   
        if (ch == -1)   
            return null;   
        do {   
            str.appendCodePoint(ch);   
        } while (!isSpaceChar(ch = read()));   
        return str.toString();   
    }  
     
    public static boolean isSpaceChar(int c) { 
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; 
    } 

    public char nextCharacter() { 
        int c = read(); 
        while (isSpaceChar(c)) 
            c = read(); 
        return (char) c; 
    } 

} 

posted on 2013-02-20 21:59  Sure_Yi  阅读(226)  评论(0编辑  收藏  举报

导航