zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    C#密封类和密封方法
    C#类和结构以及堆和栈大烩菜(本来就迷,那就让暴风来的更猛烈吧!)
    C#类、对象、方法、属性详解(摘)感谢博客园,感谢提供者。
    软件工程第十五周总结
    软件工程课堂十一(计算最长英语单词链)
    软件工程第十四周总结
    梦断代码阅读笔记03
    软件工程课堂十(找水王)
    软件工程课堂九(用户体验评价)
    软件工程第十三周总结
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4497439.html
Copyright © 2011-2022 走看看