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 }
复制代码

 

posted @   樱花落舞  阅读(403)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示