hdu 4451 容斥定理

比预选赛那道简单很多。

给出衣服,裤子,鞋子的件数。有一些衣服和裤子不能搭配,一些裤子和鞋子不能搭配。

求共有多少种方式搭配一身(衣服+裤子+鞋)。

n*m*k - ∑不能与衣服搭配的裤子 - ∑不能与鞋子搭配的裤子 + ∑ 该件裤子既不能与衣服搭配又不能与鞋子搭配

View Code
 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 1010
 4 int f[N][2];
 5 int main(){
 6     char s1[10],s2[10];
 7     int n,m,k,p,a,b;
 8     while(scanf("%d%d%d",&n,&m,&k)!=EOF&&(n||m||k)){
 9         int res=n*m*k;
10         memset(f,0,sizeof(f));
11         scanf("%d",&p);
12         while(p--){
13             scanf("%s %d %s %d",&s1,&a,&s2,&b);
14             if(s1[0]=='c') f[b][0]++;//不能和某件上衣搭配的裤子++
15             else f[a][1]++;//不能和某双鞋子搭配的裤子++
16         }
17         for(int i=1;i<=m;i++){
18             res-=f[i][0]*k;
19             res-=f[i][1]*n;
20             res+=f[i][0]*f[i][1];
21         }
22         printf("%d\n",res);
23     }
24     return 0;
25 }

 

posted @ 2013-02-06 02:44  _sunshine  阅读(247)  评论(0编辑  收藏  举报