UVA 10312 - Expression Bracketing(数论+Catalan数)

探讨了如何计算非二叉表达式的数量,利用SuperCatalan数与Catalan数的关系,给出了两种求解方法:一是使用公式直接计算;二是采用递归的方式通过记忆化搜索减少重复计算。

题目链接:10312 - Expression Bracketing

题意:有n个x,要求分括号,推断非二叉表达式的个数。
思路:二叉表达式的计算方法就等于是Catalan数的,那么仅仅要计算出总数,用总数减去二叉表达式个数。得到的就是非二叉表达式的个数。

那么计算方法是什么呢。

看题目中的图,对于n = 4的情况,能够分为这几种情况来讨论:
四个1。 一个2两个1,一个3一个1。一个4。相应的情况数为1。3。 2。 1。
答案为f(1)^4 + 3 * f(2) * f(1)^2 + f(3) * f(1) + f(4)。
一种做法是把n去分解然后计算。可是显然这是不可行的,n最大为26,情况数太多了。
然后找题解,发现这个竟然有公式,这个式子叫SuperCatalan数。

然后也有递推出来的解。设dp[n][2]。n表示还有n个子节点未分配。2表示0为最多分配n - 1个点,1为最多分配n个点,这样能保证子树都至少有两个节点。这样就是总情况了,直接用记忆化搜下去就可以

代码:

公式解:

#include <stdio.h>
#include <string.h>

int n;
long long Catalan[30], SuperCatalan[30];

int main() {
	Catalan[1] = Catalan[2] = 1;
	for (int i = 3; i <= 26; i++) {
		Catalan[i] = Catalan[i - 1] * (4 * i - 6) / i;
 	}
 	SuperCatalan[1] = SuperCatalan[2] = 1;
 	for (int i = 3; i <= 26; i++) {
		SuperCatalan[i] = (3 * (2 * i - 3) * SuperCatalan[i - 1] - (i - 3) * SuperCatalan[i - 2]) / i;
	}
	while (~scanf("%d", &n)) {
		printf("%lld\n", SuperCatalan[n] - Catalan[n]);
 	}
	return 0;
}

递推解:

#include <stdio.h>
#include <string.h>

int n;
long long Catalan[30], dp[30][2];

long long dfs(int n, int flag) {
	long long &ans = dp[n][flag];
	if (~ans) return ans;
	if (n <= 1) return ans = 1;
	ans = 0;
	for (int i = 1; i < n + flag; i++)
		ans += dfs(i, 0) *	dfs(n - i, 1);
	return ans;
}

int main() {
	Catalan[1] = Catalan[2] = 1;
	for (int i = 3; i <= 26; i++) {
		Catalan[i] = Catalan[i - 1] * (4 * i - 6) / i;
 	}
	while (~scanf("%d", &n)) {
		memset(dp, -1, sizeof(dp));
		printf("%lld\n", dfs(n, 0) - Catalan[n]);
 	}
	return 0;
}


running on 48 total cores distrk: each k-point on 48 cores, 1 groups distr: one band on 1 cores, 48 groups using from now: INCAR vasp.5.4.4.18Apr17-6-g9f103f2a35 (build Feb 20 2024 19:41:19) complex POSCAR found type information on POSCAR O In H POSCAR found : 3 types and 204 ions scaLAPACK will be used For optimal performance we recommend to set | | NCORE= 4 - approx SQRT( number of cores) | | NCORE specifies how many cores store one orbital (NPAR=cpu/NCORE). | | This setting can greatly improve the performance of VASP for DFT. | | The default, NCORE=1 might be grossly inefficient | | on modern multi-core architectures or massively parallel machines. | | Do your own testing !!!! | | Unfortunately you need to use the default for GW and RPA calculations. | | (for HF NCORE is supported but not extensively tested yet) 3613 T= 0. E= -.10133561E+04 F= -.10271913E+04 E0= -.10271913E+04 EK= 0.12536E-05 SP= 0.31E+06 SK= -.31E+06 ERROR in subspace rotation PSSYEVX: I2,IFAIL= 1 108 没有达到步就停止了计算,是哪里的问题?我的INCAR文件如下,请做出相应的修改 ##### initial parameters I/O ##### SYSTEM = P0_14H2_MD ISTART = 1 # whether or not to read the WAVECAR file. ICHARG = 1 # how VASP constructs the initial charge density. LWAVE = .FALSE. # whether the wavefunctions are written to the WAVECAR file. LCHARG = .FALSE. # LCHARG determines whether the charge densities (files CHGCAR and CHG) are written. 关闭详细电荷 LVTOT = .FALSE. # whether the total local potential is written to the LOCPOT file. LVHAR = .FALSE. # whether the electrostatic potential is written to the LOCPOT file. LELF = .FALSE. # whether to create an ELFCAR file or not. NBLOCK = 1 # 每步输出一次 KBLOCK = 1 # 每步写入一次XDATCAR NWRITE = 2 # 详细输出,写入每个电子步的信息;(1默认值-中等输出-写入每个离子步的信息) #### Electronic Relaxation #### ENCUT = 450 # cutoff energy for the planewave basis set in eV. ISMEAR = 0 # how the partial occupancies are set for each orbital. SIGMA = 0.1 # the width of the smearing in eV. EDIFF = 1E-4 # global break condition for the electronic SC-loop. 电子步收敛标准 NELMIN = 4 # specifies the minimum number of electronic SCF steps. 强制至少迭代4步,平衡精度速度 NELM = 300 # maximum number of electronic SC (selfconsistency) steps. 电子自洽迭代的最大步上限(默认60) GGA = PE # 使用PBE泛函(PE是PBE在VASP中的代码) LASPH = .TRUE. # 启用角动量修正(对d/f电子重要) LREAL = Auto # whether the projection operators are evaluated in real-space or in reciprocal space. 实空间投影加速(对表面体系安全) ISYM = 0 # determines the way VASP treats symmetry. PREC = Normal # 平衡精度速度 #### MD #### IBRION = 0 # 启用MD模式 MDALGO = 2 # NVT系综(使用Nose-Hoover控温器) SMASS = -3 # 改为经典Nose-Hoover质量参(VASP 5.4.4支持-3设置) POTIM = 0.5 # timestep of ionic movement (0.2 when not converge, maybe).时间步长0.5 fs(H-H键振动周期~10fs,含H体系一般选0.5 fs) NSW = 10000 # maxcycle of opt. MD总步(5000 fs,按0.5 fs/步) ALGO = Fast TEBEG = 573 # 起始温度=目标温度573K TEEND = 573 # 结束温度保持573K
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值