UVA10312- Expression Bracketing(Catalan + 递推)

本文通过使用动态规划和Catalan数求解方法,详细介绍了如何计算给定序列中形成非二叉表达式的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接


题意:给出一个序列,长度为n,表示有n个x(节点),可以添加任意括号,问说形成的串为非二叉表达式的有多少个。

思路:用总数减去二叉表达式的数量。二叉表达式可以用Catalan数求解,至于总数的话,用dp求解。dp[i][0]表示在第i个位置可以被拆分成两个子树,dp[i][1]表示有一个子树。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long ll;

const int MAXN = 30;

ll Catalan[MAXN], dp[MAXN][2];
int n;

void init() {
    memset(Catalan, 0, sizeof(Catalan));
    Catalan[1] = Catalan[2] = 1;
    for (int i = 3; i < MAXN; i++)
        Catalan[i] = Catalan[i - 1] * (4 * i - 6) / i; 
}

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

int main() {
    init();
    while (scanf("%d", &n) != EOF) {
        memset(dp, 0, 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、付费专栏及课程。

余额充值