hdu 4451 Dressing 衣服裤子鞋 简单容斥

Dressing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3735    Accepted Submission(s): 1681

Problem Description

Wangpeng has N clothes, M pants and K shoes so theoretically he can have N×M×K different combinations of dressing. One day he wears his pants Nike, shoes Adiwang to go to school happily. When he opens the door, his mom asks him to come back and switch the dressing. Mom thinks that pants-shoes pair is disharmonious because Adiwang is much better than Nike. After being asked to switch again and again Wangpeng figure out all the pairs mom thinks disharmonious. They can be only clothes-pants pairs or pants-shoes pairs. Please calculate the number of different combinations of dressing under mom’s restriction.

Input

There are multiple test cases. For each case, the first line contains 3 integers N,M,K(1≤N,M,K≤1000) indicating the number of clothes, pants and shoes. Second line contains only one integer P(0≤P≤2000000) indicating the number of pairs which mom thinks disharmonious. Next P lines each line will be one of the two forms“clothes x pants y” or “pants y shoes z”. The first form indicates pair of x-th clothes and y-th pants is disharmonious(1≤x≤N,1 ≤y≤M), and second form indicates pair of y-th pants and z-th shoes is disharmonious(1≤y≤M,1≤z≤K). Input ends with “0 0 0”. It is guaranteed that all the pairs are different.

Output

For each case, output the answer in one line.

Sample Input

2 2 2
0
2 2 2
1
clothes 1 pants 1
2 2 2
2
clothes 1 pants 1
pants 1 shoes 1
0 0 0

Sample Output

8
6
5

Source

2012 Asia JinHua Regional Contest

题意:给你N件衣服M条裤子K双鞋子,其中一些衣服跟裤子不能组合在一起或者是裤子跟鞋子不能组合在一起,在这种情况下,求出合理的组合套数。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int  inf =0x7f7f7f7f;
const double pi=acos(-1);
const int maxn=40000;

char s1[20],s2[20];
int x,y,f1[1005],f2[1005];
int main()
{
    int n,m,k,p;
    while(~scanf("%d %d %d",&n,&m,&k)&&(n||m||k))
    {
        scanf("%d",&p);
        MM(f1,0);MM(f2,0);
        int cnt1=0,cnt2=0;
        for(int i=1;i<=p;i++)
        {
            scanf("%s %d %s %d",s1,&x,s2,&y);
            if(s1[0]==‘c‘) {cnt1++;f1[y]++;}
            else {cnt2++;f2[x]++;}
        }
        int res=cnt1*k+n*cnt2;
        for(int i=1;i<=m;i++)
            res-=f1[i]*f2[i];
        printf("%d\n",n*m*k-res);
    }
    return 0;
}

分析:比较基础的一道容斥,题目是求合法的搭配,那么我们转化为求不合法的搭配,首先统计
单个的一对衣服和鞋或鞋和裤子的搭配,可以容易求出其对应的不合理的搭配数之和,但是

中间可能会有重复算了的,因为可能一种搭配中衣服-裤子与裤子-鞋都不符合,那么需要再减去

这种重复的,只要统计下一种裤子同时被衣服和鞋共用的次数就好

时间: 2024-10-19 11:18:29

hdu 4451 Dressing 衣服裤子鞋 简单容斥的相关文章

HDU 4059 The Boss on Mars-矩阵+容斥

错了29遍,终成正果..... 根据题意,很容易的可以想到容斥. 然后的问题就是如何求 sum(n)=1^4+2^4+3^4+....+n^4; 有三种道路: 很显然:1^4+2^4+3^4+....+n^4=(n^5)/5+(n^4)/2+(n^3)/3-n/30: 则1,用java的大数去敲这个的代码. 2,用c++敲,但是用到分数取模,求逆元. 3,用c++敲,但是不用这个公式,用矩阵去构造sum(n). 我用的是第三种.但是第三种有的缺陷,就是时间复杂度有点高. 接下来的问题就是如何优化

【HDU 4451 Dressing】水题,组合数

有衣服.裤子.鞋数量分别为n,m,k,给出p对不和谐的衣-裤或裤-鞋搭配,问一共有多少种和谐的衣裤鞋的搭配. 全部的组合有Cn1Cm1Ck1种. 设p对中有p1对衣-裤,p2对裤-鞋,则不和谐的搭配共有p1*Ck1+p2*Cn1种,但有被重复计算两次的搭配共p3对,它们引用了同一裤.设裤 i 在p1被引用 li 次,在p2被引用 ri 次,则p3=∑(1*Cli1Cri1).所以答案为n*m*k-p1*k-p2*n+p3 1 #include <cstdio> 2 #include <c

HDU 4451 Dressing

先从衣服处理到裤子,在从裤子处理到鞋子 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <queue> #include <vector> #include <cmath> #include <map> #include <stri

hdu 3682 10 杭州 现场 C - To Be an Dream Architect 简单容斥

C - To Be an Dream Architect Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3682 Appoint description:  System Crawler  (2014-11-09) Description The “dream architect” is the key role in a team o

Codeforces Round #341 (Div. 2) C. Wet Shark and Flowers(简单容斥)

题目链接:点击打开链接 题意:有n个人围坐成一圈,每个人可以从a[i].l 到 a[i].r里选一个数,如果相邻两个数的乘积能整除p,那么就奖励他们一人1000,求所得钱的总和的期望. 思路:既然求期望, 先求概率. 显然是要求每组相邻两个人的值乘积能否被p整除, 可以很容易知道在区间里有多少个数不能被p整除, 正难则反, 就能算出相邻两个有多少种组合不能被p整除, 那么也就很容易算出每组可以被p整除的概率, 乘上2000就是每组的期望, 期望加和就是总的期望. 细节参见代码: #include

GuGuFishtion HDU - 6390 (欧拉函数,容斥)

GuGuFishtion \[ Time Limit: 1500 ms\quad Memory Limit: 65536 kB \] 题意 给出定义\(Gu(a, b) = \frac{\phi(ab)}{\phi(a)\phi(b)}\) 求出\(\sum_{a=1}^{m}\sum_{b=1}^{n}Gu(a,b) (mod p)\) 思路 首先对于欧拉函数,我们知道欧拉函数的朴素式子为:\(\phi(n) = n*(1-\frac{1}{p1})*(1-\frac{1}{p2}) * ..

HDU 1796 How many integers can you find (容斥)

题意:给定一个数 n,和一个集合 m,问你小于的 n的所有正数能整除 m的任意一个的数目. 析:简单容斥,就是 1 个数的倍数 - 2个数的最小公倍数 + 3个数的最小公倍数 + ...(-1)^(n+1) * n个数的最小公倍数. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdl

HDU 6053 TrickGCD 莫比乌斯函数/容斥/筛法

题意:给出n个数$a[i]$,每个数可以变成不大于它的数,现问所有数的gcd大于1的方案数.其中$(n,a[i]<=1e5)$ 思路:鉴于a[i]不大,可以想到枚举gcd的值.考虑一个$gcd(a_1,a_2,a_3…a_n)=d$,显然每个$a_i$的倍数都满足,有$\frac{a_i}{d}$种方案 那么一个d对答案的贡献为\[\prod_{i=1}^{min(a)}{\lfloor\frac{a_i}{d}\rfloor}    \] 但是所有的d计入会有重复情况,考虑容斥,对d进行素数分

HDU 1796 How many integers can you find (lcm + 容斥)

How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5526    Accepted Submission(s): 1584 Problem Description Now you get a number N, and a M-integers set, you should