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 ...
随机推荐
- iphone弹出窗口效果的制作(Core animation, CALayer)
效果类似人人网微薄客户端的弹出效果 static CGFloat kTransitionDuration = 0.3; - (void)initView { UIWindow *window = [U ...
- PMP用语集
AC actual cost 实际成本 ACWP actual cost of work performed 已完工作实际成本 BAC budget at completion 完工预算 BCWP b ...
- IOS指纹识别调用
最近正在开发的一个app需要加入指纹识别的功能,先搜索一下找到官方文档,简单易懂: https://developer.apple.com/library/ios/documentation/Loca ...
- Dubbo -- 系统学习 笔记 -- 示例 -- 直连提供者
Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 直连提供者 在开发及测试环境下,经常需要绕过注册中心,只测试指定服务提供者,这时候 ...
- 5 -- Hibernate的基本用法 --1 3 流行的ORM框架简介
⊙ JPA : JPA本身只是一种ORM规范,并不是ORM产品.它是Java EE规范制定者向开源世界学习的结果.JPA实体与Hibernate PO十分相似,甚至JPA实体完全可作为Hibernat ...
- spray json, jackson 转到type时多key和少key的比较
有组合关系的三个class定义 A { B { C {...} ... } ... } 每个class都有loadFromJson和writeAsJson方法.过去几年,三个class里的成员变量一直 ...
- 邮件服务器日志:/var/log/maillog
/var/log/maillog会记录包含系统上运行的邮件服务器的日志信息,比如记录 postfix 或 sendmail 运行时的日志信息 [root@localhost ~]# tail /var ...
- ISD9160学习笔记02_搭建NuMicro开发环境
开发环境这边没什么好说的,烧写玩了玩录音的测试程序. 1. 烧写工具 昨晚先尝试了下烧写工具(NuMicro ICP Programming Tool 1.30.6491.exe),板子自带了烧写器, ...
- 《转载》Eclipse项目上传码云
本文转载自http://blog.csdn.net/izzyliao/article/details/53074452 把Eclipse项目上传到码云的步骤: 1.登录码云:新建项目 2.输入项目名: ...
- linux系统环境搭建
一.安装jdk 参考帖子 用yum安装JDK(CentOS) 1.查看yum库中都有哪些jdk版本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 [r ...