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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值