AtCoder Grand Contest 017 A

Problem Statement

There are N bags of biscuits. The i-th bag contains Ai biscuits.

Takaki will select some of these bags and eat all of the biscuits inside. Here, it is also possible to select all or none of the bags.

He would like to select bags so that the total number of biscuits inside is congruent to P modulo 2. How many such ways to select bags there are?

Constraints

  • 1≤N≤50
  • P=0 or 1
  • 1≤Ai≤100

Input

Input is given from Standard Input in the following format:

N P
A1 A2 ... AN

Output

Print the number of ways to select bags so that the total number of biscuits inside is congruent to P modulo 2.


Sample Input 1

Copy
2 0
1 3

Sample Output 1

Copy
2

There are two ways to select bags so that the total number of biscuits inside is congruent to 0 modulo 2:

  • Select neither bag. The total number of biscuits is 0.
  • Select both bags. The total number of biscuits is 4.

Sample Input 2

Copy
1 1
50

Sample Output 2

Copy
0

Sample Input 3

Copy
3 0
1 1 1

Sample Output 3

Copy
4

Two bags are distinguished even if they contain the same number of biscuits.


Sample Input 4

Copy
45 1
17 55 85 55 74 20 90 67 40 70 39 89 91 50 16 24 14 43 24 66 25 9 89 71 41 16 53 13 61 15 85 72 62 67 42 26 36 66 4 87 59 91 4 25 26

Sample Output 4

Copy
17592186044416
题意:数组中选出一些数字,相加求和%2==p,有多少种选取方式,可以一个也不选
解法:
1 首先数组统统%2处理
2 p=0 说明可以选取0 或者偶数个1,那么C(0的总数,选取0的个数)*(1的总数,选取1的个数)
3 p=1 说明可以选取0加奇数个1,一样的公式
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int num[100];
 4 int p,n;
 5 long long C(int n,int m)
 6 {
 7     if(n<m) return 0;
 8     long long ans=1;
 9     for(int i=0;i<m;i++) ans=ans*(long long)(n-i)/(long long)(i+1);
10     return ans;
11 }
12 long long A(int n,int m)
13 {
14     if(n<m) return 0;
15     long long ans=1;
16     for(int i=0;i<m;i++) ans*=(long long)(n-i);
17     return ans;
18 }
19 int main(){
20  
21     int Numz=0;
22     int Numo=0;
23     cin>>n>>p;
24     for(int i=1;i<=n;i++){
25         cin>>num[i];
26         num[i]%=2;
27         if(num[i]==0){
28             Numz++;
29         }else{
30             Numo++;
31         }
32     }
33     long long ans=0;
34     if(p==0){
35         for(int i=0;i<=Numz;i++){
36             long long pos=C(Numz,i);
37             for(int j=0;j<=Numo;j+=2){
38                 long long base=C(Numo,j);
39                 ans+=(pos*base);
40             }
41         }
42         cout<<ans<<endl;
43     }else if(p==1){
44         for(int i=0;i<=Numz;i++){
45             long long pos=C(Numz,i);
46             for(int j=1;j<=Numo;j+=2){
47                 long long base=C(Numo,j);
48                 ans+=(pos*base);
49             }
50         }
51         cout<<ans<<endl;
52     }
53     return 0;
54 }
原文地址:https://www.cnblogs.com/yinghualuowu/p/7143671.html