HDU 1695 GCD "Euler function + tolerant principle"

Source: Internet
Author: User
Tags greatest common divisor

GCDTime limit:6000/3000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 6253 Accepted Submission (s): 2291


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 Input
21 3 1 5 11 11014 1 14409 9
Sample Output
Case 1:9case 2:736427Hintfor 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).
Title translation: Known Interval "A, B" and "C,d" and K, how many pairs of gcd (x, y) ==k? where x belongs to "A, B", Y belongs to "c,d", for simplifying the problem, a==c==1, note gcd (2,3) and gcd (3,2) calculate a situation to solve the problem: after conversion, is to ask [1,b/k] and [1,d/k] the number of coprime between the logarithm, If the case does not consider two digital interchange, it is typical of the principle of the repulsion, but because it can not be repeated, the problem has increased the difficulty, if b<d, then we may be divided into two sections to seek, first we use the Euler function, to find how many numbers before N and N coprime, and then use the principle of tolerance to find B + How many coprime are there for any number in the 1 to D and the number before B?
#include <cstdio> #define LL long longconst int max=100005; LL elur[max];//Store the Euler function value of each number int num[max];//the number of elements of the element int p[max][20];//the number of the element void Init () {//filter method to get the number of the element and the Euler function value of each number elur[1    ]=1;                    for (int i=2;i<max;i++) {if (!elur[i]) {for (int j=i;j<max;j+=i) {if (!elur[j])                Elur[j]=j;                elur[j]=elur[j]* (i-1)/I;            P[j][num[j]++]=i; }} Elur[i]+=elur[i-1];    To accumulate}}int nop (int n,int now,int t) {int i;    int ans=0;    for (i=t;i<num[now];i++) Ans+=n/p[now][i]-nop (n/p[now][i],now,i+1); return ans;}    int main () {int t,t,a,b,c,d,k,i,j;    Init ();    scanf ("%d", &t);        for (t=1;t<=t;t++) {scanf ("%d%d%d%d%d", &a,&b,&c,&d,&k);            if (k==0) {printf ("Case%d:0\n", t);        Continue        } b/=k,d/=k;        if (b>d) {a=b;b=d;d=a;}        LL Ans=elur[b];      for (i=b+1;i<=d;i++) {Ans+=b-nop (b,i,0);  } printf ("Case%d:%lld\n", T,ans); } return 0;}

Hdu 1695 GCD "Euler function + repulsion principle"

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.