UVa Problem Solution: 10050 - Hartals

本文介绍了一种通过简单模拟的方法来计算特定条件下由多个政党发起的哈特尔斯罢工导致的工作日损失总数。该算法避免了使用包含与排除原理所带来的复杂计算,确保了在不超过给定天数范围内准确计算出罢工天数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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:
  1. /*************************************************************************
  2.  * Copyright (C) 2008 by liukaipeng                                      *
  3.  * liukaipeng at gmail dot com                                           *
  4.  *************************************************************************/
  5. /* @JUDGE_ID 00000 10050 C "Hartals" */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <strings.h>
  10.                
  11. int lost(int hp[], int nparty, int nday)
  12. {
  13.   int hartals[3651] = {0};
  14.   int nlost = 0;
  15.   int i, j;
  16.   for (i = 0; i < nparty; ++i) {
  17.     for (j = hp[i]; j <= nday; j += hp[i]) {
  18.       int weekday = j % 7;
  19.       if (weekday != 6 && weekday != 0 && !hartals[j]) {
  20.         hartals[j] = 1;
  21.         nlost += 1;
  22.       }
  23.     }
  24.   }
  25.   return nlost;
  26. }
  27. int main(int argc, char *argv[])
  28. {
  29. #ifndef ONLINE_JUDGE
  30.   char in[256];
  31.   char out[256];
  32.   strcpy(in, argv[0]);
  33.   strcat(in, ".in");
  34.   freopen(in, "r", stdin);
  35.   strcpy(out, argv[0]);
  36.   strcat(out, ".out");
  37.   freopen(out, "w", stdout);
  38. #endif
  39.   int ncase;
  40.   scanf("%d/n", &ncase);
  41.   while (ncase-- > 0) {
  42.     int nday;
  43.     int nparty, i;
  44.     int hp[100];
  45.     scanf("%d/n", &nday);
  46.     scanf("%d/n", &nparty);
  47.     for (i = 0; i < nparty; ++i)
  48.       scanf("%d/n", &hp[i]);
  49.     printf("%d/n", lost(hp, nparty, nday));
  50.   }
  51.   return 0;
  52. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值