A - Por Costel and Azerah
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-emptysubsequences 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!
Input
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
Output
The file azerah.out should contain lines. The
-th line should contain a single integer, the answer to the
-th test case.
Example
2
3
3 10 1
2
4 2
3
3
题意:给你一个序列,问你有多少个子序列的元素和为偶数。输入第一行为测试样例数,每行样例两行,第一行为序列的长度,第二行为序列。
思路:对于每一位可以选择取或不取,对于奇数,dp结果为上一位的奇数情况和偶数情况之和,对于偶数,则是偶数情况是偶数情况*2,并且奇数情况是上一位奇数情况*2,
1 #include <cstdio> 2 #include <fstream> 3 #include <algorithm> 4 #include <cmath> 5 #include <deque> 6 #include <vector> 7 #include <queue> 8 #include <string> 9 #include <cstring> 10 #include <map> 11 #include <stack> 12 #include <set> 13 #include <sstream> 14 #include <iostream> 15 #define mod 1000000007 16 #define eps 1e-6 17 #define ll long long 18 #define INF 0x3f3f3f3f 19 using namespace std; 20 21 int t,n; 22 ll x,dp[1000010][2]; 23 int main() 24 { 25 freopen("azerah.in","r",stdin); 26 freopen("azerah.out","w",stdout); 27 scanf("%d",&t); 28 while(t--) 29 { 30 scanf("%d",&n); 31 dp[0][1]=1;//1代表位偶数时 32 dp[0][0]=0;//0代表位奇数时 33 for(int i=1;i<=n;i++) 34 { 35 scanf("%lld",&x); 36 if(x%2==0) 37 { 38 dp[i][1]=dp[i-1][1]*2%mod; 39 dp[i][0]=dp[i-1][0]*2%mod; 40 } 41 else 42 { 43 dp[i][1]=dp[i][0]=(dp[i-1][1]+dp[i-1][0])%mod; 44 } 45 } 46 printf("%lld\n",dp[n][1]-1); 47 } 48 }