【数论-莫比乌斯】SPOJ-7001-Visible Lattice Points 、zoj 3435 Ideal Puzzle Bobble

VLATTICE - Visible Lattice Points

no tags 

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 : 




 
Sample Output : 

19 
175 
 
Constraints : 
T <= 50 
1 <= N <= 1000000


题意:有一三维网格长宽高均为(0~n),每个网格上有一棵树,问从(0,0,0)看(0~n, 0~n, 0~n),问能看到多少棵树;
思路:这道题比hdu2841多加了一维!树的三维坐标gcd为1,则这棵树可以被看见,利用莫比乌斯反演;
所以我们处理这道题目:从(0,0,0)看(1~n,1~m,1~k),然后剩下三面作为三个二维来处理就好;
(想像一下,比较感性的认识~)
代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

typedef long long ll;

const int N=1000100;

bool check[N+10];
int prime[N+10];
int mu[N+10];
void Mublus()
{
    memset(check,false,sizeof(check));
    mu[1]=1;
    int tot=0;
    for(int i=2;i<=N;i++)
    {
        if(!check[i])
        {
            prime[tot++]=i;
            mu[i]=-1;
        }
        for(int j=0;j<tot;j++)
        {
            if(i*prime[j]>N) break;
            check[i*prime[j]]=true;
            if(i%prime[j]==0)
            {
                mu[i*prime[j]]=0;
                break;
            }
            else
            {
                mu[i*prime[j]]=-mu[i];
            }
        }
    }
}

int sum[N+10];
ll solve(int n)
{
    ll ans=0;
//    if(n>m) swap(n,m);
    for(int i=1,la=0;i<=n;i=la+1)
    {
        la=n/(n/i);
        ans+=(ll)(sum[la]-sum[i-1])*(n/i)*(n/i)*(n/i);
    }
    return ans;
}

ll solve1(int n)
{
    ll ans=0;
    for(int i=1,la=0;i<=n;i=la+1)
    {
        la=n/(n/i);
        ans+=(ll)(sum[la]-sum[i-1])*(n/i)*(n/i);
    }
    return ans;
}

int main()
{
    Mublus();
    sum[0]=0;
    for(int i=1;i<=N;i++)
        sum[i]=sum[i-1]+mu[i];

    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);

        ll ans=solve(n)+3*solve1(n);

        printf("%lld\n",ans+3);
    }
    return 0;
}


******************************************************************************************************************************************************************************************************

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 smallwith 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

题意:有一个三维网格,长宽高分别为n,m,k,每个格点都有一棵树;问从(1,1,1)到(n ,m ,k)能看到多少棵树?

思路:可以转化为上一题的题意:从(0,0,0)看(0~n-1 , 0~m-1 ,0~ k-1)能看的多少棵树?再转化:从(0,0,0)看(1~n-1,1~m-1,1~k-1),剩下三面当作三个二维来处理;

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

typedef long long ll;

const int N=1000100;

bool check[N+10];
int prime[N+10];
int mu[N+10];
void Mublus()
{
    memset(check,false,sizeof(check));
    mu[1]=1;
    int tot=0;
    for(int i=2; i<=N; i++)
    {
        if(!check[i])
        {
            prime[tot++]=i;
            mu[i]=-1;
        }
        for(int j=0; j<tot; j++)
        {
            if(i*prime[j]>N) break;
            check[i*prime[j]]=true;
            if(i%prime[j]==0)
            {
                mu[i*prime[j]]=0;
                break;
            }
            else
            {
                mu[i*prime[j]]=-mu[i];
            }
        }
    }
}

int sum[N+10];
ll solve(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,la=0; i<=n; i=la+1)
    {
        la=min(min(n/(n/i),m/(m/i)),k/(k/i));
        ans+=(ll)(sum[la]-sum[i-1])*(n/i)*(m/i)*(k/i);
    }
    return ans;
}

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

int main()
{
    Mublus();
    sum[0]=0;
    for(int i=1; i<=N; i++)
        sum[i]=sum[i-1]+mu[i];

    int n,m,k;
    while(~scanf("%d%d%d",&n,&m,&k))
    {

        ll ans=solve(n-1,m-1,k-1)+solve1(n-1,m-1)+solve1(n-1,k-1)+solve1(m-1,k-1);

        printf("%lld\n",ans+3);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值