跳到正文

个人记录

考试总结

chenzhiyv约 10 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 10 分钟

版本

  1. v1当前

3.8考试

经验:

  1. 交换两行/列可以用一个映射数组,将复杂度从 O(n)O(n) 降到 O(1)O(1)
  2. 当问题出现改变一部分另一部分也会改变,并且合法状态唯一是,可以先考虑将部分变合法,再判断其他部分是否合法,当其他部分不合法时,一定无解,因为改变其他行就会改变已经合法的那一部分,从而让那一部分不合法(参考T1)

3.22

首先,总结一下这场考试的应得分数 100+40+20+0=160 T1因为没预处理逆元导至多带一个log -50 T1理论复杂度为 3∗n∗n∗log2(3∗n∗n)=6e3∗2e3∗log2(6e3∗2e3)=433826884(4e8)3*n*n*log_2(3*n*n) = 6e3*2e3*log_2(6e3*2e3) =433 826 884 (4e8),但大常数,寄 教训:

  1. 写完用极限数据跑一下
  2. 这种能预处理优化掉的东西应该在写的时候就优化掉

经验:

  1. dp维数不一定是越多越好,可能多了反而具有后效性或转移出现环
  2. 看到区间和可以向固定一个数算多少个区间包含,即算贡献,因为算区间枚举就是 O(n2)O(n^2) 的没有优化空间,但算贡献可以做到 O(n)O(n)

4.6考试

教训:

  1. 检查空间问题,有时候越界了本地不会有问题,特别注意数据结构不是和普通数组一样的大小,比如trie的长度是字符串的总长,而不是字符串的个数,线段树空间4倍等

4.12

教训:

  1. 注意数据分治的条件

T3解法:

设 dp[i][j]dp[i][j] 表示区间从 ii 到 jj 的所有情况的总和,cnt[i][j]cnt[i][j] 表示有多少种计算区间 ii 到 jj 的和的方法。

转移:

设 xx 是区间中的断点(因为转移就是合并两个区间)

dp[l][r]+=dp[l][x]∗cnt[x+1][r]+dp[i+1][r]∗cnt[l][x]∗C(len1−1,len−2)(op==+) dp[l][r]+=dp[l][x]*cnt[x+1][r]+dp[i+1][r]*cnt[l][x]*C(len1-1,len-2) (op== + )

其中,CC就是组合数,len1len1是左区间的长度,lenlen 是区间总长

首先,我们知到左右两边是独立的,所以左(右)的每一种情况算完后都可以和右(左)的每一种情况合并,所以是左区间的总和乘上右边区间的情况数,右边区间乘上左边区间的情况数

但是,我们注意到,虽然左右区间是独立的,但是不同的操作次序会导致方案不同,进而导致答案不同(如我先算左边两次,在算右边一次,和我算左边一次,右边一次,左边一次,这两种方案算出来的答案是相同的,但是方案是不同的,所以我们就要乘上组合数),组合数就是 C(len1−1,len−2)C(len1-1,len-2)

==解释==:首先可以发现,一个长度为 lenlen 的序列可以打 len−1len-1 个括号对,这时在首尾会强制打一对括号,减去最外边的括号后,可以自由放的括号就是 len−2len-2 个。这时,这 len−2len-2 个括号是有两种情况,是左边还是右边,而左边要打的括号有 len1−1 len1-1 个(有边同理),所以我们就可以直接乘上组合数,当然,乘上 C(len2−1,len−2)C(len2-1,len-2) 也可以。

另外两种情况:

dp[l][r]+=dp[l][i]∗cnt[i+1][r]−dp[i+1][r]∗cnt[l][i]∗C(len1−1,len−2)(op==−) dp[l][r]+=dp[l][i]*cnt[i+1][r]-dp[i+1][r]*cnt[l][i]*C(len1-1,len-2) (op== - )

同加法

dp[l][r]+=dp[l][i]∗dp[i+1][r]∗C(len1−1,len−2) dp[l][r]+=dp[l][i]*dp[i+1][r]*C(len1-1,len-2)

这时,左边的每一个情况都会和右边的每一个情况相乘,提公因式后就是 dp[l][i]dp[l][i] 与 dp[i+1][r]dp[i+1][r]

cntcnt 就是直接乘起来再乘组合数就可以了

C++
#include<bits/stdc++.h>
namespace fast_IO {
#define IOSIZE 1000000
	char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
	template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
	template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
	template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
	inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
	inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
	inline void print(char x) { putchar(x); }
	inline void print(char *x) { while (*x) putchar(*x++); }
	inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
	inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
	inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
	inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
	inline void print(bool b) { putchar(b+48); }
	template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
	template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
	struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
	template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
	template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
using namespace std;
#define int long long
const int N=500+10,mod=1e9+7;
int n;
int a[N];
int dp[N][N],cnt[N][N];
string op;
int qpow(int a,int b)
{
	int res=1;
	while(b)
	{
		if(b&1)
			res=res*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return res%mod;
}
int fac[N],infac[N];
void init(int n)
{
	fac[0]=1;
	for(int i=1;i<=n;i++)
		fac[i]=fac[i-1]*i%mod;
	infac[n]=qpow(fac[n],mod-2);
	for(int i=n-1;i>=0;i--)
		infac[i]=infac[i+1]*(i+1)%mod;
}
int C(int b,int a)
{
	if(b>a)
		return 0;
	return fac[a]*infac[b]%mod*infac[a-b]%mod;
}
signed main()
{
	cin>>n;
	init(n);
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		dp[i][i]=a[i];
		cnt[i][i]=1;
	}
	cin>>op;
	op=" "+op;
	for(int len=2;len<=n;len++)
	{
		for(int l=1;l<=(n-len+1);l++)
		{
			int r=l+len-1;
			for(int i=l;i<=r-1;i++)
			{
				if(op[i]=='+')
					dp[l][r]=(dp[l][r]+(dp[l][i]*cnt[i+1][r]%mod+dp[i+1][r]*cnt[l][i]%mod)*C(i-l,len-2)%mod)%mod;
				else if(op[i]=='-')
					dp[l][r]=((dp[l][r]+(dp[l][i]*cnt[i+1][r]%mod-dp[i+1][r]*cnt[l][i]%mod)*C(i-l,len-2)%mod)+mod)%mod;
				else if(op[i]=='*')
					dp[l][r]=(dp[l][r]+(dp[l][i]*dp[i+1][r]%mod)*C(i-l,len-2)%mod)%mod;
				cnt[l][r]=(cnt[l][r]+(cnt[l][i]*cnt[i+1][r]%mod*C(i-l,len-2)%mod))%mod;
			}
		}
	}
//	cout<<C(0,0)<<endl;
	cout<<(dp[1][n]+mod)%mod<<endl;
	return 0;
}
原文