At the first look, the hartals can be counted with the principle of inclusion and exclusion. However, this may cause the calculations to expand to O(2^numberof(parties)). In another way, simply simulation will also do the job.
Code:
- /*************************************************************************
- * Copyright (C) 2008 by liukaipeng *
- * liukaipeng at gmail dot com *
- *************************************************************************/
- /* @JUDGE_ID 00000 10050 C "Hartals" */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <strings.h>
- int lost(int hp[], int nparty, int nday)
- {
- int hartals[3651] = {0};
- int nlost = 0;
- int i, j;
- for (i = 0; i < nparty; ++i) {
- for (j = hp[i]; j <= nday; j += hp[i]) {
- int weekday = j % 7;
- if (weekday != 6 && weekday != 0 && !hartals[j]) {
- hartals[j] = 1;
- nlost += 1;
- }
- }
- }
- return nlost;
- }
- int main(int argc, char *argv[])
- {
- #ifndef ONLINE_JUDGE
- char in[256];
- char out[256];
- strcpy(in, argv[0]);
- strcat(in, ".in");
- freopen(in, "r", stdin);
- strcpy(out, argv[0]);
- strcat(out, ".out");
- freopen(out, "w", stdout);
- #endif
- int ncase;
- scanf("%d/n", &ncase);
- while (ncase-- > 0) {
- int nday;
- int nparty, i;
- int hp[100];
- scanf("%d/n", &nday);
- scanf("%d/n", &nparty);
- for (i = 0; i < nparty; ++i)
- scanf("%d/n", &hp[i]);
- printf("%d/n", lost(hp, nparty, nday));
- }
- return 0;
- }