Bzoj 2301 [Haoi2011]problem B (tolerance + Momo inversion + block optimization details)

Source: Internet
Author: User
Tags greatest common divisor


2301: [Haoi2011]problem b Time limit:50 Sec Memory limit:256 MB
submit:2096 solved:909
[Submit] [Status] [Discuss] Descriptionfor the given n queries, how many pairs (x, y) are asked each time, A≤x≤b,c≤y≤d is satisfied, and gcd (x, y) = K,GCD (x, y) function is the greatest common divisor of x and Y. Input

The first line is an integer n, and the next n rows are five integers per line, representing A, B, C, D, K, respectively.

Output

A total of n rows, one integer per line representing the number of pairs (x, y) that satisfy the requirement

Sample Input2

2 5 1) 5 1

1 5 1) 5 2
Sample Output14

3
HINT100% of the data meet: 1≤n≤50000,1≤a≤b≤50000,1≤c≤d≤50000,1≤k≤50000


Title Link: http://www.lydsy.com/JudgeOnline/problem.php?id=2301


Topic Analysis: First n is 50,000, so every time even the calculation of O (n) is also O (n^2), so for each operation we have to make time complexity less than O (n), test instructions very clear

The original variant is first:

a/k <= x/k <= b/k,c/k <= y/k <= d/k,gcd (x/k, y/k) = 1, cal (b/k,d/k) 1 <= x/k <= b /k,1 <= y/k <= d/k When the number of satisfied conditions to be removed, then according to the principle of repulsion

ans = cal (b/k,d/k)-Cal ((A-1)/k,d/k)-Cal ((c-1)/k,b/k) + cal ((A-1)/K, (c-1)/k), because 1~a-1 and 1~c-1 are not in our The scope of the request, and then minus the interval minus two times, so to add one, next look at the CAL function, here to use the block sum, if not optimized, is directly enumerate the number of the convention ans + = mob[i] * (l/i) * (r/i) But this will time out, taking into account the characteristics can not be divisible, In a very large interval (l/i) and (r/i) values are the same, for a simple example, L = 10,r = 11 Then you can see I from 6 to 10,l/i and r/i values are 1, so consider the block sum, starting from I the longest equal interval length is min (l/(l/i) , R/(r/i)), if this is not understood, such as L/(l/i), set l/i = P,p represents the value of the division, then l/p is the number of the value of P is divisible from the beginning of I

#include <cstdio> #include <cstring> #include <algorithm>using namespace std;int const MAX = 50005;int P    [MAX], Mob[max], Sum[max];bool prime[max];int A, B, C, D, K;void Mobius () {int pnum = 0;    Memset (Prime, true, sizeof (prime));    memset (sum, 0, sizeof (sum));    MOB[1] = 1;    SUM[1] = 1;            for (int i = 2; i < MAX; i++) {if (Prime[i]) {p[pnum + +] = i;        Mob[i] =-1;            } for (int j = 0; J < pnum && I * p[j] < MAX; J + +) {Prime[i * P[j]] = false;                if (i% p[j] = = 0) {Mob[i * p[j]] = 0;            Break        } Mob[i * P[j]] =-mob[i];    } Sum[i] = Sum[i-1] + mob[i];    }}int cal (int l, int r) {if (L > R) Swap (L, R);    int ans = 0;        for (int i = 1, last = 0; I <= l; i = last + 1) {last = min (l/(l/i), R/(r/i));    Ans + = (l/i) * (r/i) * (Sum[last]-sum[i-1]); } rEturn ans;}    int main () {Mobius ();    int n;    scanf ("%d", &n);        while (n-) {scanf ("%d%d%d%d", &a, &b, &c, &d, &k);        int ans = 0;        Ans + = cal (b/k, d/k);        Ans-= cal ((A-1)/K, d/k);        Ans-= cal ((c-1)/K, b/k);        Ans + = cal ((A-1)/K, (c-1)/k);       printf ("%d\n", ans); }}


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

Bzoj 2301 [Haoi2011]problem B (tolerance + Momo inversion + block optimization details)

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.