HDU 5396 Expression

题意:给出一个式子,你可以将里面的运算符标上不同的优先级,然后按照优先级运算,问,不同的标法的所有结果的和是多少。


做法:

http://www.cnblogs.com/chenchengxun/p/4741439.html

这个链接解释的比较清楚了,关于最后乘以组合数那里,我再补充一下。


这个其实就是区间DP了
dp[i][j] 代表的是区间  i 到 j 的和
枚举dp[i][j] 之间所有的子区间
假如是乘法:
t = dp[i][k] * dp[k+1][j];
这个其实可以直接算出来的:
假设我们dp[i][k] 里面所有的值是 (x1+x2+x3...xn) == dp[i][k]
假设我们dp[k+1][j] 里面所有的值是 (y1+y2+y3...yn) == dp[k+1][j]
dp[i][k] * dp[k+1][j] == (x1+x2+...xn) * (y1+y2+y3...yn) == x1*y1+x1y*y2......xn*yn 其实和所有不同结果相乘出来是一样的
 
假如是加法或者减法:
我们表示阶乘 i为A[i].
t = dp[i][k]*A[j-k-1] + dp[k+1][j]*A[k-i];
其实这里我们想一下。区间 dp[i][k] 需要加上多少次?
我们需要加的次数就是另一半区间的所有组合数,另一半区间有多少种组合方式我们就要加上多少个。
因为他们之间可以相互组成不同的种类。同理另一半也是。
 
最后的时候我们要乘上一个组合数。
假设组合数为C[i][j].
为什么要乘组合数:
因为 假如我们k 分割了两个运算式子   【 1+(2*3)  】 + 【 1+(3*4) 】
虽然说我们左右两边的式子运算顺序已经确定了,但是我们总的运算顺序还是不确定的, 
比如我们算完(2*3) 再算(3*4),跟算完 (3*4)再算 (2*3)是不一样的,也就是说虽然结果一样,但是当成不一样的,因为优先级安排不一样。
dp[i][j] = dp[i][j] + t*C[j-i-1][k-i]
这个其实就是从总的运算符(j-i),减去了第k个的运算符,也就是( j-i-1)个,然后从中选取(k-i)个进行运算。
这里可以这样理解,如果不考虑总体顺序,直接从左算到右边,那么很显然是左边k-i个运算符比右边j-k-1个运算符先进行运算,这样就可以想到,考虑总体就是从j-i-1个运算符中挑选k-i个运算符首先进行运算,再运算剩下的 j-k-1个运算符,于是就是乘上组合数的原因。


#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
int a[110];
ll fac[110];
ll c[110][110],dp[110][110];
void create()
{
	fac[0]=1;
	for(int i=1;i<=100;i++)
		fac[i]=fac[i-1]*i%mod;
	for(int i=0;i<=100;i++)
		for(int j=0;j<=i;j++)
			if(j==0)
				c[i][j]=1;
			else
				c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
}
char op[110];
int main()
{
	int n;
	create();
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=0;i<n;i++)
			scanf("%d",a+i);
		scanf("%s",op);
		for(int i=1;i<=n;i++)
			for(int j=0;j+i-1<n;j++)
			{
				int r=i+j-1;
				if(i==1)
				{
					dp[j][r]=a[j];
					continue;
				}
				dp[j][r]=0;
				for(int k=j;k<r;k++)
				{
					ll t;
					if(op[k]=='*')
						t=dp[j][k]*dp[k+1][r]%mod;
					else if(op[k]=='+')
						t=dp[j][k]*fac[r-k-1]%mod+dp[k+1][r]*fac[k-j]%mod;
					else
						t=dp[j][k]*fac[r-k-1]%mod-dp[k+1][r]*fac[k-j]%mod;
					dp[j][r]=(dp[j][r]+t*c[i-2][k-j]%mod)%mod;
				}
			}
		printf("%d\n",int((dp[0][n-1]+mod)%mod));
	}
}

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 384    Accepted Submission(s): 218


Problem Description
Teacher Mai has  n  numbers  a1,a2,,an and  n1  operators("+", "-" or "*") op1,op2,,opn1 , 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+1i  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  n1  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+4683 " is  1+46831+4(2)31+(8)3(7)321 .
 

Input
There are multiple test cases.

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

The second line contains  n  integers  a1,a2,,an(0ai109) .

The third line contains a string with length  n1  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.
 

Author
xudyh
 

Source



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Single-Chip 16-Port SerDes Gigabit Switch.The BCM5396 is a 16-port Gigabit Ethernet (GbE) switch integrated with 16 1.25G SerDes/SGMII port interfaces for connecting to external Gigabit PHYs or fiber modules. The BCM5396 provides the lowestcost GbE functionality to the desktop switching solution or WebSmart™ application. The BCM5396 is a highly integrated solution, combining all of the functions of a high-speed switch system, including packet buffer, Media Access Controllers (MACs), address management, and a nonblocking switch controller into a single monolithic 0.13 µm CMOS device. The BCM5396 complies with the IEEE 802.3™, 802.3u, 802.3ab, and 802.3x specifications, including the MAC control PAUSE frame and auto-negotiation subsections, providing compatibility with all industry-standard Ethernet, Fast Ethernet, and GbE devices. The BCM5396 device provides integrated 1.25G SerDes, reducing board footprint requirements. The 16 ports have SGMII interfaces for connecting with external GbE transceivers. 16-port 10/100/1000 Mbps integrated switch controller via 1.25G SerDes/SGMII/fiber • Embedded 256 KB on-chip packet buffer • One 10/100/1000 Mbps In-band Management Port (IMP) with GMII/RGMII/RvMII/MII interface for PHY-less connection to a CPU/management entity (for management purposes only) • Integrated address management • Supports up to 4K MAC addresses • Supports jumbo frames up to 9728 bytes. • Supports EEPROM for low-cost chip configuration • Integrated Motorola® SPI-compatible interface • Supports port mirroring • Port-based VLAN and 4K IEEE 802.1Q tag VLAN • Port-, DiffServ-, MAC-, and IEEE 802.1p-based QoS for four queues • Supports Spanning Tree, Rapid Spanning Tree, and Multiple Spanning Tree protocols (802.1D/1s/1w) • Supports IEEE Standard 802.1X port security • Supports pseudo-PHY MDIO access • MAC-based trunking with link fail-over • Ethernet-in-the-last-mile (EFM) support: OAM and P • Low-power (2.2W total) 1.2V core/2.5V (SGMII I/ O)/3.3V (GMII/MII/RvMII) and 2.5V RGMII operation with 3.3V I/O tolerance • 256-pin FBGA package
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值