HDU 4746 mophues (Möbius inversion application)

Source: Internet
Author: User


Mophues Time limit:2000/1000 MS (java/others) Memory limit:327670/327670 K (java/others) Total Submission (s): 980 Accepted Submission (s): 376

Problem Description

As we know, any positive integer c (c >= 2) can be written as the multiply of some prime numbers:
C = P1XP2XP3X...XPK
Which p1, p2 ... pk is all prime numbers. For example, if C = all, then:
= 2x2x2x3
Here, p1 = P2 = P3 = 2, P4 = 3, k = 4
Given integers p and c. If K<=p (k is the number of C's prime factors), we call C a lucky number of P.
Now, XXX needs to count the number of pairs (a, b), which 1<=a<=n, 1<=b<=m, and gcd (A, B) is a lucky number of A given P ("gcd" means "greatest common divisor").
Please note this we define 1 as lucky number of any non-negative integers because 1 have no prime factor. InputThe first line of input was an integer q meaning-there is Q test cases.
Then Q lines-follow, each line was a test case and each test case contains three non-negative numbers:n, M and P (n, M, p <= 5x105. Q <=5000). Outputfor each test case, print the number of pairs (a, b), which 1<=a<=n, 1<=b<=m, and gcd (A, a, a, a) are a lucky num ber of P. Sample Input
210 10 010) 10 1
 Sample Output
6393
 SourceACM/ICPC Asia regional Hangzhou Online
Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4746

The main idea: Define Num[i] for the number of all the mass factors after the only decomposition, require NUM[GCD (A, b)] <= P's (A, a) logarithm, wherein 1 <= a <= n,1 <= b <= m

Topic Analysis: This problem T one day, first not to consider 1, the title to the maximum value of P is 5e5, so num[i] maximum is 18, because 2^19 is greater than 5e5
Defines the number of f (d) = GCD (A, b) = d, and g (d) is the number of multiples of gcd (A, b) = d, apparently G (d) is good, that is (n/d) * (M/D)
and g (d) = f (d) + f (2d) + F (3d) + ... The inverse of this type of mo.
F (d) = u (1) g (d) + U (2) g (2d) + U (3) g (3d) + ..., the final answer is Σu (k) G (KD), at this point if the direct enumeration of D, even if the calculation F (d) with a block sum optimization to close to sqrt (n), N*sqrt (n) near 2e8 positive timeout, Therefore, in order to change other ideas, considering the num[i] maximum only 18, we can preprocess the greatest common divisor, and decomposition I after the number of quality factors equal to num[i], according to the formula has Sum[ki][num[i] + + u[k], J=ki, then sum[j][num[ I]] + = u[j/i], note that this is only the contribution of the Möbius function,
As an example,
F (2) = u (1) G (2) + U (2) g (4) + U (3) G (6) + ...
F (3) = U (1) G (3) + U (2) g (6) + U (3) G (9) + ...
The answer is definitely to add them up, notice that G (6) appears two times, can be understood as 6 this number for num[i] = 1 of the case has two contributions, so can be written (U (2) + U (3)) * g (6)
And then processing decomposition I after the number of factors less than or equal to Num[i] The number of scenarios, and finally processing the prefix and the greatest common divisor I as the end of the preprocessing work, the complexity of the NLOGN, on-line calculation with the block sum optimization, the complexity of QSQRT (n), the total complexity is probably nlogn+ QSQRT (N)

#include <cstdio> #include <cstring> #include <algorithm> #define LL long longusing namespace Std;int Const MAX = 5e5 + 5;int N, m, p, Pnum;int Mob[max], Pr[max], sum[max][20];int num[max];bool prime[max];void Mobius () {p    num = 0;    Memset (Prime, true, sizeof (prime));    MOB[1] = 1;            for (int i = 2; i < MAX; i++) {if (Prime[i]) {pr[pnum + +] = i;            Num[i] = 1;        Mob[i] =-1;            } for (int j = 0; J < pnum && I * pr[j] < MAX; J + +) {Num[i * pr[j]] = num[i] + 1;            Prime[i * Pr[j]] = false;                if (i% pr[j] = = 0) {Mob[i * pr[j]] = 0;            Break        } Mob[i * Pr[j]] =-mob[i];    }}}void Init () {Mobius ();    for (int i = 1, i < Max; i++) for (int j = i; J < MAX; J + = i) sum[j][num[i]] + = mob[j/i];         for (int i = 1, i < MAX; i++) for (int j = 1; J < + j + +)   SUM[I][J] + = sum[i][j-1]; for (int i = 1, i < MAX; i++) for (int j = 0; J < + j + +) Sum[i][j] + = Sum[i-1][j];}    ll cal (int l, int r) {ll ans = 0;    if (L > R) Swap (L, R);        for (int i = 1, last = 0; I <= l; i = last + 1) {last = min (l/(l/i), R/(r/i));      Ans + = (ll) (l/i) * (r/i) * (Sum[last][p]-sum[i-1][p]);   } return ans;    int main () {Init ();    int T;    scanf ("%d", &t);        while (T--) {scanf ("%d%d%d", &n, &m, &p); printf ("%lld\n", p > 18?    (LL) n * m:cal (n, m)); }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 4746 mophues (Möbius inversion application)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.