Bzoj 1101: [Poi2007]zap (Möbius inversion)

Source: Internet
Author: User

Answer =∑[GCD (x, y) = d] (1 <= x <= A, 1 <= y <= b).

Make a ' = A/d, b ' = b/d, simplify to get:

Answer =σ μ (t) * ⌊a '/t⌋*⌊b '/t⌋

⌊a '/t⌋ is equal to a continuous interval, ⌊b '/t⌋ the same, and the number is a square root level

so get out μ is prefixed and then chunked processed.

----------------------------------------------------------------

#include <bits/stdc++.h>using namespace std;const int MAXN = 50009;bool CHECK[MAXN];int MU[MAXN], prime[maxn], N = 0; void init () {memset (check, false, sizeof check);mu[1] = 1;for (int i = 2; i < MAXN; i++) {if (!check[i]) {Mu[i] =-1;prime[n++] = i;}For (int j = 0; J < N && I * prime[j] < MAXN; J + +) {check[i * Prime[j]] = true;if (i% prime[j])mu[i * Prime[j]] =-mu[i];else {mu[i * Prime[j]] = 0;Break ;}}}for (int i = 1; i < MAXN; i++)Mu[i] + = mu[i-1];}void Work (int a, int b) {if (a > B) Swap (A, b);int ans = 0;for (int L = 1; L <= A; l++) {int R = min (A/(A/L), b/(B/L));ans + = (Mu[r]-mu[l-1]) * (a/l) * (B/L);L = R;}printf ("%d\n", ans);}int main () {init ();int T;cin >> T;While (t--) {int A, b, C;scanf ("%d%d%d", &a, &b, &c);Work (A/C, b/c);}return 0;}

----------------------------------------------------------------

1101: [Poi2007]zap time limit: ten Sec Memory Limit: 162 MB
Submit: 1599 Solved: 554
[Submit] [Status] [Discuss] Description

FGD is cracking a cipher, and he needs to answer a number of similar questions: for a given integer, a, B, and D, how many positive integers are to X, Y, X<=a,y<=b, and gcd (x, y) =d. As a classmate of FGD, FGD hopes to get your help.

Input

The first line contains a positive integer n, which indicates that there are altogether n groups of queries. (1<=n<= 50000) next n rows, each line represents a query, three positive integers per line, respectively, A,b,d. (1<=d<=a,b<=50000)

Output

For each set of queries, output to the output file zap.out a positive integer that represents the integer logarithm that satisfies the condition.

Sample Input2
4 5 2
6 4 3Sample Output3
2HINT

For the first set of queries, an integer pair that satisfies the condition has (2,2), (2,4), (4,2). For the second set of queries, an integer pair that satisfies the condition has (6,3), (3,3).

Source

Bzoj 1101: [Poi2007]zap (Möbius inversion)

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.