您的位置:首页 > 其它

ZOJ 3435 Ideal Puzzle Bobble (莫比乌斯反演基础题)

2015-08-24 18:25 363 查看
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 andH in height. Around him are so many colorful bubbles. We can use 3D Cartesian coordinates (x,y,
z) to represent the locations of the bobble dragon and those bubbles. All these coordinates (x,y,
z) are triple positive integers ranged from (1, 1,
1) to (L, W, H).

To simplify the problem, let's assume the bobble dragon is standing at (1,1,
1) in the room. And there is one colorful bubble at every (x,y,
z) in the room except (1, 1, 1). The dragon is so strong that he can shoot out a magical bubble to crushall the colorful bubbles in the straight line which the magical bubble flies every single time. Note
that bubbles aresignificantly 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 integersL,
W 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

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3435

题目大意:(1,1,1)和(l, w, h)组成的长方体中从点(1,1,1)能看到的点数

题目分析:和前一题几乎一样,唯一的不同就是gcd(a, b, c) = 1,1 <= a <= l,1 <= b <= w,1 <= c <= h,范围不同,然而这并没有什么卵关系,l,w,h都减1,函数的参数变一下就行了

#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;
int const MAX = 1000005;
int mob[MAX], p[MAX], sum[MAX];
bool noprime[MAX];

int Min(int a, int b, int c)
{
    return min(a, min(b, c));
}

void Mobius()
{
    int pnum = 0;
    mob[1] = 1;
    sum[1] = 1;
    for(int i = 2; i < MAX; i++)
    {
        if(!noprime[i])
        {
            p[pnum ++] = i;
            mob[i] = -1;
        }
        for(int j = 0; j < pnum && i * p[j] < MAX; j++)
        {
            noprime[i * p[j]] = true;
            if(i % p[j] == 0)
            {
                mob[i * p[j]] = 0;
                break;
            }
            mob[i * p[j]] = -mob[i];
        }
        sum[i] = sum[i - 1] + mob[i]; 
    }
}

ll cal(int l, int r)
{
    if(l > r)
        swap(l, r);
    ll ans = 0;
    for(int i = 1, last = 0; i <= l; i = last + 1)
    {
        last = min(l / (l / i), r / (r / i));
        ans += (ll) (l / i) * (r / i) * (sum[last] - sum[i - 1]);
    }
    return ans;
}

ll cal(int l, int m, int r)
{
    if(l > r)
        swap(l, r);
    if(l > m)
        swap(l, m);
    ll ans = 0;
    for(int i = 1, last = 0; i <= l; i = last + 1)
    {
        last = Min(l / (l / i), m / (m / i), r / (r / i));
        ans += (ll) (l / i) * (m / i) * (r / i) * (sum[last] - sum[i - 1]);
    }
    return ans;
}

int main()
{
    Mobius();
    int a, b, c;
    while(scanf("%d %d %d", &a, &b, &c) != EOF)
    {
        a --;
        b --;
        c --;
        ll ans = 3;
        ans += (ll) cal(a, b, c);
        ans += (ll) cal(a, b);
        ans += (ll) cal(b, c);
        ans += (ll) cal(a, c);
        printf("%lld\n", ans);
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: