Reference: http://hi.baidu.com/myzone2009/blog/item/9a3e7e1895046574dab4bdff.html
Question:
Give you a, B, c, d, k; find such a Team x, y, so that gcd (x, y) = k, and x in [1, B], yε [1, d]: ask how many pairs meet the requirements (x, y ).
------------------------------------------------------------------------------
Solution:
Refresh principle + screening method + Euler's function.
<Refresh principle>
For example, five letters a, B, c, d, and e are used to form a word with a length of 5, which must not contain a, B, and c, ask how many words meet the requirements.
Let me scold you for understanding this question. All the five letters are in a combination of U = 5 ^ 5. Then, set A [x] to indicate the number of words that do not contain the letter x, where x = {a, B, c }. If A [x, y] is set, it indicates the number of permutation and combination of words without letters x and y, where x, y = {a, B, c }......
The number of words that meet the requirements is ANS = U-A [a]-A [B]-A [c] + A [a, B] + A [, c] + A [B, c]-A [a, B, c].
(1) the question must be gcd (x, y) = k, which can be converted to gcd (x/k, y/k) = gcd (n, m) = 1. Correspondingly, the range of x and y in the question can also be converted to [1, B/k] (the following is represented by [1, bb]) and [1, d/k] ([1, dd ).
(2) In the question, x and y are unordered. Therefore, we can assume B <= d.
(3) enumerate every m in [1, dd], and find the number of m mutual quality in [1, min (dd, m-1.
① When m is <= bb, you can use the Euler's function to obtain the number of numbers that are less than or equal to m and are mutually compatible with m.
② When bb is <= m <= dd, it is determined by the principle of rejection... For m, in [1, bb], we can calculate the number of the total number of mutual quality. ans = bb-we can find the number of non-mutual quality.
The number of non-interphysical elements = Σ B/(a prime factor of m)-Σ B/(product of two prime factors of m) + Σ (the product of the three prime factors of B/m )······
# Include <iostream> # include <cstdio> # include <cstring> # include <cstdlib> # include <cmath> # include <algorithm> # include <ctime> # include <map> # include <vector> using namespace std; const int N = 100009; typedef _ int64 i64; i64 euler [N + 5]; // euler [I] saves the euler's function value of I vector <int> prime [N + 5]; // prime [I] saves the I prime factor void EulerFunc () // obtain the euler's function values from 1 to N and sieve out the prime factor {euler [1] = 1; for (int I = 2; I <= N; I ++) {if (euler [I] = 0) {for (int j = I; j <= N; j + = I) {if (euler [j] = 0) euler [j] = j; euler [j] = euler [j] * (I-1)/I; prime [j]. push_back (I) ;}}} i64 dfs (int st, int B, int now) // obtain the number of non-mutex values between 1 and B {i64 res = 0; for (int I = st; I <prime [now]. size (); I ++) {res + = B/prime [now] [I]-dfs (I + 1, B/prime [now] [I], now);} return res;} int main () {int Case, a, B, c, d, k; scanf ("% d", & Case ); eulerFunc (); for (int ii = 1; ii <= Case; ii ++) {scanf ("% d", &, & B, & c, & d, & k); if (k = 0) {printf ("Case % d: 0 \ n", ii); continue ;} b/= k; d/= k; if (B> d) swap (B, d); i64 ans = 0; for (int I = 1; I <= B; I ++) ans + = euler [I]; for (int I = B + 1; I <= d; I ++) {ans + = B-dfs (0, b, I);} printf ("Case % d: % I64d \ n", ii, ans );}}