Bzoj 2301 "HAOI2011" Problem B

Source: Internet
Author: User

Description for the given n queries, how many pairs (x, y) are asked each time, satisfy A≤x≤b,c≤y≤d, and gcd (x, y) = K,GCD (x, y) functions are 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

HINT

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

After so long finally wrote the Möbius inversion of the introductory question tat ...

This problem mainly uses a property of the Möbius function, for any positive integer $n$, there are: $$\sum_{d|n} \mu (d) = \left \{\begin{aligned} &1 & (N=1) \ &0 & (n>1 ) \end{aligned} \right.$$

So $[GCD (i,j) =1]$ This expression can be expressed as: $$\SUM_{D|GCD (I,J)}\mu (d) $$

So Möbius inversion is especially useful for dealing with problems such as $GCD (x, y) =1$.

Möbius inversion stamp here

Refer to the Huang long blog (I have turned to the previous page ...). If you want to see the code, please turn to the next article)

After all, I just looked at Huang long blog made out of ...

Paste the following code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Cmath> #define FILE (s) freopen (S ".", "R", stdin), Freopen (S ". Out", "w", stdout) #define MAXN 50010using namespace std; typedef long LONG Llg;int t,a,b,c,d,k,ls;int mu[maxn],s[maxn],w[maxn];bool vis[maxn];int getint () {int W=0;bool Q=0;char C=getchar (); while (c> ' 9 ' | | c< ' 0 ') &&c!= '-') C=getchar (), if (c== '-') C=getchar (), Q=1;while (c>= ' 0 ' &&c<= ' 9 ') w=w*10+c-' 0 ', C=getchar (); return q?-w:w;} void Get () {//Linear sieve primes with the mu[1]=1;for function (int i=2;i<maxn;i++) {if (!vis[i]) s[++ls]=i,mu[i]=-1;for (int j=1;j<=ls && s[j]*i<maxn;j++) {vis[s[j]*i]=1;if (i%s[j]) Mu[s[j]*i]=-mu[i];else{mu[s[j]*i]=0;break;}}} for (int i=1;i<maxn;i++) w[i]=w[i-1]+mu[i];} LLG F (int n,int m) {//Find x in [1,n], y in [1,m] answer LLG ans=0;if (n>m) swap (n,m); for (int i=1,nt;i<=n;i=nt+1) {nt=min (n/(n /i), m/(m/i)), ans+= (LLG) (W[nt]-w[i-1]) * (LLG) (n/i) * (LLG) (m/i);} return ans;} int main () {File ("a"); get (); T=getint (); while (t--) {a=getint (); B=getint (); C=getint ();d =getint (); K=getint (); a--; c--;//note border a/=k; b/=k; c/=k; d/=k;printf (" %lld\n ", F (b,d)-F (a,d)-F (b,c) +f (a,c));//conversion to prefix and repulsion solution}return 0;}

  

Bzoj 2301 "HAOI2011" Problem B

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.