URAL1036——DP+高精度—— Lucky Tickets

Description

You are given a number 1 ≤ N ≤ 50. Every ticket has its 2 N-digit number. We call a ticket lucky, if the sum of its first N digits is equal to the sum of its last N digits. You are also given the sum of ALL digits in the number. Your task is to count an amount of lucky numbers, having the specified sum of ALL digits.

Input

Two space-separated numbers: N and S. Here S is the sum of all digits. Assume that 0 ≤ S ≤ 1000.

Output

The amount of lucky tickets.

Sample Input

inputoutput
2 2
4

Notes

The tickets are 0101, 0110, 1001, 1010 in the example above
大意:这类题型会拉hhh,去搜了题解,发现初始化写错了囧。。
输入两个数n,s,分别表示前面数的长度和总共长度每一位之和
先把s为奇数时特判掉,然后就是很经典的dp,dp[i][j]表示当前的为i为,j表示已经用的和
动态转移方程就是  dp[i][j] = dp[i-1][j-k](k从 0 .....j-k > 0 && k <= 9)
初始化从 0开始orz ....j也从0开始 orz....蓝后愉快地套一下高精度模板就行~(≧▽≦)/~啦啦啦
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int numlen = 105;
struct bign{
    int len,s[numlen] ;
    bign(){
        memset(s,0,sizeof(s));
        len = 1;
    }
    bign(int num){*this = num;}
    bign(const char *num){*this = num;}
    bign operator = (const int num){
        char s[numlen];
        sprintf(s,"%d",num);
        *this = s;
        return *this;
    }
    bign operator = (const char *num){
        len = strlen(num);
        while(len > 1 && num[0] == '0') num++,len--;
        for(int i = 0 ; i < len ;i++) s[i] = num[len-i-1] - '0';
        return *this;
    }
    void deal(){
        while(len > 1 &&!s[len-1]) len--;
    }
    bign operator + (const bign &a) const {
        bign ret;
        ret.len = 0;
        int top = max(len,a.len),add = 0;
        for(int i = 0 ; add || i < top; i++){
            int now = add;
            if(i < len) now += s[i];
            if(i < a.len) now += a.s[i];
            ret.s[ret.len++] = now %10;
            add = now /10;
            }
        return ret;
    }
        bign operator *(const bign &a) const {
            bign ret;
            ret.len = len + a.len;
            for(int i = 0 ; i < len ;i++){
                for(int j = 0 ; j < a.len ;j++)
                    ret.s[i+j] += s[i] * a.s[j];
            }
            for(int i = 0 ; i < ret.len; i++){
                ret.s[i+1] += ret.s[i]/10;
                ret.s[i] %= 10;
            }
            ret.deal();
            return ret;
        }
        string str() const {
            string ret = "";
            for(int i = 0 ; i <len ; i++)
                ret = char(s[i] + '0') +ret;
            return ret;
        }
};
istream& operator >> (istream &in,bign &x){
    string s;
    in >> s;
    x = s.c_str();
    return in;
}
ostream& operator << (ostream &out,const  bign &x){
    out << x.str();
    return out;
}
bign dp[55][1010];
int main()
{
    int n,s;
    while(~scanf("%d%d",&n,&s)){
        if(s % 2 == 1){
            printf("0
");
            continue;
        }
        else {
            s /= 2;
        for(int i = 0; i <= 9; i++)
            dp[1][i] = 1;
        for(int i = 2; i <= n ; i++){
            for(int j = 0; j <= s;j++){
                  for(int k = 0;k <= 9 && j - k >= 0; k++){
                    dp[i][j] = dp[i][j] + dp[i-1][j-k];
                  }
            }
          }
        }
        cout << dp[n][s] * dp[n][s] << endl;
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/zero-begin/p/4497439.html