Por Costel the Pig has received a royal invitation to the palace of the Egg-Emperor of Programming, Azerah. Azerah had heard of the renowned pig and wanted to see him with his own eyes. Por Costel, having arrived at the palace, tells the Egg-Emperor that he looks "tasty". Azerah feels insulted (even though Por Costel meant it as a compliment) and, infuratied all the way to his yolk, threatens to kill our round friend if he doesn't get the answer to a counting problem that he's been struggling with for a while
Given an array of numbers, how many non-empty subsequences of this array have the sum of their numbers even ? Calculate this value mod
(Azerah won't tell the difference anyway)
Help Por Costel get out of this mischief!
The file azerah.in will contain on its first line an integer
, the number of test cases. Each test has the following format:
the first line contains an integer
(the size of the array) and
the second line will contain
integers
(
),
separated by single spaces.
It is guaranteed that the sum of over all test cases is at
most
The file azerah.out should contain
lines. The
-th line should contain a single integer, the answer
to the
-th test case.
2 3 3 10 1 2 4 2
33
题解:这是一道dp水题,对于每一位可以选择取或不取,对于奇数dp结果为上一位的奇数情况和偶数情况之和,对于偶数,则是偶数情况是偶数情况*2和奇数情况是上一位奇数情况*2,非常水,适合新手
代码如下
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long mod=1e9+7;
long long dp[1000005][2];
int main()
{
freopen("azerah.in","r",stdin);
freopen("azerah.out","w",stdout);
int t;
cin>>t;
while(t--){
long long n1=0,n2=0;
long long n;
cin>>n;
dp[0][0]=1;
dp[0][1]=0;
for(long long i=1;i<=n;i++)
{
long long l;
scanf("%lld",&l);
if(l&1){
n1++;
dp[i][0]=(dp[i-1][1]+dp[i-1][0])%mod;
dp[i][1]=(dp[i-1][0]+dp[i-1][1])%mod;
}
else
{
n2++;
dp[i][0]=2*dp[i-1][0]%mod;
dp[i][1]=2*dp[i-1][1]%mod;
}
//cout<<dp[i][0]<<' '<<dp[i][1]<<endl;
}
cout<<dp[n][0]-1<<endl;
}
//cout << "Hello world!" << endl;
return 0;
}