题目链接:http://poj.org/problem?id=2346

思路分析:使用动态规划解法:设函数 d( n, x )代表长度为n且满足左边n/2位的和减去右边n/2位的和为x的数的数目。

将一个长度为n的数看做n个数字 A1, A2....An ( 0 <= Ai <= 9  ),将字符分为两个集合{ A1, A2....An/2 } 与 { An/2+1.....An };

问题转换为求集合中元素和相等的数目。先从每个集合中选出一个元素,求它们差为a的可能数目,即d( 2, a );

再求两个集合剩下的元素和的差为 x - a 的数目,即d( n -2, x -a );所以动态递归方程为 d( n, x ) = ∑ d( n - 2, x - a ) * d( 2, a );  (-9<=a<=9)

可以使用记忆搜索求解。

代码如下:

#include <stdio.h>
#include <math.h>
#include <string.h> const int MAX_N = ;
int dp[MAX_N][MAX_N]; int d( int n, int x )
{
if ( dp[n][x] != - )
return dp[n][x]; if ( n == )
{
int sum = ;
for ( int a = ; a <= ; ++a )
for ( int b = ; b <= ; ++b )
if (a - b == x) sum++;
return dp[n][x] = sum;
}
else
{
int a, b, sum = ; for ( b = -; b <= ; ++b )
{
a = x - b;
if (a >= - * (n - ) / && a <= * (n - ) / )
sum += d(n - , abs(a)) * d(, abs(b) );
}
return dp[n][x] = sum;
}
} int main()
{
int n;
long long ans = ; memset(dp, -, sizeof(dp)); scanf("%d", &n);
ans = d(n, );
printf("%lld\n", ans); return ;
}

poj 2346 Lucky tickets(区间dp)的更多相关文章

  1. Codeforces Gym 100418J Lucky tickets 数位DP

    Lucky ticketsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...

  2. POJ - 3280Cheapest Palindrome-经典区间DP

    POJ - 3280 Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & ...

  3. poj 2955 括号匹配 区间dp

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6033   Accepted: 3220 Descript ...

  4. POJ 3280 Cheapest Palindrome (区间DP) 经典

    <题目链接> 题目大意: 一个由小写字母组成的字符串,给出字符的种类,以及字符串的长度,再给出添加每个字符和删除每个字符的代价,问你要使这个字符串变成回文串的最小代价. 解题分析: 一道区 ...

  5. POJ 1141 Brackets Sequence(区间DP, DP打印路径)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  6. POJ 2176 Folding(区间DP)

    题意:给你一个字符串,请把字符串压缩的尽量短,并且输出最短的方案. 例如:AAAAA可压缩为5(A), NEERCYESYESYESNEERCYESYESYES可压缩为2(NEERC3(YES)). ...

  7. POJ 2955 Brackets (区间DP,常规)

    题意: 给出一个字符串,其中仅仅含 “ ( ) [ ] ” 这4钟符号,问最长的合法符号序列有多长?(必须合法的配对,不能混搭) 思路: 区间DP的常规问题吧,还是枚举区间[i->j]再枚举其中 ...

  8. POJ - 2955 Brackets (区间DP)

    题目: 给出一个有括号的字符串,问这个字符串中能匹配的最长的子串的长度. 思路: 区间DP,首先枚举区间长度,然后在每一个长度中通过枚举这个区间的分割点来更新这个区间的最优解.还是做的少. 代码: / ...

  9. POJ 3280 Cheapest Palindrome ( 区间DP && 经典模型 )

    题意 : 给出一个由 n 中字母组成的长度为 m 的串,给出 n 种字母添加和删除花费的代价,求让给出的串变成回文串的代价. 分析 :  原始模型 ==> 题意和本题差不多,有添和删但是并无代价 ...

随机推荐

  1. Foundation NSMutableArray遍历,选取出符合条件的所有对象

    一.查找数组中一个元素,找到后立即返回 当遍历数组只需要返回其中一个符合条件的元素时,使用 indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, ...

  2. Auto-Layout 的各种坑Unable to create description in descriptionForLayoutAttribute_layoutItem_coefficient. Something is nil'

    我们的很多人现在都在使用autolayout,用着也是非常爽但是有了这个东西以后更爽 很省事,什么都不用自己搞.Xcode完全搞定了,但是我终于为自己的懒惰付出了代价,再iphone4怎么运行怎么cr ...

  3. The Painter's Partition Problem Part I

    (http://leetcode.com/2011/04/the-painters-partition-problem.html) You have to paint N boards of leng ...

  4. La=LaULb (单链表)

    #include<stdio.h> typedef struct LNode { int data; struct LNode *next; }LNode,*LinkList; void ...

  5. jQuery学习之结构解析

    jQuery内核解析 1.jQuery整体的结构是一个匿名函数 (function( window, undefined ) {})(window); 2.jQuery就是一个很普通的函数,也是一个很 ...

  6. JavaScript 堆

    1.IE中不兼容ajax中data最后一个参数加逗号,其余chrome Firefox均支持. code: $("document ").ready(function() { $( ...

  7. GoWithTheFlow

    GoWithTheFlow http://notes.jetienne.com/2011/07/17/gowiththeflow.js-async-flow-control-with-a-zen-to ...

  8. Win32 SecuritySetting

    http://flylib.com/books/en/2.21.1.207/1/ http://blogs.technet.com/b/heyscriptingguy/archive/2011/11/ ...

  9. TMT行业分析师

    诚聘英才 - 传媒梦工场 TMT行业分析师 工作经验:  2年以上 发布日期:  2013-01-04 最低学历:  本科 管理经验:  否 工作性质:  全职 招聘人数:  1人 职位类别:  金融 ...

  10. mac 如何显示隐藏文件和.点开头文件?

    如果想在Finder中就能直观看到隐藏文件,那么在终端中输入以下下命令: defaults write com.apple.Finder AppleShowAllFiles YES killall F ...