Guest User

Untitled

a guest
Feb 9th, 2011
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef long long ll;
  6.  
  7. /* Calculates gcd(a,b): the greatest common divisor of a and b */
  8. static ll gcd(ll a, ll b)
  9. {
  10.     while (b)
  11.     {
  12.         ll tmp = a%b;
  13.         a = b;
  14.         b = tmp;
  15.     }
  16.     return a;
  17. }
  18.  
  19. static ll *memo;
  20.  
  21. /* Calculates number of ordered pairs (a,b) such that gcd(a,b) == 1 */
  22. static ll calc2(ll a, ll lim)
  23. {
  24.     ll *m, b;
  25.  
  26.     m = &memo[a];
  27.     if (*m == 0)
  28.     {
  29.         for (b = 0; b < lim; ++b)
  30.         {
  31.             if (gcd(a, b) == 1) ++*m;
  32.         }
  33.     }
  34.     return *m;
  35. }
  36.  
  37. /* Calculates number of ordered triples (a,b,c) such that gcd(a,b,c) == 1 */
  38. static ll calc(ll lim)
  39. {
  40.     ll res = 0, a, b;
  41.  
  42.     memo = calloc(lim, sizeof(ll));
  43.     memo[0] = 1;
  44.     memo[1] = lim;
  45.     for (a = 0; a < lim; ++a)
  46.     {
  47.         res += calc2(a, lim);
  48.         for (b = 0; b < a; ++b)
  49.         {
  50.             res += 2*calc2(gcd(a, b), lim);
  51.         }
  52.     }
  53.     free(memo);
  54.     return res;
  55. }
  56.  
  57. ll main()
  58. {
  59.     ll depth;
  60.     for (depth = 1; depth <= 16; ++depth)
  61.     {
  62.         ll lim = 1 << depth;
  63.         ll cnt = calc(lim);
  64.         ll tot = lim*lim*lim;
  65.         printf("%2lld:  %15lld / %15lld = %.9f\n", depth, cnt, tot, (double)cnt/tot);
  66.     }
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment