zoj3435(莫比乌斯反演+数论分块)

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 (LWH).

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


/*
思路:把问题转换为三种情况
1.起点连接的三个楞
2.起点连接的三个面
3.除1、2以外立方体里面的点

由于起点在(1,1,1),位了方面做gcd,把它移到(0,0,0),即 L--;W--;H--;
对于1,答案是3,没有疑问。 
对于2,就是要找出三个面中gcd(x,y)==1的情况数,利用莫比乌斯反演公式即可
对于3,也是一样,找出gcd(x,y,z)==1的情况数

G(n)代表gcd等于n的情况数,若为情况二,G(n)=(a/n)*(b/n),
若为情况三,G(n)=(a/n)*(b/n)*(c/n)

由于case数较多,加上数学分块优化
对于a/x:
x=i与x=a/(a/i)时它的结果是一样的,
更具体的,i<=x<= a/(a/i)时,a/x的结果都一样
而且这个区间随着i的增大而变所以可以算除mu的前缀和分块计算 
*/ 
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long LL;
int L,W,H;
const int maxn=1000005;
bool vis[maxn];
int prime[maxn],mu[maxn],sum[maxn];
int maxnum(int a,int b,int c){return max(a,max(b,c));}
int minnum(int a,int b,int c){return min(a,min(b,c));}
void init_mu(int n){
	memset(vis,0,sizeof(vis));
	memset(prime,0,sizeof(prime));
	memset(mu,0,sizeof(mu));
	memset(sum,0,sizeof(sum));
    int cnt=0;
    mu[1]=1;
    for(int i=2;i<n;i++){
        if(!vis[i])      //素数没有出现过 
		{
            prime[cnt++]=i;
            mu[i]=-1;
        }
        for(int j=0;j<cnt&&i*prime[j]<n;j++)
		{
            vis[i*prime[j]]=1;
            if(i%prime[j]==0)     // i*prime[j]为第三类,即包含重复素数的乘积,置0 
			{
				mu[i*prime[j]]=0;
				break;
			}
            else                //前两类 
				mu[i*prime[j]]=-mu[i];
        }
    }
    for(int i=1;i<n;i++)sum[i]=sum[i-1]+mu[i];   //计算mu[]的前缀和 
}
LL cal(int a,int b)
{
	int j;
	LL ans=0;
	for(int i=1;i<=b;i=j+1)
	{
		j=min(a/(a/i),b/(b/i));      //数论分块原理 
		ans+=(long long)(sum[j]-sum[i-1])*(a/i)*(b/i);
	}
	return ans;
}
LL cal(int a,int b,int c)
{
	int j;
	LL ans=0;
	for(int i=1;i<=c;i=j+1)
	{
		j=minnum(a/(a/i),b/(b/i),c/(c/i));
		ans+=(long long)(sum[j]-sum[i-1])*(a/i)*(b/i)*(c/i);
	}
	return ans;
}
int main()
{
	init_mu(maxn);
	while(~scanf("%d%d%d",&L,&W,&H))
	{
		L--;W--;H--;
		LL ans=3;
		int ma=maxnum(L,W,H),mi=minnum(L,W,H),mid=L+W+H-ma-mi;
		
		ans+=cal(ma,mid,mi);
		ans+=cal(ma,mid);
		ans+=cal(ma,mi);
		ans+=cal(mid,mi);
		printf("%lld\n",ans);
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值