HDU 1695 gcd (Euler's Function + rejection principle)

Source: Internet
Author: User
Tags gcd

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1695

It is a classic question and hard at the same time.

In the range [a, B] and [c, d], take an arbitrary number X and Y to make gcd (x, y) = K. Ask how many pairs (x, y) there are. It can be considered that both A and C are 1, and gcd () are the same.


Because gcd (x, y) = K, then gcd (x/K, Y/K) = 1, that is, finding the range [1, B/K] and [1, such (x, y) pairs in D/K] Make gcd (x, y) = 1. To prevent counting duplication, assume B/k <= D/K first, then when y <= B/K, such logarithm has Euler [Y, when Y> B/K, the prime factor of Y is first decomposed into P1, p2... pi, as long as we can find the number of non-mutual quality with Y in [1, B/K], then the number of mutual quality with y can be subtracted by B. The number of numbers that do not interact with Y is based on the volume exclusion principle, because the multiples of some prime factors are the same, so that the set of integer multiples of PI is Ai, this is the sum of these sets. Evaluate the set and perform DFS Based on the exclusion relationship.

I haven't thought about it for a long time. I wrote it on a blog of a great god. The general idea is to assume that the prime factor is decomposed into P1, P2, and P3. First, the number of p1' multiples is obtained. This is a recursive process, that is, the number of P1 multiples minus the number of P1P2 multiples minus the number of p1p2p3 multiples. Similarly, for P2, the number of P2 multiples minus the number of p2p3 multiples.


# Include <stdio. h> # include <iostream> # include <map> # include <set> # include <list> # include <stack> # include <vector> # include <math. h> # include <string. h> # include <queue> # include <string> # include <stdlib. h> # include <algorithm> // # define ll long # define ll _ int64 # define EPS 1e-12 # define PI ACOs (-1.0) # Define c 240 # define s 20 using namespace STD; const int maxn = 100010; int flag [maxn]; int prime [maxn]; In T Phi [maxn]; vector <int> edge [maxn]; int test, a, B, c, d, K; // Euler's function void Init () based on prime screening () {memset (flag, 0, sizeof (FLAG); prime [0] = 0; Phi [1] = 1; for (INT I = 2; I <maxn; I ++) {If (flag [I] = 0) {Prime [++ prime [0] = I; Phi [I] = I-1 ;} for (Int J = 1; j <= prime [0] & prime [J] * I <maxn; j ++) {flag [prime [J] * I] = 1; if (I % prime [J] = 0) phi [prime [J] * I] = Phi [I] * prime [J]; else Phi [prime [J] * I] = Phi [I] * (Prime [J ]-1) ;}}// a vector stores the prime factor of each number. Void spilt () {for (INT I = 1; I <maxn; I ++) {int TMP = I; for (Int J = 1; prime [J] * prime [J] <= TMP; j ++) {If (TMP % prime [J] = 0) {edge [I]. push_back (prime [J]); While (TMP % prime [J] = 0) tmp/= prime [J];} If (TMP = 1) break ;} if (TMP> 1) edge [I]. push_back (TMP) ;}/// calculates the number of non-Interconnectivity numbers between cur and B smaller than or equal to ll DFS (INT St, int B, int cur) {ll res = 0; for (INT I = sT; I <(INT) edge [cur]. size (); I ++) {int K = B/edge [cur] [I]; Res + = k-DFS (I + 1, K, cur );} return res;} int main () {Init (); spilt (); scanf ("% d", & Test); For (INT item = 1; item <= test; item ++) {scanf ("% d", & A, & B, & C, & D, & K ); if (k = 0 | K> B | K> D) {printf ("case % d: 0 \ n", item); continue ;} b/= K; D/= K; If (B> d) Swap (B, d); LL ans = 0; For (INT I = 1; I <= B; I ++) {ans + = Phi [I];} For (INT I = B + 1; I <= D; I ++) {ans + = B-DFS (0, B, I);} printf ("case % d: % i64d \ n", item, ANS);} return 0 ;}




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.