Problem Description

Teacher Mai has n numbers a1,a2,⋯,an and n−1 operators("+", "-" or "*")op1,op2,⋯,opn−1 , which are arranged in the form a1 op1 a2 op2 a3 ⋯ an .

He wants to erase numbers one by one. In i -th round, there are n+1−i numbers remained. He can erase two adjacent numbers and the operator between them, and then put a new number (derived from this one operation) in this position. After n−1 rounds, there is the only one number remained. The result of this sequence of operations is the last number remained.

He wants to know the sum of results of all different sequences of operations. Two sequences of operations are considered different if and only if in one round he chooses different numbers.

For example, a possible sequence of operations for "1+4∗6−8∗3 " is 1+4∗6−8∗3→1+4∗(−2)∗3→1+(−8)∗3→(−7)∗3→−21 .

Input

There are multiple test cases.

For each test case, the first line contains one number n(2≤n≤100) .

The second line contains n integers a1,a2,⋯,an(0≤ai≤109) .

The third line contains a string with length n−1 consisting "+","-" and "*", which represents the operator sequence.

Output

For each test case print the answer modulo 109+7 .

Sample Input

3

3 2 1

-+

5

1 4 6 8 3

+*-*

Sample Output

2

999999689

Hint

Two numbers are considered different when they are in different positions.

题目大意是给一个表达式自己可以随意定义运算符的运算顺序,求所有不同运算顺序得到的答案的和。

首先总共的种数有(n-1)!个。这个就是一个乘法原理。

但是99!这么多跑一遍就跪了。

于是考虑一种策略,记s[i][j]表示从第i个到第j个数这个序列能得到的不同运算结果的和。

对于最后一个运算的运算符假设是k,那么k可以取i, i+1, ..., j。

所以s[i][j] = sum(s[i][k] op s[k+1][j]),

关键是这个op运算如何实现,显然这个运算不是纯粹的+,-,*。

然后我们再看。对于s[i][j]这个式子,当a[j]加入时,这个式子才有意义。

也就是说,如果之前所有的s[x][y](y < j)都计算出来后,才能通过a[j]计算出所有的s[x][j]。

所以上述式子应该是这样s[i][j] = f(i, j) = sum(s[i][k] op f(k+1, j))。

其中f(i, j)表示生成s[i][j]的函数,相当于f是新的s。当然这样写只是为了在递推的时候搞清楚k的顺序。这样如果搞不清k的递推顺序,就能通过这个式子进行记忆化搜索。

接下来就是考虑这个op操作了。

假设记s[i][k]对应的解集为A(即A中所有元素和为s[i][k]),s[k+1][j]对应的解集为B

对于op是’*’的:

那么s[i][k] op s[k+1][j]应该为sum(Ai*Bj) = sum(Ai*sum(Bi)) = sum(Ai)*sum(Bi)

但是这样并没有考虑A和B中运算符的顺序,虽然对于一个Ai,必定是通过i到k的元素经过一定的运算顺序才能得到。但是Ai和Bi的运算顺序是不相干的(也就是说可以先在Ai这里算一个’+’,再在Bi这里算一个’*’),所以这里就变成一个计数问题了。Ai和Bi需要排在一起,但是Ai中的元素不计排序,Bi同样的。

所以相当于在j-i-1个位置中取k-i个位置给Ai。所以结果需要乘上C(k-i, j-i-1)。

得到了s[i][k] ‘*’ s[k+1][j] = sum(Ai)*sum(Bi)*C(k-i, j-i-1) = s[i][k] * s[k+1][j] * C(k-i, j-i-1)

对于op是’+’的:

同样的考虑,

那么s[i][k] op s[k+1][j]应该为sum(Ai+Bj) = sum(kB*Ai+sum(Bi)) = kBsum(Ai)+kAsum(Bi),(其中kA和kB分别表示A和B集合元素的个数)

最后结果再乘上个C(k-i, j-i-1)

于是s[i][k] ‘+’ s[k+1][j] = (kBsum(Ai)+kAsum(Bi))*C(k-i, j-i-1)

kA = (k-i)!, kB = (j-k-1)!可以通过乘法原理得到。

注意C[0][0]也不能漏掉。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#define LL long long
#define MOD 1000000007 using namespace std; const int maxN = ;
int n, a[maxN];
char op[maxN];
LL A[maxN], C[maxN][maxN], s[maxN][maxN]; void init()
{
A[] = ;
for (int i = ; i < maxN; ++i)
A[i] = (A[i-]*i)%MOD; for (int i = ; i < maxN; ++i)
C[][i] = C[i][i] = ;
for (int i = ; i < maxN; ++i)
for (int j = ; j < i; ++j)
C[j][i] = (C[j][i-]+C[j-][i-])%MOD;
} inline LL cal(int x, int y, int z, char p)
{
LL ans;
if (p == '+')
ans = (s[x][y]*A[z-y-]%MOD+s[y+][z]*A[y-x]%MOD)%MOD;
else if (p == '-')
ans = ((s[x][y]*A[z-y-]%MOD-s[y+][z]*A[y-x]%MOD)%MOD+MOD)%MOD;
else
ans = (s[x][y]*s[y+][z])%MOD;
return (ans*C[y-x][z-x-])%MOD;
} void input()
{
memset(s, , sizeof(s));
for (int i = ; i < n; ++i)
{
scanf("%d", &a[i]);
s[i][i] = a[i];
}
scanf("%s", op);
} void work()
{
for (int j = ; j < n; ++j)
for (int i = j-; i >= ; --i)
for (int k = j-; k >= i; --k)
s[i][j] = (s[i][j]+cal(i, k, j, op[k]))%MOD;
printf("%lld\n", s[][n-]);
} int main()
{
//freopen("test.in", "r", stdin);
init();
while (scanf("%d", &n) != EOF)
{
input();
work();
}
return ;
}

ACM学习历程—HDU5396 Expression(递推 && 计数)的更多相关文章

  1. ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 题目大意就是求一个序列里面长度为m的递增子序列的个数. 首先可以列出一个递推式p(len, i) =  ...

  2. ACM学习历程—HDU1041 Computer Transformation(递推 && 大数)

    Description A sequence consisting of one digit, the number 1 is initially written into a computer. A ...

  3. ACM学习历程——ZOJ 3822 Domination (2014牡丹江区域赛 D题)(概率,数学递推)

    Description Edward is the headmaster of Marjar University. He is enthusiastic about chess and often ...

  4. ACM学习历程—HDU1028 Ignatius and the Princess III(递推 || 母函数)

    Description "Well, it seems the first problem is too easy. I will let you know how foolish you ...

  5. ACM学习历程—HDU 5326 Work(树形递推)

    Problem Description It’s an interesting experience to move from ICPC to work, end my college life an ...

  6. ACM学习历程—SNNUOJ 1116 A Simple Problem(递推 && 逆元 && 组合数学 && 快速幂)(2015陕西省大学生程序设计竞赛K题)

    Description Assuming a finite – radius “ball” which is on an N dimension is cut with a “knife” of N- ...

  7. ACM学习历程—NPU 2015年陕西省程序设计竞赛网络预赛(正式赛)F题 和谐的比赛(递推)

    Description 今天西工大举办了一场比赛总共有m+n人,但是有m人比较懒没带电脑,另外的n个人带了电脑.不幸的是,今天机房的电脑全坏了只能用带的电脑,一台电脑最多两人公用,确保n>=m. ...

  8. 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始

    以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告

  9. ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...

随机推荐

  1. gulp入门-压缩js/css文件(windows)

    类似于grunt,都是基于Node.js的前端构建工具.不过gulp压缩效率更高. 工具/原料 nodejs/npm 方法/步骤 首先要确保pc上装有node,然后在global环境和项目文件中都in ...

  2. 根据URL发起HTTP请求,我的HTTPHelper。

     完整的demo using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  3. visual C++ & Makefile

    Makefile说明: visual c++ 不通过IDE有3中方式对工程进行编译链接. 1.MSBuild 编译链接(IDE就是使用MSBuild,需要工程的.vcxproj和.filters文件) ...

  4. activiti自己定义流程之Spring整合activiti-modeler实例(六):启动流程

    1.启动流程并分配任务是单个流程的正式開始,因此要使用到runtimeService接口.以及相关的启动流程的方法.我习惯于用流程定义的key启动,由于有多个版本号的流程定义时,用key启动默认会使用 ...

  5. 数据结构与算法之枚举(穷举)法 C++实现

    枚举法的本质就是从全部候选答案中去搜索正确的解,使用该算法须要满足两个条件: 1.能够先确定候选答案的数量. 2.候选答案的范围在求解之前必须是一个确定的集合. 枚举是最简单.最基础.也是最没效率的算 ...

  6. Vue引入js、css文件

    1.js调用方法一:这是组件内调用,非公共js 2.js调用方法二:公共jsmain.js内加入公共jsVue.prototype.timeago = timeago 3.引入公共css在main.j ...

  7. Servlet单例模式(注意)

    package com.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax ...

  8. cocos2d-js添加道有道插屏(通过jsb反射机制)

    1.导入jar包 2.修改AndroidManifest.xml文件 添加权限:      <activity android:configChanges="keyboard|keyb ...

  9. 一文看透 Redis 分布式锁进化史(解读 + 缺陷分析)(转)

    近两年来微服务变得越来越热门,越来越多的应用部署在分布式环境中,在分布式环境中,数据一致性是一直以来需要关注并且去解决的问题,分布式锁也就成为了一种广泛使用的技术,常用的分布式实现方式为Redis,Z ...

  10. windows系统下nodejs、npm、express的下载和安装教程——2016.11.09

    1. node.js下载 首先进入http://nodejs.org/dist/,这里面的版本呢,几乎每个月都出几个新的,建议大家下载最新版本,看看自己的电脑是多少位的,别下错了. 下载完解压到你想放 ...