HDU1695-GCD (number theory-Euler's function-tolerance)

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): 5454 Accepted Submission (s): 1957


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).

Test instructions: Number of logarithms (1,a) and (1,b) two interval conventions with K
Train of thought: will be a B, respectively, can be converted to (1,a/k) and (1,b/k) two interval 22 coprime number, can first use Euler function to find out (1,a) 22 coprime number, (a+1,b) can decompose factorization, because the number of factorization is up to 7 can be calculated using the principle of tolerance.
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <string > #include <algorithm> #include <queue>using namespace std;const int maxn = 10000+10;const int maxxn = 100000 +10;typedef Long Long ll;int a,b,gcd;ll ans;bool isprime[maxn];ll mindiv[maxxn],phi[maxxn],sum[maxxn];vector<int    > Prime,cnt[maxxn],digit[maxxn];void getprime () {prime.clear ();    memset (isprime,1,sizeof isprime);            for (int i = 2;i < MAXN; i++) {if (Isprime[i]) {prime.push_back (i);            for (int j = i*i; j < Maxn; j+=i) {isprime[j] = 0;    }}}}void Getphi () {for (ll i = 1; i < maxxn; i++) {mindiv[i] = i;                } for (ll i = 2, i*i < maxxn; i++) {if (mindiv[i]==i) {for (int j = i*i; J < maxxn; J + = i) {            MINDIV[J] = i;    }}} Phi[1] = 1;    SUM[1] = 1; for (ll i = 2; i < maxxn; i++) {Phi[i] = Phi[i/mindiv[i]];        if ((I/mindiv[i])%mindiv[i]==0) {Phi[i] *= mindiv[i];        }else{Phi[i] *= mindiv[i]-1;    } Sum[i] = phi[i]+sum[i-1];        }}void Getdigit () {for (ll i = 1; i < maxxn; i++) {int x = i; for (int j = 0; J < prime.size () &&x >= prime[j]; j + +) {if (x%prime[j]==0) {digit[i].                Push_back (Prime[j]);                int t = 0;                    while (x%prime[j]==0) {t++;                x/= prime[j];            } cnt[i].push_back (t);            }} if (X!=1) {digit[i].push_back (x);        Cnt[i].push_back (1);    }}}int Main () {getprime ();    Getphi ();    Getdigit ();    int ncase,t=1;    Cin >> Ncase;        while (ncase--) {int t1,t2;        scanf ("%d%d%d%d%d", &AMP;T1,&AMP;A,&AMP;T2,&AMP;B,&AMP;GCD);            if (gcd==0) {printf ("Case%d:0\n", T++,ans);        Continue }else{if (a> B) Swap (A, a);            A/= gcd,b/= gcd;            ans = sum[a];                for (ll i = a+1; I <= b; i++) {int d = digit[i].size ();                int t = 0;                Vector<int> di;                    for (int k = 1; k < (1&LT;&LT;D); k++) {di.clear (); for (int f = 0; f < D; f++) {if (k& (1<<f)) {Di.push_back (digi                        T[I][F]);                    }} int ji = 1;                    for (int f = 0; F < di.size (); f++) {ji *= di[f];                    } if (Di.size ()%2==0) {T-= A/ji;                    }else{T + = A/ji;            }} ans + = a-t;            } printf ("Case%d:", t++);        cout<<ans<<endl; }} return 0;}


HDU1695-GCD (number theory-Euler's function-tolerance)

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.