SPOJ:Visible Lattice Points(莫比乌斯函数 分块加速)

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

题意:给出N*N*N的正方体,问在(0,0,0)点可以看见多少个点。

思路:若(x,y,z)点可视,则gcd(x,y,z)=1,于是可用莫比乌斯函数解决之,因为若gcd不为1,则这个点被(x/gcd(x,y,z), y/gcd(x,y,z), z/gcd(x,y,z))挡住了,然后在面上和在棱上的点另外讨论,面上的当二元组求,加上棱上的只有3个点。这里可使用分块加速优化(n/i的值很多都是一样的,为了避免重复计算,可以一次性求出n/i相同的值,因此莫比乌斯数组改为前缀和)。

# include <iostream>
# include <cstdio>
# include <cstring>
# define ll long long
# define maxn 1000000
using namespace std;
short mu[maxn+3];
bool bo[maxn+3];
int prime[maxn+3];
int n;


void get_table()
{
    mu[1] = 1;
    memset(bo, 0, sizeof(bo));
    for(int i=2; i<=maxn; ++i)
    {
        if(!bo[i])
        {
            prime[++prime[0]] = i;
            mu[i] = -1;
        }
        for(int j=1; j<=prime[0] && i*prime[j]<=maxn; ++j)
        {
            bo[i*prime[j]] = 1;
            if(i%prime[j]==0)
            {
                mu[i*prime[j]] = 0;
                break;
            }
            else
                mu[i*prime[j]] = -mu[i];
        }
    }
    for(int i=1; i<=maxn; ++i)//分块加速。
        mu[i] += mu[i-1];
}


void solve()
{
    ll ans=0;
    for(int i=1, j; i<=n; i=j+1)
    {
        j = n/(n/i);
        ans += (ll)(mu[j]-mu[i-1])*(n/i)*(n/i)*(n/i+3);
    }
    printf("%lld\n",ans+3);
}


int main()
{
    get_table();
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        solve();
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值