码迷,mamicode.com
首页 > 其他好文 > 详细

Ural 1036 Lucky Tickets

时间:2015-09-29 21:58:07      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

 

Lucky Tickets

Time Limit: 2000ms
Memory Limit: 16384KB
This problem will be judged on Ural. Original ID: 1036
64-bit integer IO format: %lld      Java class name: (Any)
 
You are given a number 1 ≤ N ≤ 50. Every ticket has its 2N-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

2 2

Sample Output

4

Hint

The tickets are 0101, 0110, 1001, 1010 in the example above
 
解题:动态规划$dp[i][j]表示处理到了第i位,且前i位的和为j的方案数$
技术分享
  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 typedef long long LL;
  4 #define MAXN 100
  5 struct HP {
  6     int len,s[MAXN];
  7     HP() {
  8         memset(s,0,sizeof(s));
  9         len = 1;
 10     }
 11     HP operator=(const char *num) { //字符串赋值
 12         len = strlen(num);
 13         for(int i = 0; i < len; i++) s[i] = num[len-i-1]-0;
 14     }
 15     HP operator=(int num) { //int 赋值
 16         char s[MAXN];
 17         sprintf(s,"%d",num);
 18         *this = s;
 19         return *this;
 20     }
 21     HP(int num) {
 22         *this = num;
 23     }
 24     HP(const char*num) {
 25         *this = num;
 26     }
 27     string str()const { //转化成string
 28         string res = "";
 29         for(int i = 0; i < len; i++) res = (char)(s[i]+0) + res;
 30         if(res == "") res = "0";
 31         return res;
 32     }
 33     HP operator +(const HP& b) const {
 34         HP c;
 35         c.len = 0;
 36         for(int i = 0,g = 0; g||i < max(len,b.len); i++) {
 37             int x = g;
 38             if(i < len) x += s[i];
 39             if(i < b.len) x += b.s[i];
 40             c.s[c.len++] = x%10;
 41             g = x/10;
 42         }
 43         return c;
 44     }
 45     void clean() {
 46         while(len > 1 && !s[len-1]) len--;
 47     }
 48 
 49     HP operator *(const HP& b) {
 50         HP c;
 51         c.len = len + b.len;
 52         for(int i = 0; i < len; i++)
 53             for(int j = 0; j < b.len; j++)
 54                 c.s[i+j] += s[i]*b.s[j];
 55         for(int i = 0; i < c.len-1; i++) {
 56             c.s[i+1] += c.s[i]/10;
 57             c.s[i] %= 10;
 58         }
 59         c.clean();
 60         return c;
 61     }
 62     HP operator - (const HP& b) {
 63         HP c;
 64         c.len = 0;
 65         for(int i = 0,g = 0; i < len; i++) {
 66             int x = s[i]-g;
 67             if(i < b.len) x -= b.s[i];
 68             if(x >= 0) g = 0;
 69             else {
 70                 g = 1;
 71                 x += 10;
 72             }
 73             c.s[c.len++]=x;
 74         }
 75         c.clean();
 76         return c;
 77     }
 78     HP operator /(const HP &b) {
 79         HP c, f = 0;
 80         for(int i = len-1; i >= 0; i--) {
 81             f = f*10;
 82             f.s[0] = s[i];
 83             while(f >= b) {
 84                 f = f - b;
 85                 c.s[i]++;
 86             }
 87         }
 88         c.len = len;
 89         c.clean();
 90         return c;
 91     }
 92     HP operator % (const HP &b) {
 93         HP r = *this / b;
 94         r = *this - r*b;
 95         return r;
 96     }
 97     HP operator /= (const HP &b) {
 98         *this  = *this / b;
 99         return *this;
100     }
101     HP operator %= (const HP &b) {
102         *this = *this % b;
103         return *this;
104     }
105     bool operator < (const HP& b) const {
106         if(len != b.len) return len < b.len;
107         for(int i = len-1; i >= 0; i--)
108             if(s[i] != b.s[i]) return s[i] < b.s[i];
109         return false;
110     }
111     bool operator > (const HP& b) const {
112         return b < *this;
113     }
114     bool operator <= (const HP& b) {
115         return !(b < *this);
116     }
117     bool operator == (const HP& b) {
118         return !(b < *this) && !(*this < b);
119     }
120     bool operator != (const HP &b) {
121         return !(*this == b);
122     }
123     HP operator += (const HP& b) {
124         *this = *this + b;
125         return *this;
126     }
127     bool operator >= (const HP &b) {
128         return *this > b || *this == b;
129     }
130 };
131 istream& operator >>(istream &in, HP& x) {
132     string s;
133     in >> s;
134     x = s.c_str();
135     return in;
136 }
137 ostream& operator <<(ostream &out, const HP& x) {
138     out << x.str();
139     return out;
140 }
141 HP dp[101][1001];
142 int main(){
143     dp[0][0] = 1;
144     for(int i = 1; i < 51; ++i)
145         for(int j = 0; j <= i*9; ++j)
146             for(int k = 0; k < 10; ++k)
147                 dp[i][j + k] = dp[i][j + k] + dp[i-1][j];
148     int n,s;
149     while(~scanf("%d%d",&n,&s)){
150         if(s&1) puts("0");
151         else cout<<dp[n][s>>1]*dp[n][s>>1]<<endl;
152     }
153     return 0;
154 }
View Code

 

 

Ural 1036 Lucky Tickets

标签:

原文地址:http://www.cnblogs.com/crackpotisback/p/4847215.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!