2015暑假多校联合---Expression(区间DP)
题目链接
http://acm.split.hdu.edu.cn/showproblem.php?pid=5396
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.
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.
Two numbers are considered different when they are in different positions.
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#define eps 1e-8
#define maxn 105
#define inf 0x3f3f3f3f3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
const long long mod=1e9+;
long long a[];
char s[];
long long dp[][];
long long A[];
long long C[][]; int main()
{
int n;
A[]=;
for(long long i=;i<=;i++) ///排列数;
A[i]=A[i-]*i%mod; for(int i=;i<;i++) ///组合数;
C[i][]=;
C[][]=;
for(int i=;i<;i++)
{
for(int j=;j<=i;j++)
C[i][j]=(C[i-][j-]+C[i-][j])%mod;
}
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
scanf("%lld",&a[i]);
scanf("%s",s+);
memset(dp,,sizeof(dp)); for(int i=;i<=n;i++)
dp[i][i]=a[i]; for(int len=;len<=n;len++)
{
for(int i=;i<=n;i++)
{
if(i+len->n) break;
for(int k=i;k<i+len-;k++)
{
long long t;
if(s[k]=='*')
t=(dp[i][k]*dp[k+][i+len-])%mod;
else if(s[k]=='-')
t=(dp[i][k]*A[i+len--k]-dp[k+][i+len-]*A[k-i])%mod;
else
t=(dp[i][k]*A[i+len--k]+dp[k+][i+len-]*A[k-i])%mod;
dp[i][i+len-]=(dp[i][i+len-]+t*C[len-][k-i])%mod;
}
}
}
printf("%lld\n",(dp[][n]%mod+mod)%mod);
}
return ;
}
2015暑假多校联合---Expression(区间DP)的更多相关文章
- 2015暑假多校联合---Mahjong tree(树上DP 、深搜)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5379 Problem Description Little sun is an artis ...
- 2015暑假多校联合---CRB and His Birthday(01背包)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5410 Problem Description Today is CRB's birthda ...
- 2015暑假多校联合---Zero Escape(变化的01背包)
题目链接 http://acm.hust.edu.cn/vjudge/contest/130883#problem/C Problem Description Zero Escape, is a vi ...
- 2015暑假多校联合---Assignment(优先队列)
原题链接 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbere ...
- 2015暑假多校联合---Cake(深搜)
题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...
- 2015暑假多校联合---Friends(dfs枚举)
原题链接 Problem Description There are n people and m pairs of friends. For every pair of friends, they ...
- 2015暑假多校联合---Problem Killer(暴力)
原题链接 Problem Description You are a "Problem Killer", you want to solve many problems. Now ...
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- 2016暑假多校联合---To My Girlfriend
2016暑假多校联合---To My Girlfriend Problem Description Dear Guo I never forget the moment I met with you. ...
随机推荐
- [Spring框架]Spring 事务管理基础入门总结.
前言:在之前的博客中已经说过了数据库的事务, 不过那里面更多的是说明事务的一些锁机制, 今天来说一下Spring管理事务的一些基础知识. 之前的文章: [数据库事务与锁]详解一: 彻底理解数据库事务一 ...
- Atitit 代理CGLIB 动态代理 AspectJ静态代理区别
Atitit 代理CGLIB 动态代理 AspectJ静态代理区别 1.1. AOP 代理主要分为静态代理和动态代理两大类,静态代理以 AspectJ 为代表:而动态代理则以 spring AOP 为 ...
- git忽略以点开头的文件夹
git忽略以点开头的文件夹 好像不是什么问题,可是我用的时候不好使,还是记录下 参考:http://www.oschina.net/question/1437985_2181276
- Java 线程 — AbstractQueuedSynchronizer
锁 锁就是一种状态,比如互斥锁:同一时间只能有一个线程拥有,可以使用一个整型值来标志当前的状态 0:表示没有现成占有锁 1:表示锁已经被占用 AbstractQueuedSynchronizer 实现 ...
- SSIS Send Mail
在SSIS中Send Mail的方法主要有三种,使用Send Mail Task,使用Script Task和使用存储过程msdb.dbo.sp_send_dbmail. 一,使用Send Mail ...
- KendoUI系列:Window
1.基本使用 <link href="@Url.Content("~/Content/kendo/2014.1.318/kendo.common.min.css") ...
- 深入理解PHP内核(六)函数的定义、传参及返回值
一.函数的定义 用户函数的定义从function 关键字开始,如下 function foo($var) { echo $var; } 1.词法分析 在Zend/zend_language_scann ...
- js断点调试心得
虽然网上已经有多的数不清的调试教程了,但仍然没有发现哪篇文章写的通俗易懂,索性自己尝试写写自己的一些使用习惯或者说是心得,希望对那些还不是很懂得使用断点调试的孩子有一些帮助(大神请无视~). 1.断点 ...
- eclipse中关联文件设置方法
在前几次的试验中,只是做了处于应用程序最上层的界面设计,其实还不知程序在运行过程中到底调用了哪些函数,这些函数是怎么实现的,由于搭建环境时没有进行文件关联,所以在环境中无法实现ctrl键+左击鼠标的方 ...
- JAVA 设计模式 解释器模式
用途 解释器模式 (Interpreter) 定义一个语言,定义它的文法的一种表示. 并定义一个解释器,这个解释器使用该表示来解释语言中的句子. 解释器模式是一种行为型模式. 结构