Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.80 KB | None | 0 0
  1. /*
  2.  * round 1a problem b
  3.  *
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. struct barber
  10. {
  11.     unsigned i;
  12.     unsigned long M, t;
  13. };
  14.  
  15. unsigned long gcd(unsigned long a, unsigned long b)
  16. {
  17.     while (1)
  18.     {
  19.         if (a == 0)
  20.             return b;
  21.         b %= a;
  22.         if (b == 0)
  23.             return a;
  24.         a %= b;
  25.     }
  26. }
  27.  
  28. unsigned long lcm(unsigned long a, unsigned long b)
  29. {
  30.     unsigned long tmp = gcd(a, b);
  31.     return tmp ? (a / tmp * b) : 0;
  32. }
  33.  
  34. unsigned long lcmbarber(unsigned c, struct barber* v)
  35. {
  36.     unsigned long m;
  37.     unsigned i;
  38.    
  39.     m = 1;
  40.     for (i = 0; i < c; ++i)
  41.         m = lcm(m, v[i].M);
  42.  
  43.     return m;
  44. }
  45.  
  46. int cmp(const struct barber** b1, const struct barber** b2)
  47. {
  48.     if ((*b1)->t < (*b2)->t)
  49.         return -1;
  50.     if ((*b1)->t > (*b2)->t)
  51.         return 1;
  52.     if ((*b1)->i < (*b2)->i)
  53.         return -1;
  54.     return 1;
  55. }
  56.  
  57. int main(int argc, char* argv[])
  58. {
  59.     unsigned T, x;
  60.  
  61.     scanf(" %u ", &T);
  62.  
  63.     for (x = 1; x <= T; ++x)
  64.     {
  65.         unsigned long B, N, y, i, m, asd;
  66.         struct barber* b;
  67.         struct barber** bp;
  68.  
  69.         scanf(" %lu %lu ", &B, &N);
  70.         b = malloc(B * sizeof(struct barber));
  71.         bp = malloc(B * sizeof(struct barber*));
  72.  
  73.         for (i = 0; i < B; ++i)
  74.         {
  75.             b[i].i = i+1;
  76.             b[i].t = 0;
  77.             scanf(" %lu ", &b[i].M);
  78.             bp[i] = &b[i];
  79.         }
  80.  
  81.         /* optmize!!1!1ONE! */
  82.         m = lcmbarber(B, b);
  83.         asd = 0;
  84.  
  85.         for (i = 0; i < B; ++i)
  86.             asd += m / b[i].M;
  87.  
  88.         while (asd < N)
  89.             N -= asd;
  90.  
  91.         while (1)
  92.         {
  93.             unsigned long t;
  94.             qsort(bp, B, sizeof (struct barber*), (int(*)(const void*, const void*))&cmp);
  95.  
  96.             i = 0;
  97.  
  98.             if (N == 1)
  99.             {
  100.                 y = bp[i]->i;
  101.                 break;
  102.             }
  103.  
  104.             t = bp[0]->t;
  105.  
  106.             for (i = 0; i < B; ++i)
  107.                 bp[i]->t -= t;
  108.  
  109.             for (i = 0; (i < B) && (N > 1) && (bp[i]->t == 0); ++i, --N)
  110.                 bp[i]->t = bp[i]->M;
  111.         }
  112.  
  113.         printf("Case #%u: %lu\n", x, y);
  114.  
  115.         free(bp);
  116.         free(b);
  117.     }
  118.  
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement