O(1) gcd 板子

const int N = 2e5+10;
const int M = 500;
int cnt, p[N], _gcd[M][M];
int v[N][3],vis[N];
int gcd(int x, int y) {
	int g = 1;
	REP(i,0,2) {
		int tmp;
		if (v[x][i]>=M) {
			if (y%v[x][i]==0) tmp = v[x][i];
			else tmp = 1;
		}
		else tmp = _gcd[y%v[x][i]][v[x][i]];
		y /= tmp, g *= tmp;
	}
	return g;
}

void init() {
	v[1][0] = v[1][1] = v[1][2] = 1;
	REP(i,2,N-1) {
		if (!vis[i]) p[++cnt]=i,v[i][0]=v[i][1]=1,v[i][2]=i;
		for (int j=1; j<=cnt&&i*p[j]<N; ++j) {
			vis[i*p[j]] = 1;
			int *A = v[i*p[j]], *B = v[i];
			A[0] = B[0]*p[j], A[1] = B[1], A[2] = B[2];
			if (A[0]>A[1]) swap(A[0],A[1]);
			if (A[1]>A[2]) swap(A[1],A[2]);
			if (i%p[j]==0) break;
		}
	}
	REP(i,1,M-1) {
		_gcd[i][0] = _gcd[0][i] = i;
		REP(j,1,i) _gcd[i][j]=_gcd[j][i]=_gcd[i%j][j];
	}
}

 

posted @ 2019-09-24 11:31  uid001  阅读(292)  评论(0编辑  收藏  举报