spoj 7001 zoj 3435 莫比乌斯反演

57 篇文章 0 订阅

Consider a N*N*N lattice. One corner is at (0,0,0) and the opposite one is at (N,N,N). How many lattice points are visible from corner at (0,0,0) ? A point X is visible from point Y iff no other lattice point lies on the segment joining X and Y.
 
Input :
The first line contains the number of test cases T. The next T lines contain an interger N
 
Output :
Output T lines, one corresponding to each test case.
 
Sample Input :
3
1
2
5
 
Sample Output :
7
19
175
 
Constraints :
T <= 50
1 <= N <= 1000000



题意:

给出一个 n*n*n 的立方体,求从(0,0,0)可以看到的有多少个点

题解:

易知gcd(x , y , z)==1,的这个点一定可以遮盖住gcd(x , y , z)>1 的点

故是求gcd(x , y , z)==1 有多少个点

令 f(n) = gcd(x , y , z)为质数的对数

F(d)= gcd(x , y , z)为质数的倍数的对数

故:



特殊处理一下在x ,y  z 轴上的,以及平面上的点



#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define MAXN 1000000
#define LL long long
bool check[MAXN+10];
int primer[MAXN+10];
int mu[MAXN+10];

void Moblus()
{
    memset(check,0,sizeof(check));
    mu[1]=1;
    int tot=0;
    for(int i=2;i<=MAXN;i++){
        if(!check[i]){
            primer[tot++]=i;
            mu[i]=-1;
        }
        for(int j=0;j<tot&&i*primer[j]<=MAXN;j++){
            check[i*primer[j]]=true;
            if(i%primer[j]==0){
                mu[i*primer[j]]=0;
                break;
            }
            mu[i*primer[j]]=-mu[i];
        }
    }
}

int main()
{
    int T;
    Moblus();
    int n;
   // freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        LL ans=3;
        for(int i=1;i<=n;i++)
            ans+=(LL)mu[i]*(n/i)*(n/i)*(n/i+3);
        printf("%lld\n",ans);
    }
    return 0;
}



zoj 3435

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 (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 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 L, 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


和上面的一题一样,只是分情况都处理了



#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define MAXN 1000000
#define LL long long
bool check[MAXN+10];
int primer[MAXN+10];
int mu[MAXN+10];
int sum[MAXN+10];

void Moblus()
{
    memset(check,0,sizeof(check));
    sum[1]=mu[1]=1;
    int tot=0;
    for(int i=2;i<=MAXN;i++){
        if(!check[i]){
            primer[tot++]=i;
            mu[i]=-1;
        }
        for(int j=0;j<tot&&i*primer[j]<=MAXN;j++){
            check[i*primer[j]]=true;
            if(i%primer[j]==0){
                mu[i*primer[j]]=0;
                break;
            }
            mu[i*primer[j]]=-mu[i];
        }
        sum[i]=sum[i-1]+mu[i];
    }
}

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

LL cal(int n, int m, int k)
{
    LL ans=0;
    if(n>m) swap(n,m);
    if(n>k) swap(n,k);

    for(int i=1,last;i<=n;i=last+1){
        last=min(n/(n/i),min(m/(m/i),k/(k/i)));
        ans+=(LL)(sum[last]-sum[i-1])*(n/i)*(m/i)*(k/i);
    }
    return ans;
}

int main()
{
    Moblus();
    int a,b,c;
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d%d",&a,&b,&c)!=EOF)
    {
        LL ans=3;
        a--,b--,c--;
        ans+=cal(a,b,c);
        ans+=cal(a,b);
        ans+=cal(b,c);
        ans+=cal(a,c);
        printf("%lld\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值