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;
    }
    

      

  • 相关阅读:
    存储过程系列之存储过程sql查询存储过程的使用
    SQL Server 连接字符串和身份验证详解
    存储过程
    Objective-C:MRC(引用计数器)在OC内部的可变对象是适用的,不可变对象是不适用的(例如 NSString、NSArray等)
    Objective-C:MRC(引用计数器)获得对象所有权的方式(init、retain、copy等)
    Objective-C:MRC手动释放对象内存举例(引用计数器)
    C语言:内存的分配与管理
    Objective-C:继承、分类(Category、extension)、协议(protocol),个人理解,仅供参考
    Objective-C:在类中设置不同协议
    Objective-C:继承的体现
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4497439.html
Copyright © 2011-2022 走看看