UVa 11375 Matches

大年夜的写代码果然状态非常之差...感觉特别困,连个高精度都折腾了我好久。还是刘汝佳《训练指南》里的一道例题,解题思路其实也差不多,但是想对书里面的内容再讲讲。其中d[i]是代表i个火柴棒恰好能构成的正整数数目(不包含整数0),然后有点类似于动态规划的做法,通过已知的d[]求出剩下的d[]。

不过仔细想来貌似有点问题。例如已知d[j],那么d[j+num[0]]+=d[j].那么新加的数字0是不是应该也有好几种排列方式呢?例如d[j]中有一个数字123,那么d[j+num[0]]是不是应该有1230,1203,1023...等等各种方式呢?其实不是的d[j+num[0]]+=d[j]这个式子其实都是在数的末尾添加新数字的。123新增一个0就是1230,而1203则是120新加一个3完成的。所以,!(i==0&&j==0)就可以理解了。第一个数字不能是0,否则会产生前置0的问题。至于那个整数0,在N>=6时,每个答案都要加上1。

头有点晕,哈哈哈,写得可能不是很清楚,具体还是参考代码吧!另:用string还是会超时,可能是由于频繁的内存分配问题....

#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#define MAX 2000+5
#define MAXL 1000
using namespace std;

char d[MAX][MAXL],ans[MAX][MAXL];//d[i]为恰好用i根火柴棍能完成的正整数,ans[i]为最后的解答
int dlen[MAX],alen[MAX];//dlen[i]对应字符串d[i]的长度,alen同理对应ans
int num[10]={6,2,5,5,4,5,6,3,7,6};//10个数字对应的火柴棍数

void add(int&,int,char*,const char*);
int main()
{
    d[0][0]='1',dlen[0]=1;
    d[1][0]='0',dlen[1]=1;//这里必须要初始化,否则按照下述高精度方式,ans[i]可能会产生空

    for(int i=0;i<MAX;++i){
        for(int j=0;j<10;++j){
            if(!(i==0&&j==0)&&i+num[j]<MAX) add(dlen[i+num[j]],dlen[i],d[i+num[j]],d[i]);
        }
    }
    for(int i=1;i<MAX;++i){
        for(int j=1;j<=i;++j) add(alen[i],dlen[j],ans[i],d[j]);
        if(i>=6) add(alen[i],1,ans[i],"1");//火柴棍数大于6,结果要加1
    }

    int N;
    while(cin>>N){
        int pos;
        for(pos=alen[N]-1;pos>=0;--pos) cout<<ans[N][pos];
        cout<<endl;
    }

    return 0;
}

void add(int& la,int lb,char* a,const char* b)//高精度运算
{
    int len;
    len=max(la,lb);

    int sum,overflow=0;
    for(int i=0;i<len;++i){
        if(lb<=i){//被加的字符长度不足
            sum=a[i]-'0'+overflow;
        }
        else{
            if(la<=i){a[i]='0';++la;}//加的字符串长度不足,且长度值需修改
            sum=a[i]-'0'+b[i]-'0'+overflow;
        }
        a[i]=sum%10+'0';
        overflow=sum/10;
    }
    if(overflow){//还有进位
        a[la++]=overflow+'0';
    }

    return;
}
时间: 2024-08-26 05:40:56

UVa 11375 Matches的相关文章

uva 11375 - Matches(递推)

题目链接:11375 - Matches 题目大意:给出n根火柴,问说能组成多少种数字,要求说0不能打头. 解题思路:d[i]表示i根火柴能够组成的数量,d[i+c[j]] = d[i+c[j]] + d[i]; 最后dp[i]表示小于等于i根火柴能组成的数量,dp[i]=∑jidp[j]. 高精度. #include <cstdio> #include <cstring> #include <iostream> using namespace std; const i

【Java】【高精度】【递推】UVA - 11375 - Matches

d[i+c[j]]+=d[i](c[j]是拼成j所需的火柴数) d[0]=1: 别忘了不能有前导零,所以当i为零时,不要尝试去拼成零.反而应该在n>=6时,最后给答案加1(单独拼出0). import java.util.*; import java.io.*; import java.math.*; public class Main{ static BigInteger[] d=new BigInteger[2010]; static int[] c=new int[]{6,2,5,5,4,

uva 11357 Matches

// uva 11357 Matches // // 题目大意: // // 给你n个火柴,用这n个火柴能表示的非负数有多少个,不能含有前导零 // // 解题思路: // // f[i] 表示正好用i根火柴所能表示的整数的个数,则f[i + c[j]] += f[i]; // c[j] 为数字j所需要的火柴的数量,因为当j==0并且i==0的时候,我们不允许这样的 // 转移,因为i=0的时候不允许使用数字0,当n>=6时,再将0加上.我的理解是,当开始可以 // 用0的时候,那么后面的递推,

dp题目列表

10271 - Chopsticks 10739 - String to Palindrome 10453 - Make Palindrome 10401 - Injured Queen Problem 825 - Walking on the Safe Side 10617 - Again Palindrome 10201 - Adventures in Moving - Part IV 11258 - String Partition 10564 - Paths through the Ho

uva 111 History Grading(DP初步应用)

uva 111 History Grading Many problems in Computer Science involve maximizing some measure according to constraints. Consider a history exam in which students are asked to put several historical events into chronological order. Students who order all

UVa OJ 127 - &quot;Accordian&quot; Patience (“手风琴”纸牌)

UVa OJ 127 - "Accordian" Patience ("手风琴"纸牌) Time limit: 3.000 seconds 限时:3.000秒 Problem 问题 You are to simulate the playing of games of "Accordian" patience, the rules for which are as follows: 模拟玩一个"手风琴"纸牌游戏,规则如下: D

UVA 之10010 - Where&#39;s Waldorf?

 Where's Waldorf?  Given a m by n grid of letters, ( ), and a list of words, find the location in the grid at which the word can be found. A word matches a straight, uninterrupted line of letters in the grid. A word can match the letters in the grid

uva 101 History Grading

Background Many problems in Computer Science involve maximizing some measure according to constraints. Consider a history exam in which students are asked to put several historical events into chronological order. Students who order all the events co

Uva 127 poj 1214 `Accordian&#39;&#39; Patience

 ``Accordian'' Patience  You are to simulate the playing of games of ``Accordian'' patience, the rules for which are as follows: Deal cards one by one in a row from left to right, not overlapping. Whenever the card matches its immediate neighbour on