题意:给你五个数a,b,c,d,k,令x ∈[a,b], y∈ [c,d]。求出有多少对(x,y)可以使gcd(x,y) == k。题中所有的a,b都等于1.
题解:
1. b /= k, d /= k, 这样就转换成求b,d之间有多少对互素。
2.不妨令b<=d, 那么我们枚举y,当1<=y <=b的时候,与y互素的的个数就是phi(y)。
3.当b < y <= d的时候,先将y因式分解 y = p1^k1*p2^k2···,然后用容斥求出[1,b]中与p1,p2都互素的数,这些数即是x。
#include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; #define lint __int64 #define MAXN 200000 int p[MAXN], a[MAXN], pn; int f[MAXN], fnum; //f用来存储素因子,fnum表示素因子的个数 int l1, r1, l2, r2, k; lint eul[MAXN]; //这里的欧拉是叠加之后的值,所以用lint void prime () { int i, j; pn = 0; memset(a,0,sizeof(a)); for ( i = 2; i < MAXN; i++ ) { if ( a[i] == 0 ) p[pn++] = i; for ( j = 0; j < pn && i * p[j] < MAXN && (p[j] <= a[i] || a[i] == 0); j++ ) a[i*p[j]] = p[j]; } } void split ( int n ) { fnum = 0; while ( a[n] != 0 ) { f[fnum++] = a[n]; lint tmp = n; while ( tmp % a[n] == 0 ) tmp /= a[n]; n = tmp; } if ( n != 1 ) f[fnum++] = n; } void Euler () { eul[1] = 0; for ( int i = 2; i < MAXN; i++ ) { if ( a[i] == 0 ) eul[i] = i - 1; else { int k = i / a[i]; if ( k % a[i] == 0 ) eul[i] = eul[k] * a[i]; else eul[i] = eul[k] * ( a[i] - 1 ); } } eul[1] = 1; for ( int i = 2; i < MAXN; i++ ) eul[i] = eul[i-1] + eul[i]; } int In_Exclusion ( int k, int num ) //容斥原理,求出[1,b]与当前的y不互素的个数 { if ( ! num ) return 0; int ret = 0; for ( int i = k; i < fnum; i++ ) ret += num / f[i] - In_Exclusion ( i + 1, num / f[i] ); return ret; } int main() { prime(); Euler(); int Case, cs; scanf("%d",&Case); for ( cs = 1; cs <= Case; cs++ ) { scanf("%d%d%d%d%d",&l1,&r1,&l2,&r2,&k); if (!k) { printf("Case %d: 0\n",cs); continue;} if ( r1 > r2 ) swap ( r1, r2 ); r1 /= k; r2 /= k; lint ret = eul[r1]; for ( lint i = r1 + 1; i <= r2; i++ ) { if ( a[i] ) { split ( i ); ret += r1 - In_Exclusion(0,r1); // [1,b]的总个数 - 与y不互素的个数 } else ret += r1; } printf("Case %d: %I64d\n",cs,ret); } return 0; }
容斥之版本二:
#include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; #define lint __int64 #define MAXN 200000 int p[MAXN], a[MAXN], pn; int f[MAXN], fnum; int l1, r1, l2, r2, k; int limit, A; //limit = i表示i元组,A用来计数 lint eul[MAXN]; void prime () { int i, j; pn = 0; memset(a,0,sizeof(a)); for ( i = 2; i < MAXN; i++ ) { if ( a[i] == 0 ) p[pn++] = i; for ( j = 0; j < pn && i * p[j] < MAXN && (p[j] <= a[i] || a[i] == 0); j++ ) a[i*p[j]] = p[j]; } } void split ( int n ) { fnum = 0; while ( a[n] != 0 ) { f[fnum++] = a[n]; lint tmp = n; while ( tmp % a[n] == 0 ) tmp /= a[n]; n = tmp; } if ( n != 1 ) f[fnum++] = n; } void Euler () { eul[1] = 0; for ( int i = 2; i < MAXN; i++ ) { if ( a[i] == 0 ) eul[i] = i - 1; else { int k = i / a[i]; if ( k % a[i] == 0 ) eul[i] = eul[k] * a[i]; else eul[i] = eul[k] * ( a[i] - 1 ); } } eul[1] = 1; for ( int i = 2; i < MAXN; i++ ) eul[i] = eul[i-1] + eul[i]; } void DFS ( int k, int dep, int val ) //dfs用来求组合,k是下标,dep表示组合元素的个数 { if ( dep == limit ) { A += r1 / val; return; } for ( int i = k; i < fnum; i++ ) DFS ( i + 1, dep + 1, val * f[i] ); } int In_Exclusion () { int ret = 0; for ( int i = 1; i <= fnum; i++ ) { A = 0; limit = i; DFS ( 0, 0, 1 ); if ( i & 1 ) ret += A; else ret -= A; } return ret; } int main() { prime(); Euler(); int Case, cs; scanf("%d",&Case); for ( cs = 1; cs <= Case; cs++ ) { scanf("%d%d%d%d%d",&l1,&r1,&l2,&r2,&k); if (!k) { printf("Case %d: 0\n",cs); continue;} if ( r1 > r2 ) swap ( r1, r2 ); r1 /= k; r2 /= k; lint ret = eul[r1]; for ( lint i = r1 + 1; i <= r2; i++ ) { if ( a[i] ) { split ( i ); ret += r1 - In_Exclusion(); } else ret += r1; } printf("Case %d: %I64d\n",cs,ret); } return 0; }