Special Practice---(number theory) the inverse of the MO

Source: Internet
Author: User
Tags greatest common divisor

GCD

Time limit:6000/3000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 7026 Accepted Submission (s): 2584


Problem Descriptiongiven 5 integers:a, B, C, D, K, you ' re-find x in a...b, y in c...d that GCD (x, y) = K. GCD (x, y) Me Ans The greatest common divisor of x and Y. Since the number of choices may is very large, you ' re only required to output the total number of different number pairs.
Notice that, (x=5, y=7) and (x=7, y=5) is considered to be the same.

Yoiu can assume a = c = 1 in the all test cases.

Inputthe input consists of several test cases. The first line of the input is the number of the cases. There is no more than 3,000 cases.
Each case contains five integers:a, B, C, D, K, 0 < a <= b <= 100,000, 0 < c <= D <= 100,000, 0 <= K <= 100,000, as described above.

Outputfor each test case, print the number of choices. Use the format in the example.

Sample Input21 3 1 5 11 11014 1 14409 9

Sample outputcase 1:9case 2:736,427 HintFor the first sample input, all the 9 pairs of numbers is (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3; 4), (3, 5).

Source2008 "Sunline Cup" National Invitational Contest questionmeaning:   gives A,b,c,d,k, which makes  a<=x<=b, C<=y<=d  &&gcd (x, y) =k number pairs (x, y) of the logarithm. Limitations:  a=c=1,  0<b,c<=100000;(n1,n2) and (N2, N1) are counted in the same situation,   before doing this problem, first to understand, Möbius function:    Baidu Encyclopedia---The inverse function of the Möbius, known as the function, the inverse function ..... You can see the address above    for this problem, we can analyze:  
1  Set F (k) for gcd (x, y) =k number pairs (x, y) logarithm, we require f (1)2 set F (k) is gcd (x, y) is a multiple of the number of K (x, y) logarithm, can think of f (k) = Floor (b/k) *floor (d/k),
F (k) =e[b/k]*[d/k]*mu[k];

3 the Möbius was reversed by: 4 Order lim=min (b/k,d/k)5 F (1) =mu[1]*f (1) + mu[2 ]*f[2] + ... + mu[lim]*F (Lim)6 because (N1,N2) and (N2,N1) are counted in the same situation, the final result is also to reduce the duplication of the situation.

Code:

1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <algorithm>6 #define_llint Long Long7 using namespacestd;8 Const intmaxn=100010;9 BOOLJUD[MAXN];Ten intMU[MAXN], PRIME[MAXN]; One  A voidMobius () { -   intCnt=0; -memset (Jud,0,sizeofJud); themu[1]=1;//d=1 -    for(intI=2; I&LT;MAXN; i++){ -  -       if(!Jud[i]) { +mu[i]=-mu[1];//1*p -prime[cnt++]=i; +       } A      for(intj=0; J&LT;CNT&AMP;&AMP;I*PRIME[J]&LT;MAXN; J + + ){ at  -jud[i*prime[j]]=1; -         if(i%prime[j]==0){ -mu[i*prime[j]]=0;//²»ê绥òìµäëøêý -              Break; -}Else{ inMu[i*prime[j]]=-1*Mu[i]; -         } to     } +   } -   return ; the } *  $_llint Solve (intNintm) {Panax Notoginseng  -_llint res=0; the     for(intI=1; I<=n; i++){ +res+= (_llint) (m/i) * (n/i) *Mu[i]; A    } the    returnRes; + } -  $ intMain () $ { -   intt,a,b,c,d,k; - Mobius (); thescanf"%d",&T); -    for(intI=1; i<=t; i++.){Wuyiscanf"%d%d%d%d%d",&a,&b,&c,&d,&k); the     if(0==k) -     { Wuprintf"Case %d:0\n", i); -         Continue; About     } $B/=k, d/=K; -      if(b>d) Swap (b, d); -_llint ans1=solve (b,d); -_llint ans2=solve (b,b); Aprintf"Case %d:%lld\n", i,ans1-ans2/2); +   } the  return 0; -}

Special Practice---(number theory) the inverse of the MO

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.