UVa 10214 (Möbius inversion or Euler function) Trees in a Wood.

Source: Internet
Author: User

Test instructions

This question and POJ 3090 very similar, asks |x|≤a,|y|≤b to stand in the origin point to see the whole hour the number k, all the hour number is N (removes the origin), asks k/n

Analysis:

There are four visible points on the axis because the number of points visible in each quadrant is the same, so we only ask for the number of points visible in the first quadrant and then X4+4, which is K.

The visible point satisfies gcd (x, y) = 1, so the problem is converted to x∈[1, a], y∈[1, b], and the number of gcd (x, y) = 1.

Analogy HDU 1695 can be used Möbius inversion to do, I also wrote the normal and block acceleration of the two code, turn to find the running time difference is not too much.

1#include <cstdio>2#include <algorithm>3typedefLong LongLL;4 5 Const intMAXN = -;6 intmu[maxn+Ten], vis[maxn+Ten], PRIME[MAXN];7 8 voidMobius ()9 {Tenmu[1] =1; One     intCNT =0; A      for(inti =2; I <= MAXN; ++i) -     { -         if(!Vis[i]) the         { -Mu[i] =-1; -prime[cnt++] =i; -         } +          for(intj =0; J < CNT && (LL) i*prime[j] <= maxn; ++j) -         { +VIS[I*PRIME[J]] =1; A             if(i% prime[j]! =0) Mu[i*prime[j]] =-Mu[i]; at             Else -             { -MU[I*PRIME[J]] =0; -                  Break; -             } -         } in     } -     //compute prefixes and, for block acceleration to      for(inti =2; I <= -; ++i) Mu[i] + = mu[i-1]; +  - } the  * intMain () $ {Panax Notoginseng Mobius (); -     intA, B; the      while(SCANF ("%d%d", &a, &b) = =2) +     { A         if(A = =0&& b = =0) Break; theLL K =0, N = (LL) (A *2+1) * (b*2+1) -1; +         if(A >b) Std::swap (A, b); -          for(inti =1, J; I <= A; i = j +1) $         { $j = Std::min (A/(a/i), b/i)); -K + = (LL) (Mu[j]-mu[i-1]) * (a/i) * (b/i); -         } the         //for (int i = 1; i <= A; ++i) K + = (LL) mu[i] * (a/i) * (b/i); -K = (+ K1)*4;Wuyi  theprintf"%.7f\n", (Double* H/N); -     } Wu  -     return 0; About}
code June

You can also follow the purple book on the idea of Euler functions, because a has a smaller range, so you can count by column. But this method is much slower than the Möbius inversion.

1#include <cstdio>2#include <cmath>3typedefLong LongLL;4 5 intPhiintN)6 {7     intm = sqrt (n +0.5);8     intAns =N;9      for(inti =2; I <= m; ++i)if(n% i = =0)Ten     { OneAns = ans/i * (i-1); A          while(n% i = =0) n/=i; -     } -     if(N >1) ans = ans/n * (n1); the     returnans; - } -  - intgcdintAintb) + { -     returnb = =0? A:GCD (b, a%b); + } A  at intMain () - { -     intA, B; -      while(SCANF ("%d%d", &a, &b) = =2&&a) -     { -ll N = (ll) (A *2+1) * (b*2+1) -1; inLL K =0; -          for(intx =1; x <= A; ++x) to         { +             intK = b/x; -K + = Phi (x) *K; the              for(inty = k*x+1; Y <= b; y++) *                 if(GCD (x, y) = =1) k++; $         }Panax NotoginsengK = (+ K1)*4; -printf"%.7f\n", (DoubleKN); the     } +  A     return 0; the}
code June two

UVa 10214 (Möbius inversion or Euler function) Trees in a Wood.

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.