题目:

Problem Description

Consider words of length 3n over alphabet {A, B, C} . Denote the number of occurences of A in a word a as A(a) , analogously let the number of occurences of B be denoted as B(a), and the number of occurenced of C as C(a) .

Let us call the word w regular if the following conditions are satisfied:

A(w)=B(w)=C(w) ; 
if c is a prefix of w , then A(c)>= B(c) >= C(c) . 
For example, if n = 2 there are 5 regular words: AABBCC , AABCBC , ABABCC , ABACBC and ABCABC .

Regular words in some sense generalize regular brackets sequences (if we consider two-letter alphabet and put similar conditions on regular words, they represent regular brackets sequences).

Given n , find the number of regular words.

Input

There are mutiple cases in the input file.

Each case contains n (0 <= n <= 60 ).

There is an empty line after each case.

Output

Output the number of regular words of length 3n .

There should be am empty line after each case.

Sample Input

2

3

Sample Output

5

42

Source

Recommend

xhd

题解:

dp方程很好想:用f[a][b][c]表示A有a个B有b个C有c个的单词的总数量,则f[a][0][0]=1,f[a][b][0]=f[a-1][b][0]+f[a][b-1][0],f[a][b][c]=f[a-1][b][c]+f[a][b-1][c]+f[a][b][c-1],只是要注意f可能过大··所以要用高精度顺便压下位····

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int N=;
const int Bit=;
int f[N][N][N][],n;
inline void add(int f1[],int f2[])
{
int l1,l2;
for(l1=;!f1[l1];l1--);for(l2=;!f2[l2];l2--);
if(l1<l2) l1=l2;
for(int i=;i<=l1;i++) f1[i]+=f2[i];
for(int i=;i<=l1;i++) f1[i+]+=f1[i]/Bit,f1[i]%=Bit;
}
inline void W(int f1[])
{
int l1;
for(l1=;!f1[l1];l1--);
cout<<f1[l1];
for(int i=l1-;i>=;i--)
{
if(f1[i]>=&&f1[i]<) putchar('');
else if(f1[i]>=&&f1[i]<) putchar(''),putchar('');
else if(f1[i]>=&&f1[i]<) putchar(''),putchar(''),putchar('');
cout<<f1[i];
}
cout<<endl;cout<<endl;
}
inline void getans()
{
for(int i=;i<=;i++)
for(int j=;j<=i;j++)
for(int k=;k<=j;k++)
{
if(j==) f[i][j][k][]=;
else if(k==)
{
add(f[i][j][],f[i][j-][]);add(f[i][j][],f[i-][j][]);
}
else
{
add(f[i][j][k],f[i-][j][k]);add(f[i][j][k],f[i][j-][k]);add(f[i][j][k],f[i][j][k-]);
}
}
}
int main()
{
//freopen("a.in","r",stdin);
getans();
while(~scanf("%d",&n))
W(f[n][n][n]);
return ;
}

刷题总结——regular words(hdu1502 dp+高精度加法+压位)的更多相关文章

  1. $2019$ 暑期刷题记录1:(算法竞赛DP练习)

    $ 2019 $ 暑期刷题记录: $ POJ~1952~~BUY~LOW, BUY~LOWER: $ (复杂度优化) 题目大意:统计可重序列中最长上升子序列的方案数. 题目很直接的说明了所求为 $ L ...

  2. POJ 2711 Regular Words(DP + 高精度)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1711 题目大意:给定一个正整数n,产生一个3*n位长的串,要求这个串 ...

  3. 刷题10. Regular Expression Matching

    一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处 ...

  4. DP刷题记录(持续更新)

    DP刷题记录 (本文例题目前大多数都选自算法竞赛进阶指南) TYVJ1071 求两个序列的最长公共上升子序列 设\(f_{i,j}\)表示a中的\(1-i\)与b中色\(1-j\)匹配时所能构成的以\ ...

  5. DP刷题记录

    目录 dp刷题记录 codeforces 706C codeforces 940E BZOJ3997 POJ2279 GYM102082B GYM102082D codeforces132C L3-0 ...

  6. 刷题向》DP》关于基础DP(easy)

    openjudge  8464 这道题其实很简单,算是DP的基础题,比较适合开拓DP思维. 题目比较有欺骗性,其实稍微想想就可以解决,因为题意说第一次卖出后才能买入,所以我们可以考虑枚举断点,所以题目 ...

  7. Leetcode OJ 刷题

    Valid Palindrome吐槽一下Leetcode上各种不定义标准的输入输出(只是面试时起码能够问一下输入输出格式...),此篇文章不是详细的题解,是自己刷LeetCode的一个笔记吧,尽管没有 ...

  8. 【刷题记录】BZOJ-USACO

    接下来要滚去bzoj刷usaco的题目辣=v=在博客记录一下刷题情况,以及存一存代码咯.加油! 1.[bzoj1597][Usaco2008 Mar]土地购买 #include<cstdio&g ...

  9. POJ 水题(刷题)进阶

    转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...

随机推荐

  1. UVA439 knightMoves (A*启发搜索)

    第一个A*,纪念下. A*要保证最短路一定要估价函数小于等于实际值,越接近越好 估价函数取Manhattan距离除以二. //Rey #include<cstdio> #include&l ...

  2. noip模拟赛#38

    我打开了#39的problem...想了半个小时多发现我一道题都不会写...于是我打开了#38的problem T1:循环数字的定义为能够将该数划分为若干相同长度的段并且都相同. n=2e18. =& ...

  3. 剑指offer64 数据流中的中位数

    priority_queue优先级队列,他的模板声明带有三个参数,priority_queue<Type, Container, Functional> Type 为数据类型, Conta ...

  4. PopClip:你会热爱的文本穿梭机

    http://www.ifanr.com/234952 由于我是一名 Evernote 用户(不是印象笔记),最近发现它所提供的浏览器插件无论是 Web Cliper 还是 Clearly,反应速度都 ...

  5. SD Card Formatter for Mac Download

    https://www.sdcard.org/downloads/formatter_4/eula_mac/ SDFormatter Mac版是一款Mac OS平台上的sd卡修复工具,SDFormat ...

  6. WINDOWS-基础:LPTSTR

    1. LPTSTR解释 与char*等价,表示普通字符/字符串变量,指向字符/字符串的指针. LP:  长指针(long pointer). T:   win32环境中有一个_T宏,用来标识字符是否采 ...

  7. PAT (Basic Level) Practise (中文)- 1010. 一元多项式求导 (25)

    http://www.patest.cn/contests/pat-b-practise/1010 设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为n*xn-1.) 输入格式:以指数递降 ...

  8. Java基础面试题: 份证号码组成规则是前4位是代表省份和地区 编写一个程序,通过身份证号码判断某人是否是武汉人以及其性别。

    package com.swift; public class ID_Where_Male_Test { public static void main(String[] args) { /* * 中 ...

  9. 如何用纯 CSS 创作一个行驶中的火车 loader

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/RBLWzJ 可交互视频 ...

  10. 使用slot-scope复制vue中slot内容

    有时候我们的vue组件需要复制使用者传递的内容. 比如我们工程里面的轮播组件需要使用复制的slot来达到循环滚动的效果 使用者关注轮播内容的静态效果,组件负责让其滚动起来 组件: <div cl ...