zoj 2711 - Regular Words

题目:求由A,B,C构成的有序传中长度为n,且每个B前面的A的个数不少于当前B,每个C前面的B的个数不少于当前C的个数。

分析:dp,求排列组合数。

考虑二维的状况:

如果 A>=B 则在 F(A-1,B)后面放上A,在F(A,B-1)后面放上B;

F(A,B)= F(A,B-1)+ F(A-1,B) { A > B };

当 A = B 时 也满足  F(A,B)= F(A,B-1)+ F(A-1,B)= F(A,B-1)+ 0;

所以有: F(A,B)= F(A,B-1)+ F(A-1,B) { A >= B };

考虑三维的状况:

F(A,B,C)= F(A-1,B,C)+ F(A,B-1,C-1)+ F(A,B,C-1) {A >= B >= C}。

说明:(2011-09-19 01:32)。

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

char ABC[ 61 ][ 61 ][ 61 ][ 82 ];

int main()
{
    memset( ABC, 0, sizeof( ABC ) );
    for ( int A = 1 ; A <= 60 ; ++ A )
        ABC[ A ][ 0 ][ 0 ][ 0 ] = 1;

    for ( int A = 1 ; A <= 60 ; ++ A )
    for ( int B = 1 ; B <= 60 ; ++ B )
        if ( A >= B )
            for ( int k = 0 ; k <= 80 ; ++ k ) {
                ABC[ A ][ B ][ 0 ][ k ] += ABC[ A-1 ][ B ][ 0 ][ k ] + ABC[ A ][ B-1 ][ 0 ][ k ];
                if ( ABC[ A ][ B ][ 0 ][ k ] > 9 ) {
                     ABC[ A ][ B ][ 0 ][ k+1 ] += ABC[ A ][ B ][ 0 ][ k ]/10;
                     ABC[ A ][ B ][ 0 ][  k  ] %= 10;
                }
            }

    for ( int A = 1 ; A <= 60 ; ++ A )
    for ( int B = 1 ; B <= 60 ; ++ B )
    for ( int C = 1 ; C <= 60 ; ++ C )
        if ( A >= B && B >= C )
            for ( int k = 0 ; k <= 80 ; ++ k ) {
                ABC[ A ][ B ][ C ][ k ] += ABC[ A-1 ][ B ][ C ][ k ] + ABC[ A ][ B-1 ][ C ][ k ] + ABC[ A ][ B ][ C-1 ][ k ];
                if ( ABC[ A ][ B ][ C ][ k ] > 9 ) {
                     ABC[ A ][ B ][ C ][ k+1 ] += ABC[ A ][ B ][ C ][ k ]/10;
                     ABC[ A ][ B ][ C ][  k  ] %= 10;
                }
            }

    int n;
    while ( scanf("%d",&n) != EOF ) {
        int start = 80;
        while ( !ABC[ n ][ n ][ n ][ start ] && start > 0 ) -- start;
        while ( start >= 0 )
            printf("%d",ABC[ n ][ n ][ n ][ start -- ]);
        printf("\n\n");
    }
    return 0;
}
时间: 2024-08-02 07:34:51

zoj 2711 - Regular Words的相关文章

ZOJ 2711 Regular Words (三维Catalan数)

题目链接:ZOJ 2711 Regular Words (三维Catalan数) 题意:给出一串3*n长度的字符,其中是含有A,B,C,满足以下条件的字符串有多少种. 1.字符串中A,B,C的个数相同. 2.该字符串的前缀中 A,B,C的个数成非递减. 做法一:一个三维的Catalan数. 做法二:三维的DP. AC代码: import java.math.*; import java.util.*; public class Main{ public static void main(Stri

zoj 1010 (线段相交判断+多边形求面积)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=10 Area Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Jerry, a middle school student, addicts himself to mathematical research. Maybe the problems he has thought are

LeetCode 10. Regular Expression Matching

https://leetcode.com/problems/regular-expression-matching/description/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover

Python正则表达式Regular Expression基本用法

资料来源:http://blog.csdn.net/whycadi/article/details/2011046   直接从网上资料转载过来,作为自己的参考.这个写的很清楚.先拿来看看. 1.正则表达式re模块的基本函数. (1)findall函数的用法 findall(rule,target[,flag])是在目标字符串中找到符合规则的字符串.参数说明:rule表示规则,target表示目标字符串,[,flag]表示的是规则选项.返回的结果是一个列表.若没找到符合的,是一个空列表. 如: 因

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

zoj 2156 - Charlie&#39;s Change

题目:钱数拼凑,面值为1,5,10,25,求组成n面值的最大钱币数. 分析:dp,01背包.需要进行二进制拆分,否则TLE,利用数组记录每种硬币的个数,方便更新. 写了一个 多重背包的 O(NV)反而没有拆分快.囧,最后利用了状态压缩优化 90ms: 把 1 cents 的最后处理,其他都除以5,状态就少了5倍了. 说明:貌似我的比大黄的快.(2011-09-26 12:49). #include <stdio.h> #include <stdlib.h> #include <

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy

ZOJ - 2243 - Binary Search Heap Construction

先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal n