SPOJ VLATTICE - Visible Lattice Points 【“小”大数加减】

题目链接

一道比较简单的莫比乌斯反演,不过ans会爆long long,我是用结构体来存结果的,结构体中两个LL型变量分别存大于1e17和小于1e17的部分

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;

const int maxn=1e6;

int prime[maxn+5];
bool check[maxn+5];
int mu[maxn+5];

void init()
{
    mu[1]=1;
    int tot=0;
    for(int i=2;i<=maxn;i++)
    {
        if(!check[i])
        {
            prime[tot++]=i;
            mu[i]=-1;
        }
        for(int j=0;j<tot;j++)
        {
            if(i*prime[j]>maxn) break;
            check[i*prime[j]]=true;
            if(i%prime[j]==0)
            {
                mu[i*prime[j]]=0;
                break;
            }
            else
            {
                mu[i*prime[j]]=-mu[i];
            }
        }
    }
}

LL n;

const LL mod=1e17;
struct node
{
    LL a,b;
    node(LL a_=0,LL b_=0)
    {
        a=a_,b=b_;
    }
    void print()
    {
        if(a)    printf("%lld%017lld\n",a,b);
        else printf("%lld\n",b);
    }
};

node add(node x,LL y)
{
    if(y>=0)
    {
        LL t1=(x.b+y)/mod;
        LL t2=(x.b+y)%mod;
        x.a+=t1,x.b=t2;
        return x;
    }
    else
    {
        LL t1=(x.b+y)/mod;
        LL t2=(x.b+y)%mod;
        if(x.a>0&&t2<0) t2+=mod,t1--;
        x.a+=t1,x.b=t2;
        return x;
    }
}

int main()
{
    init();
    int T;
    node t;
    LL x;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld",&n);
        node ans;
        for(int i=1;i<=n;i++)
            ans=add(ans,mu[i]*(n/i)*(n/i)*(n/i));
        for(int i=1;i<=n;i++)
            ans=add(ans,mu[i]*(n/i)*(n/i)*3);
        ans=add(ans,3);
        ans.print();
    }
}
时间: 2024-10-04 08:47:57

SPOJ VLATTICE - Visible Lattice Points 【“小”大数加减】的相关文章

[SPOJ VLATTICE]Visible Lattice Points 数论 莫比乌斯反演

7001. Visible Lattice Points Problem code: VLATTICE 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 lat

SPOJ VLATTICE Visible Lattice Points 莫比乌斯反演 难度:3

http://www.spoj.com/problems/VLATTICE/ 明显,当gcd(x,y,z)=k,k!=1时,(x,y,z)被(x/k,y/k,z/k)遮挡,所以这道题要求的是gcd(x,y,z)==1的个数+{(x,y,0)|gcd(x,y)==1}的个数+3{(0,0,1),(0,1,0),(1,0,0)} 现在不去管最后的三个坐标轴上的点, 设f(i)=|{(x,y,0)|gcd(x,y)==i}|*3+|{(x,y,z)|gcd(x,y,z)==i}|,也就是不在坐标轴上且

SPOJ—VLATTICE Visible Lattice Points(莫比乌斯反演)

http://www.spoj.com/problems/VLATTICE/en/ 题意: 给一个长度为N的正方形,从(0,0,0)能看到多少个点. 思路:这道题其实和能量采集是差不多的,只不过从二维上升到了三维. 分三部分计算: ①坐标值上的点,只有3个. ②与原点相邻的三个表面上的点,需满足gcd(x,y)=1. ③其余空间中的点,需满足gcd(x,y,z)=1. 1 #include<iostream> 2 #include<algorithm> 3 #include<

SPOJ - VLATTICE Visible Lattice Points 莫比乌斯反演

题目大意: 从坐标(0,0,0)处观察到所有在(n,n,n)范围内的点的个数,如果一条直线上出现多个点,除了第一个,后面的都视为被遮挡了 这题目稍微推导一下可得知 gcd(x,y,z) = 1的点是可观察到的,若三者的gcd>1,则这个点之前必然出现了一个(x/gcd(x,y,z) , y/gcd(x,y,z) , z/gcd(x,y,z))的点 那么因为 0 是无法计算gcd的 , 所以先不考虑0 1. x>0 , y>0 , z>0  ans = ∑x<=n∑y<=

SPOJ VLATTICE Visible Lattice Points 初入莫比乌斯

题意:求两个点(x,y,z)的连线不经过其他点有几个 解:即为求GCD(x,y,z)为1的点有几个 解一:因为x,y,z均在1~n内,所以可以用欧拉函数解出 解二:莫比乌斯反演 设f[n]为GCD(x,y,z)=n的个数 设F[b]为b|GCD(x,y,z)的个数,很明显F[b]=(n/i)*(n/i)*(n/i) 所以F[n]=sigema(b|n,f[b]); f[n]=sigema(n|b,mu[n],F[n]) #include <stdio.h> #include <strin

SPOJ1007 VLATTICE - 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

spoj 7001 Visible Lattice Points莫比乌斯反演

Visible Lattice Points Time Limit:7000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description 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 co

Visible Lattice Points(spoj7001+初探莫比乌斯)gcd(a,b,c)=1 经典

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

【POJ】3090 Visible Lattice Points

Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7705   Accepted: 4707 Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible fr