POJ-2346 Lucky tickets(线性DP)
Lucky tickets
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3298 Accepted: 2174
Description
The public transport administration of Ekaterinburg is anxious about the fact that passengers don’t like to pay for passage doing their best to avoid the fee. All the measures that had been taken (hard currency premiums for all of the chiefs, increase in conductors’ salaries, reduction of number of buses) were in vain. An advisor especially invited from the Ural State University says that personally he doesn’t buy tickets because he rarely comes across the lucky ones (a ticket is lucky if the sum of the first three digits in its number equals to the sum of the last three ones). So, the way out is found — of course, tickets must be numbered in sequence, but the number of digits on a ticket may be changed. Say, if there were only two digits, there would have been ten lucky tickets (with numbers 00, 11, …, 99). Maybe under the circumstances the ratio of the lucky tickets to the common ones is greater? And what if we take four digits? A huge work has brought the long-awaited result: in this case there will be 670 lucky tickets. But what to do if there are six or more digits?
So you are to save public transport of our city. Write a program that determines a number of lucky tickets for the given number of digits. By the way, there can’t be more than 10 digits on one ticket.
Input
Input contains a positive even integer N not greater than 10. It’s an amount of digits in a ticket number.
Output
Output should contain a number of tickets such that the sum of the first N/2 digits is equal to the sum of the second half of digits.
Sample Input
4
Sample Output
670
动态规划,求一个N位数是否是幸运数字,那么可以枚举前N/2可以组成的所有数字的个数,答案就是每个数字的个数的平方和。dp[i][k]表示i位组成k的个数,用线性dp,递推就好了
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
int dp[6][46];
int main()
{
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(int i=1;i<6;i++)
{
//j表示当前这个位上的数字
for(int j=0;j<=9;j++)
{
for(int k=9*i;k>=j;k--)
{
dp[i][k]+=dp[i-1][k-j];
}
}
}
int n,ans;
while(scanf("%d",&n)!=EOF)
{
n/=2;
ans=0;
for(int i=0;i<=45;i++)
ans+=dp[n][i]*dp[n][i];
printf("%d\n",ans);
}
return 0;
}
POJ-2346 Lucky tickets(线性DP)的更多相关文章
- poj 2346 Lucky tickets(区间dp)
题目链接:http://poj.org/problem?id=2346 思路分析:使用动态规划解法:设函数 d( n, x )代表长度为n且满足左边n/2位的和减去右边n/2位的和为x的数的数目. 将 ...
- Codeforces Gym 100418J Lucky tickets 数位DP
Lucky ticketsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...
- POJ 1458-Common Subsequence(线性dp/LCS)
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39009 Accepted: 15 ...
- poj 1836 Alignment(线性dp)
题目链接:http://poj.org/problem?id=1836 思路分析:假设数组为A[0, 1, …, n],求在数组中最少去掉几个数字,构成的新数组B[0, 1, …, m]满足条件B[0 ...
- poj 2593 Max Sequence(线性dp)
题目链接:http://poj.org/problem?id=2593 思路分析:该问题为求给定由N个整数组成的序列,要求确定序列A的2个不相交子段,使这m个子段的最大连续子段和的和最大. 该问题与p ...
- POJ 2346:Lucky tickets
Lucky tickets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3247 Accepted: 2136 Des ...
- poj 1050 To the Max(线性dp)
题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...
- DP+高精度 URAL 1036 Lucky Tickets
题目传送门 /* 题意:转换就是求n位数字,总和为s/2的方案数 DP+高精度:状态转移方程:dp[cur^1][k+j] = dp[cur^1][k+j] + dp[cur][k]; 高精度直接拿J ...
- POJ 2479-Maximum sum(线性dp)
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33918 Accepted: 10504 Des ...
随机推荐
- Sublime text2插件
Sublime插件: Sublime有好几种安装插件的方法,但是最好用也是最长用的是ctrl+shift+p. 第一步: 使用ctrl+` 调出Sublime控制台,在控制台中输入 import ur ...
- 关于python单方法的类
1.大部分情况下,你拥有一个单方法类的原因是需要存储某些额外的状态来给方法使用. 此种情况下可以使用闭包代替,参考 javascript的闭包计数器实现,python实现各种方法来实现计数器 关于这个 ...
- MAP参数估计
(学习这部分内容大约需要40分钟) 摘要 在贝叶斯参数估计中, 除了先验是特别选定的情况下, 通常要积分掉所有模型参数是没有解析解的. 在这种情况下, 最大后验(maximum a posterior ...
- Xcode文件被锁定:The file ".xcodeproj" could not be unlocked
同事从svn上面checkout项目到本地,通过xcode打开的时候提示的这个问题. The file "xcodeproj" could not be unlocked. Cou ...
- getActionBar().setDisplayHomeAsUpEnabled(true)报空指针(已解决)
今天捣鼓了一下午.getActionBar().setDisplayHomeAsUpEnabled(true)总是报空指针.在我的还有一个Android4.4.2的项目中就没有一点问题.我还以为是我自 ...
- AESDK开发之UI消息响应
UI创建: 在该入口下 case PF_Cmd_PARAMS_SETUP: //.... break; 必须在末尾指定UI数目,UI数目一般是枚举,如果和枚举长度不一致也会报错.所以最好是直接修改枚举 ...
- MQTT-C-PUB
/* ============================================================================ Name : mqtest ...
- linux下getsockopt和setsockopt详解及测试
linux下getsockopt和setsockopt详解及测试 NAME 名字 getsockopt, setsockopt - get and set options on sockets 获取或 ...
- 在linux下如何判断是否已经安装某个软件?
如果你使用rpm -ivh matlab装的,用rpm -qa | grep matlab肯定是能够找到的. 如果你是用make && make install装的.那么最好直接去找执 ...
- vc 关于局部刷新
在绘制图像对象的时候,时刻获取其所占范围大小,并使用InvalidateRect( m_rectRefresh);刷新,但是光这样还是不行的要在onDraw()函数里获取PAINTSTRUCT结构的无 ...