SPOJ 7001 Visible Lattice Points 莫比乌斯反演
http://www.spoj.com/problems/VLATTICE/
PS:jzp线性筛那个PPT做的很棒...浅显易懂...对理解莫比乌斯反演帮助很大
http://www.isnowfy.com/mobius-inversion/
再PS:这个博客介绍了莫比乌斯反演的一些基本知识,最后的例题也和这个差不多了...
这题是求(0,0,0)~(N,N,N)中gcd(a,b,c)=1的点的个数
显然就是莫比乌斯反演的板题了...很容易就能得出F(1) = sigma(miu(d)*(n/d)*(n/d)*(n/d))
但这求出来的是(0,0,0)~(N,N,N)中 i,j,k>0 三维空间内点的个数...含0的点并没有包含在莫比乌斯函数中
还要加上退化的三个平面内的点...所以要加上3个平面内整点 sigma(miu(d)*(n/d)*(n/d)),化简以后就是代码中的形式了
/********************* Template ************************/ #include <set> #include <map> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <bitset> #include <cstdio> #include <string> #include <vector> #include <cassert> #include <cstdlib> #include <cstring> #include <sstream> #include <fstream> #include <numeric> #include <iomanip> #include <iostream> #include <algorithm> #include <functional> using namespace std; #define EPS 1e-8 #define DINF 1e15 #define MAXN 1000050 #define MOD 1000000007 #define INF 0x7fffffff #define LINF 1LL<<60 #define PI 3.14159265358979323846 #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define BUG cout<<" BUG! "<<endl; #define LINE cout<<" ------------------ "<<endl; #define FIN freopen("in.txt","r",stdin); #define FOUT freopen("out.txt","w",stdout); #define mem(a,b) memset(a,b,sizeof(a)) #define FOR(i,a,b) for(int i = a ; i < b ; i++) #define read(a) scanf("%d",&a) #define read2(a,b) scanf("%d%d",&a,&b) #define read3(a,b,c) scanf("%d%d%d",&a,&b,&c) #define write(a) printf("%d\n",a) #define write2(a,b) printf("%d %d\n",a,b) #define write3(a,b,c) printf("%d %d %d\n",a,b,c) #pragma comment (linker,"/STACK:102400000,102400000") template<class T> inline T L(T a) {return (a << 1);} template<class T> inline T R(T a) {return (a << 1 | 1);} template<class T> inline T lowbit(T a) {return (a & -a);} template<class T> inline T Mid(T a,T b) {return ((a + b) >> 1);} template<class T> inline T gcd(T a,T b) {return b ? gcd(b,a%b) : a;} template<class T> inline T lcm(T a,T b) {return a / gcd(a,b) * b;} template<class T> inline T Min(T a,T b) {return a < b ? a : b;} template<class T> inline T Max(T a,T b) {return a > b ? a : b;} template<class T> inline T Min(T a,T b,T c) {return min(min(a,b),c);} template<class T> inline T Max(T a,T b,T c) {return max(max(a,b),c);} template<class T> inline T Min(T a,T b,T c,T d) {return min(min(a,b),min(c,d));} template<class T> inline T Max(T a,T b,T c,T d) {return max(max(a,b),max(c,d));} template<class T> inline T exGCD(T a, T b, T &x, T &y){ if(!b) return x = 1,y = 0,a; T res = exGCD(b,a%b,x,y),tmp = x; x = y,y = tmp - (a / b) * y; return res; } template<class T> inline T reverse_bits(T x){ x = (x >> 1 & 0x55555555) | ((x << 1) & 0xaaaaaaaa); x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc); x = (x >> 4 & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0); x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00); x = (x >>16 & 0x0000ffff) | ((x <<16) & 0xffff0000); return x; } typedef long long LL; typedef unsigned long long ULL; //typedef __int64 LL; typedef unsigned __int64 ULL; /********************* By F *********************/ int T,cnt; int prime[MAXN]; int pri[MAXN]; int phi[MAXN]; int miu[MAXN]; /* 莫比乌斯筛 */ void pre_miu(){ mem(prime,0); cnt = 0; miu[1] = 1; for(int i = 2 ; i < MAXN ; i++){ if(!prime[i]){ miu[i] = -1; pri[cnt++] = i; } for(int j = 0 ; j < cnt ; j++){ if(i * pri[j] > MAXN) break; prime[i * pri[j]] = 1; if(i % pri[j] == 0){ miu[i * pri[j]] = 0; break; }else miu[i * pri[j]] = -miu[i]; } } } /* 欧拉筛 */ void pre_phi(){ mem(prime,0); cnt = 0; phi[1] = 1; for(int i = 2 ; i < MAXN ; i++){ if(!prime[i]){ miu[i] = -1; pri[cnt++] = i; } for(int j = 0 ; j < cnt ; j++){ if(i * pri[j] > MAXN) break; prime[i * pri[j]] = 1; if(i % pri[j] == 0){ phi[i * pri[j]] = phi[i] * pri[j]; break; }else phi[i * pri[j]] = phi[i] * (pri[j] - 1); } } } int main(){ //FIN; //FOUT; pre_miu(); int T; cin>>T; while(T--){ LL n; cin>>n; LL ans = 0; for(LL i = 1; i <= n ; i++) ans += ((n/i + 1) * (n/i + 1) * (n/i + 1) - 1) * (LL)miu[i]; cout<<ans<<endl; } return 0; }