首页 > 其他 > 详细

【动态规划】Gym - 100923A - Por Costel and Azerah

时间:2017-01-20 07:18:40      阅读:504      评论:0      收藏:0      [点我收藏+]

azerah.in / azerah.out

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!

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

Input
2
3
3 10 1
2
4 2
Output
3
3

 

给你一个序列,问你有多少个子序列的元素和为偶数。

水dp。分别记f(i)为前i位元素和为偶数的子序列数,g(i)为前i位元素和为奇数的子序列数。瞎几把转移一下就行了。

#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
#define MOD 1000000007ll
int T,a[1000010],n;
ll f[1000010],g[1000010];
int main()
{
//	freopen("a.in","r",stdin);
	freopen("azerah.in","r",stdin);
	freopen("azerah.out","w",stdout);
	scanf("%d",&T);
	for(;T;--T)
	  {
	  	memset(f,0,sizeof(f));
	  	memset(g,0,sizeof(g));
	  	scanf("%d",&n);
	  	for(int i=1;i<=n;++i)
	  	  scanf("%d",&a[i]);
	  	if(a[1]%2)
	  	  g[1]=1;
	  	else
	  	  f[1]=1;
	  	for(int i=2;i<=n;++i)
	  	  if(a[i]%2)
	  	    {
	  	      f[i]=(g[i-1]+f[i-1])%MOD;
	  	      g[i]=((f[i-1]+g[i-1])%MOD+1ll)%MOD;
	  	    }
	  	  else
	  	    {
	  	      f[i]=((f[i-1]*2ll)%MOD+1ll)%MOD;
	  	      g[i]=(g[i-1]*2ll)%MOD;
	  	    }
	  	printf("%d\n",f[n]);
	  }
	return 0;
}

【动态规划】Gym - 100923A - Por Costel and Azerah

原文:http://www.cnblogs.com/autsky-jadek/p/6321740.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!