SPOJ 7001. Visible Lattice Points 莫比乌斯反演

A - 莫比乌斯反演
Time Limit:1368MS    Memory Limit:1572864KB    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 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

Hint

Added by: Varun Jalan
Date: 2010-07-29
Time limit: 1.368s
Source limit: 50000B
Memory limit: 1536MB
Cluster: Cube (Intel G860)
Languages: All except: NODEJS objc PERL 6 VB.net
Resource: own problem used for Indian ICPC training camp


题要求求满足gcd(a,b,c)=1 的个数 其中0<=a,b,c<=n

令f[n]=gcd(a,b,c)=n的个数

F[n]为gcd(a,b,c)=d的倍数的个数

F[n]=sigma(n|d,f(d));即F[d]=(n/i)*(n/i)*(n/i)

f[n]=sigma(n|d,mu[d/n]*F[d]);

求f(1)=sigma(n,mu[i]*F[i]);

因为这是从(0,0,0)开始所以F[d]=(n/i)*(n/i)*(n/i+3)///xyz上的点

ACcode:

#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define maxn 1000100
#define ll long long
using namespace std;
bool vis[maxn];
int prime[maxn];
int mu[maxn];
void get_M(){
    memset(vis,false,sizeof(vis));
    mu[1]=1;
    int tot=0;
    for(int i=2;i<=maxn;++i){
        if(!vis[i]){
            prime[tot++]=i;
            mu[i]=-1;
        }
        for(int j=0;j<tot;j++){
            if(i*prime[j]>maxn)break;
            vis[i*prime[j]]=1;
            if(i%prime[j]==0){
                mu[i*prime[j]]=0;
                break;
            }
            else mu[i*prime[j]]=-mu[i];
        }
    }
}
int main(){
    int loop,n;
    get_M();
    scanf("%d",&loop);
    while(loop--){
        scanf("%d",&n);
        ll ans=3;
        for(int i=1;i<=n;++i)ans+=(long long)mu[i]*(n/i)*(n/i)*((n/i)+3);
        printf("%lld\n",ans);
    }
    return 0;
}


你可能感兴趣的:(C++)