HDU 1695 GCD (Euler function, repulsion 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): 7357 Accepted Submission (s): 2698


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

Source2008 "Sunline Cup" National Invitational Contest

To find the logarithm of the number of gcd (x, y) =k within [A, b] and [c,d] intervals. Idea: Since it is the logarithm of the number of GCD to K, it is advisable to divide both B and D into the logarithm of the number of gcd (x, y) 1 in the [1,n] and [1,m] intervals. Because A and C can be thought of as 1 in the title, it's easier. For a [1,n] interval, we can calculate the total logarithm with the Euler function. Then the problem can be decomposed into 2:1, the total logarithm is calculated with Euler's function on [1,n]. 2, on [N+1,m], calculate the total logarithm in [1,n], you can use the principle of repulsion.
#include <stdio.h> #include <math.h> #include <string.h> #include <iostream> #include < algorithm> #include <vector> #define MIN (A, b) A<b?a:b#define Max (A, b) a>b?a:b#define Max 100005#define LL __int64using namespace Std; LL sum[max],tot;int p[max][20];int num[max];void init () {sum[1]=1;for (int i=2;i<max;i++) sum[i]=i;for (int i=2;i< max;i++) if (sum[i]==i) for (int j=i;j<max;j+=i) sum[j]=sum[j]/i* (i-1);} void Init2 () {LL x,k,i,j;for (i=1;i<=max;i++) {x=i;k=0;for (j=2;j<=sqrt (i); j + +) {if (x%j==0) {while (x%j==0) x=x/j ;//p[i].push_back (j);p [I][num[i]++]=j;}} if (x>1) p[i][num[i]++]=x;}} ll DFS (int n,int b,int x,int k) {LL ans=0;for (int i=x;i<k;i++) {Ans+=b/p[n][i]-dfs (n,b/p[n][i],i+1,k);} return ans;} int main () {LL t,a,b,c,d,k;int i,j,t;init (); Init2 ();//printf ("%i64d%i64d\n", sum[2],sum[3]); scanf ("%i64d", &t); T =0;while (t--) {tot=0;t++;scanf ("%i64d%i64d%i64d%i64d%i64d", &a,&b,&c,&d,&k);p rintf ("Case%d:" , t), if (k==0) {printf ("0\n"); continue;} b=B/k;d=d/k;int m;m=min (b,d);d =max (b,d); B=m;for (i=1;i<=b;i++) tot=tot+sum[i];   for (i=b+1;i<=d;i++) {//printf ("%d\n", P[i].size ());   Tot+=b-dfs (I,b,0,num[i]);} printf ("%i64d\n", tot);} return 0;}



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

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.