ZOJ 3435 Ideal Puzzle Bobble 莫比乌斯反演
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4119
依然是三维空间内求(1,1,1)~(a,b,c)能看到的整点数,平移一下转化成(0,0,0)~(a-1,b-1,c-1)就和前一题就一样了
还是莫比乌斯反演求gcd(a,b,c)=1的组数,公式还是sigma{u(d) * ((a/d+1) * (b/d+1) * (c/d+1) - 1)}
但直接暴力会T...所以加了分块优化...因为当a/d,b/d,c/d的值保持不变的时候...可以跳过很多数据
所以维护一下miu的前缀和...中间相同的部分就可以直接得出了
/********************* 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 miu[MAXN]; int sum[MAXN]; /* 莫比乌斯筛 */ void pre_miu(){ 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 && i * pri[j] <= MAXN ; j++){ prime[i * pri[j]] = 1; if(i % pri[j] == 0){ miu[i * pri[j]] = 0; break; }else miu[i * pri[j]] = -miu[i]; } } for(int i = 1 ; i < MAXN ; i++) sum[i] = sum[i-1] + miu[i]; } int main(){ //FIN; //FOUT; pre_miu(); int a,b,c; while(~scanf("%d%d%d",&a,&b,&c)){ a--;b--;c--; int m = Max(a,b,c); LL res = 0; for(int i = 1 ,now = INF ; i <= m ; i = now+1){ now = Min(a/i ? a/(a/i) : INF , b/i ? b/(b/i) : INF , c/i ? c/(c/i) : INF); res += ((LL)(a/now+1) * (LL)(b/now+1) * (LL)(c/now+1) - 1) * (sum[now] - sum[i-1]); } printf("%lld\n",res); } return 0; }