【POJ】2346 - Lucky tickets(数位dp)
2016-08-11 21:14
363 查看
点击打开题目
Lucky tickets
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
Sample Output
Source
Ural State University Internal Contest October'2000 Students Session
明显是个数位dp的题,但是有几点不好想。
我们用数组dp[ i ] [ j ] 表示i位数(一共 2*i 位数,我们只考虑前一半),和为 j 的数有多少个,那么 2*i 位数一共可以形成 dp [ i ] [ j ]^2 个,而状态转移方程为:
dp [ i ] [ j ] = dp[ i - 1 ] [ j ];
我是先打表了,然后输出。
哦对了,我看有人的代码还用了一种压缩空间的做法,感觉特别巧,因为只用到了上一个状态的数值,所以把dp数组第一维度压缩到了2。但是没法打表啦。
代码如下:
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
int main()
{
int dp[6][50];
CLR(dp,0);
for (int i = 0 ; i < 10 ; i++)
dp[1][i] = 1;
for (int i = 2 ; i <= 5 ; i++)
{
for (int j = 0 ; j <= i * 9 ; j++)
{
for (int k = 0 ; k <= 9 ; k++)
{
if (j >= k)
dp[i][j] += dp[i-1][j-k];
}
}
}
int n;
while (~scanf ("%d",&n))
{
LL ans = 0;
n >>= 1;
for (int i = 0 ; i <= n*9 ; i++)
ans += (dp
[i] * dp
[i]); //dp的平方
printf ("%lld\n",ans);
}
return 0;
}
Lucky tickets
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3327 | Accepted: 2195 |
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
Source
Ural State University Internal Contest October'2000 Students Session
明显是个数位dp的题,但是有几点不好想。
我们用数组dp[ i ] [ j ] 表示i位数(一共 2*i 位数,我们只考虑前一半),和为 j 的数有多少个,那么 2*i 位数一共可以形成 dp [ i ] [ j ]^2 个,而状态转移方程为:
dp [ i ] [ j ] = dp[ i - 1 ] [ j ];
我是先打表了,然后输出。
哦对了,我看有人的代码还用了一种压缩空间的做法,感觉特别巧,因为只用到了上一个状态的数值,所以把dp数组第一维度压缩到了2。但是没法打表啦。
代码如下:
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
int main()
{
int dp[6][50];
CLR(dp,0);
for (int i = 0 ; i < 10 ; i++)
dp[1][i] = 1;
for (int i = 2 ; i <= 5 ; i++)
{
for (int j = 0 ; j <= i * 9 ; j++)
{
for (int k = 0 ; k <= 9 ; k++)
{
if (j >= k)
dp[i][j] += dp[i-1][j-k];
}
}
}
int n;
while (~scanf ("%d",&n))
{
LL ans = 0;
n >>= 1;
for (int i = 0 ; i <= n*9 ; i++)
ans += (dp
[i] * dp
[i]); //dp的平方
printf ("%lld\n",ans);
}
return 0;
}
相关文章推荐
- POJ 2346 Lucky tickets(数位dp求前几位数字和)
- POJ-2346 Lucky tickets(线性DP)
- POJ-2346 Lucky tickets(线性DP)
- POJ 2346 Lucky tickets [DP]
- poj 3286-数位DP
- poj2282(数位dp)
- POJ 3252 E - Round Numbers(数位dp)(模板)
- 数位DP-POJ-3252-Round Numbers
- POJ3252:Round Numbers(数位dp)
- POJ 3252 Round Numbers(数位DP)
- POJ 3252 Round Numbers(数位dp&记忆化搜索)
- 数位DP——POJ 3252 Round Number
- poj 3252 数位dp
- POJ 3252 Round Numbers (数位DP)
- 【数位DP】Round Numbers POJ - 3252
- poj Round Numbers 3252(数位dp进阶)
- POJ 3252 Round Numbers(数位dp,区间中二进制表示时0的个数大于等于1的个数的数字的个数)
- POJ 3252 Round Numbers (组合数学/数位dp)
- POJ 3286 How many 0's?(数位DP)
- POJ 3252 Round Numbers(数位DP)