UVa 11375 Matches:DP&高精度

11375 - Matches

Time limit: 2.000 seconds

http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=2370

We can make digits with matches as shown below:

Given N matches, find the number of different numbers representable using the matches. We shall only make numbers greater than or equal to 0, so no negative signs should be used. For instance, if you have 3 matches, then you can only make the numbers 1 or 7. If you have 4 matches, then you can make the numbers 1, 4, 7 or 11. Note that leading zeros are not allowed (e.g. 001, 042, etc. are illegal). Numbers such as 0, 20, 101 etc. are permitted, though.

Input

Input contains no more than 100 lines. Each line contains one integer N (1 ≤ N ≤ 2000).

Output

For each N, output the number of different (non-negative) numbers representable if you have N matches.

Sample Input

3
4

Sample Output

2
4

简单DP。

完整代码:

/*0.076s*/

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 2001;
// 本文URL地址:http://www.bianceng.cn/Programming/sjjg/201410/45373.htm
const int c[] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6}; ///  c[i]表示数字i需要的火柴数  

struct node
{
    int p[500], len;  

    node()
    {
        memset(p, 0, sizeof(p));
        len = 0;
    }  

    node(int a)
    {
        p[0] = a;
        len = 1;
    }  

    node operator + (const node& a) const
    {
        node b;
        b.len = max(len, a.len);
        for (int i = 0; i < b.len; i++)
        {
            b.p[i] += p[i] + a.p[i];
            b.p[i + 1] = b.p[i] / 10;
            b.p[i] %= 10;
        }
        if (b.p[b.len] > 0) b.len++;
        return b;
    }  

    node operator += (const node& a)
    {
        *this = *this + a;
        return *this;
    }  

    void out()
    {
        if (len == 0) putchar('0');
        else
        {
            for (int i = len - 1; i >= 0; i--)
                printf("%d", p[i]);
        }
        putchar(10);
    }
} d[maxn];  

int main()
{
    int i, j, n;
    d[0] = node(1);
    for (i = 0; i < maxn; ++i)
        for (j = 0; j < 10; ++j)
            if (i + c[j] < maxn && (i || j))
                d[i + c[j]] += d[i];
    d[6] += node(1);
    for (i = 2; i < maxn; ++i) d[i] += d[i - 1];
    while (~scanf("%d", &n))
        d[n].out();
    return 0;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索int
, const
, node
, numbers
, len
matches
gb11375、,以便于您获取更多的相关知识。

时间: 2024-10-31 22:02:38

UVa 11375 Matches:DP&amp;高精度的相关文章

UVa 10157 Expressions:组合数学&amp;amp;高精度

10157 - Expressions Time limit: 10.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=34&page=show_problem&problem=1098 Let X be the set of correctly built parenthesis expressions. The elements of X a

UVa 10128 Queue (DP)

10128 - Queue Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1069 既然有最优子结构,不妨从DP的角度来思考. 有三个不同的维度:总人数N,从前看到的人P,从后看到的人R. 假设现在队列由i-1个人变成了i个,由于谁后进到队列是无所谓的,不

UVa 324 Factorial Frequencies:高精度

324 - Factorial Frequencies Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=260 In an attempt to bolster her sagging palm-reading business, Madam Phoenix

UVa 10023 Square root:高精度及开平方公式

10023 - Square root Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=964 The Problem You are to determinate X by given Y, from expression The Input The first line is the n

算法:uva 672 Gangsters( dp )

题目大意: 有个酒店的门会改变尺寸,变化范围是[0,k],这个门每秒钟尺寸可以变大1,可以 减小1,也可以不变. 现在有n个人,他们的尺寸为si,每个人在ti时刻想要进入酒店,只有在ti时刻 酒店门的尺寸恰好和这个人的尺寸大小相等,这个人才可以进入. 每个人有一个值Pi,当某人进入酒 店,酒店就会增加Pi值. 在[0,T]这段时间内(0秒时酒店的门尺寸状态是0),求让一些人进入酒店 ,使得总Pi值最大. 思路: 开始时,以为同一时间两个人不能同时进入,结果WA了很久. 其 实如果是同一时间,两个

算法题之UVA 11081 Strings(dp)

string by combining two subsequences from the first two strings. After deleting 0 or more characters from a string we can get its subsequence. For example "a", "b", "c", "ab", "ac", "bc" and &quo

UVA 10405 Longest Common Subsequence:简单DP

省赛还有不到50天了,自己DP这块实在是弱,准备就拿着些天狂刷DP了. http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1346 大意: 求两个字符串的最长公共子序列. 思路:水题,不过需要注意的就是字符串里可能会出现空格,需要用gets,真是坑. 更多精彩内容:http://www.bianceng.cnhttp://www.biancen

uva 10688:The Poor Giant(区间dp)

题目链接: uva-10688 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=514&page=show_problem&problem=1629 题意 有n个苹果,和一个数k,第i个苹果的重量是k+i(1<=i<=n). 已知其中只有一个苹果是甜的, 所有比它重量轻的都是苦的,比它重的都是酸的. 为了要找出甜的苹果,就要去一个一个地吃它,且吃了咬了苹果

UVa 1362 Exploring Pyramids:多叉树遍历&amp;amp;DP

1362 - Exploring Pyramids Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=469&page=show_problem&problem=4108 Archaeologists have discovered a new set of hidden caves in one of the Egy