zoj3435 Ideal Puzzle Bobble 莫比乌斯反演

Ideal Puzzle Bobble

Time Limit: 2 Seconds       Memory Limit: 65536 KB

Have you ever played Puzzle Bobble, a very famous PC game? In this game, as a very cute bobble dragon, you must keep shooting powerful bubbles to crush all the colorful bubbles upwards. Victory comes when all the bubbles upwards are crushed.

Little Tom is crazy about this game. One day, he finds that all kinds of Puzzle Bobble are 2D Games. To be more excited when playing this game, he comes up with a new idea to design a 3D Puzzle Bobble game! In this game, the bobble dragon is standing in a cubic room with L in length, W in width and H in height. Around him are so many colorful bubbles. We can use 3D Cartesian coordinates (xyz) to represent the locations of the bobble dragon and those bubbles. All these coordinates (xyz) are triple positive integers ranged from (111) to (L,WH).

To simplify the problem, let's assume the bobble dragon is standing at (111) in the room. And there is one colorful bubble at every (xyz) in the room except (111). The dragon is so strong that he can shoot out a magical bubble to crush all the colorful bubbles in the straight line which the magical bubble flies every single time. Note that bubbles are significantly small with respect to the distances between each two bubbles. Our question remains, how many magical bubbles will the cute dragon shoot before crushing all the colorful bubbles around him?

Input

There are multiple cases, no more than 200. Each case contains one single line. In this line, there are three positive integers LW and H (2 ≤ L, W, H ≤ 1000000) which describes the size of the room. Proceed to the end of the file.

Output

For each case, print the number of the magical bubbles needed to crush all the colorful bubbles in one line.

Sample Input

2 2 2
3 3 3

Sample Output

7
19

Author:  ZHU, Yuke
Contest:  ZOJ Monthly, November 2010

题意: 给出一个三维坐标 (x, y, z), 问该点与 (1, 1, 1) 组成的长方体中有多少条经过点 (1, 1, 1) 的直线 .



即求 (0, 0, 0), (x - 1, y - 1 , z - 1) 组成的长方体中有多少条经过 (0, 0, 0) 的直线 .


然后我们将满足条件的直线分为 3 部分:


1. 从 (0, 0, 0) 出发的 3 条棱;


2. 从 (0, 0, 0) 出发的 3 个表面;


3. 从 (0, 0, 0) 出发到长方体内部的直线;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.StringTokenizer;


public class Main {
	
	public static void main(String[] args) {
		new Task().solve();
	}
}


class Task {
	InputReader in = new InputReader(System.in) ;
	PrintWriter out = new PrintWriter(System.out) ;
	final int N = 1000000 ;
	int[] mu = new int[N+1] ;
	int[] prime = new int[N+1] ;
	boolean[] vis = new boolean[N+1] ;
	int[] sum = new int[N+1] ;
	{
		mu[1] = 1 ;
		int psize = 0 ;
		Arrays.fill(vis, false);
		for(int i = 2 ; i <= N ; i++){
			if(! vis[i]){
				prime[psize++] = i ;
				mu[i] = -1 ;
			}
			for(int j = 0 ; j < psize && prime[j] * i <= N ; j++){
				vis[prime[j] * i] = true ;
				if(i % prime[j] == 0){
					mu[prime[j] * i] = 0 ;
					break ;
				}
				mu[prime[j] * i] = -mu[i] ;
			}
		}
		sum[0] = 0 ;
		for(int i = 1 ; i <= N ; i++){
			sum[i] = sum[i-1] + mu[i] ;
		}
	}
	
	long calc3(long l , long w , long h){
		long x = Math.min(Math.min(l , w) , h) ;
		long res = 0 ;
		int last = -1 ;
		for(int i = 1 ; i <= x ; i = last+1){
			last = (int)Math.min(Math.min(l/(l/i), w/(w/i)) , h/(h/i)) ;
			res += (l/i) * (w/i) * (h/i) * (sum[last] - sum[i-1]) ;
		}
		return res ;
	}
	
	long calc2(long l , long w){
		long x = Math.min(l , w) ;
		long res = 0 ;
		int last = -1 ;
		for(int i = 1 ; i <= x ; i = last+1){
			last =  (int)Math.min(l/(l/i), w/(w/i)) ;
			res += (l/i) * (w/i) * (sum[last] - sum[i-1]) ;
		}
		return res ;
	}
	
	void solve(){
		while(in.hasNext()){
			long l = in.nextLong() -1 ;
			long w = in.nextLong() -1 ;
			long h = in.nextLong() -1 ;
			out.println(calc3(l , w , h) + calc2(l, w) + calc2(l, h) + calc2(w, h) + 3) ;
			//out.flush();
		}
		out.flush() ;
	}
}

class InputReader {    
    public BufferedReader reader;    
    public StringTokenizer tokenizer;    
    
    public InputReader(InputStream stream) {    
        reader = new BufferedReader(new InputStreamReader(stream), 32768);    
        tokenizer = new StringTokenizer("");    
    }    
    
    private void eat(String s) {    
        tokenizer = new StringTokenizer(s);    
    }    
    
    public String nextLine() {     
        try {    
            return reader.readLine();    
        } catch (Exception e) {    
            return null;    
        }    
    }    
    
    public boolean hasNext() {    
        while (!tokenizer.hasMoreTokens()) {    
            String s = nextLine();    
            if (s == null)    
                return false;    
            eat(s);    
        }    
        return true;    
    }    
    
    public String next() {    
        hasNext();    
        return tokenizer.nextToken();    
    }    
    
    public int nextInt() {    
        return Integer.parseInt(next());    
    }    
    
    public int[] nextInts(int n) {    
        int[] nums = new int[n];    
        for (int i = 0; i < n; i++) {    
            nums[i] = nextInt();    
        }    
        return nums;    
    }    
    
    public long nextLong() {    
        return Long.parseLong(next());    
    }    
    
    public double nextDouble() {    
        return Double.parseDouble(next());    
    }    
    
    public BigInteger nextBigInteger() {    
        return new BigInteger(next());    
    }    
    
}    


相关文章
相关标签/搜索