Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * round 1a problem b
- *
- */
- #include <stdio.h>
- #include <stdlib.h>
- struct barber
- {
- unsigned i;
- unsigned long M, t;
- };
- unsigned long gcd(unsigned long a, unsigned long b)
- {
- while (1)
- {
- if (a == 0)
- return b;
- b %= a;
- if (b == 0)
- return a;
- a %= b;
- }
- }
- unsigned long lcm(unsigned long a, unsigned long b)
- {
- unsigned long tmp = gcd(a, b);
- return tmp ? (a / tmp * b) : 0;
- }
- unsigned long lcmbarber(unsigned c, struct barber* v)
- {
- unsigned long m;
- unsigned i;
- m = 1;
- for (i = 0; i < c; ++i)
- m = lcm(m, v[i].M);
- return m;
- }
- int cmp(const struct barber** b1, const struct barber** b2)
- {
- if ((*b1)->t < (*b2)->t)
- return -1;
- if ((*b1)->t > (*b2)->t)
- return 1;
- if ((*b1)->i < (*b2)->i)
- return -1;
- return 1;
- }
- int main(int argc, char* argv[])
- {
- unsigned T, x;
- scanf(" %u ", &T);
- for (x = 1; x <= T; ++x)
- {
- unsigned long B, N, y, i, m, asd;
- struct barber* b;
- struct barber** bp;
- scanf(" %lu %lu ", &B, &N);
- b = malloc(B * sizeof(struct barber));
- bp = malloc(B * sizeof(struct barber*));
- for (i = 0; i < B; ++i)
- {
- b[i].i = i+1;
- b[i].t = 0;
- scanf(" %lu ", &b[i].M);
- bp[i] = &b[i];
- }
- /* optmize!!1!1ONE! */
- m = lcmbarber(B, b);
- asd = 0;
- for (i = 0; i < B; ++i)
- asd += m / b[i].M;
- while (asd < N)
- N -= asd;
- while (1)
- {
- unsigned long t;
- qsort(bp, B, sizeof (struct barber*), (int(*)(const void*, const void*))&cmp);
- i = 0;
- if (N == 1)
- {
- y = bp[i]->i;
- break;
- }
- t = bp[0]->t;
- for (i = 0; i < B; ++i)
- bp[i]->t -= t;
- for (i = 0; (i < B) && (N > 1) && (bp[i]->t == 0); ++i, --N)
- bp[i]->t = bp[i]->M;
- }
- printf("Case #%u: %lu\n", x, y);
- free(bp);
- free(b);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement