Ural 1036 Lucky Tickets
2015-09-29 20:27
281 查看
Lucky Tickets
Time Limit: 2000msMemory 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的方案数$
#include <bits/stdc++.h> using namespace std; typedef long long LL; #define MAXN 100 struct HP { int len,s[MAXN]; HP() { memset(s,0,sizeof(s)); len = 1; } HP operator=(const char *num) { //字符串赋值 len = strlen(num); for(int i = 0; i < len; i++) s[i] = num[len-i-1]-'0'; } HP operator=(int num) { //int 赋值 char s[MAXN]; sprintf(s,"%d",num); *this = s; return *this; } HP(int num) { *this = num; } HP(const char*num) { *this = num; } string str()const { //转化成string string res = ""; for(int i = 0; i < len; i++) res = (char)(s[i]+'0') + res; if(res == "") res = "0"; return res; } HP operator +(const HP& b) const { HP c; c.len = 0; for(int i = 0,g = 0; g||i < max(len,b.len); i++) { int x = g; if(i < len) x += s[i]; if(i < b.len) x += b.s[i]; c.s[c.len++] = x%10; g = x/10; } return c; } void clean() { while(len > 1 && !s[len-1]) len--; } HP operator *(const HP& b) { HP c; c.len = len + b.len; for(int i = 0; i < len; i++) for(int j = 0; j < b.len; j++) c.s[i+j] += s[i]*b.s[j]; for(int i = 0; i < c.len-1; i++) { c.s[i+1] += c.s[i]/10; c.s[i] %= 10; } c.clean(); return c; } HP operator - (const HP& b) { HP c; c.len = 0; for(int i = 0,g = 0; i < len; i++) { int x = s[i]-g; if(i < b.len) x -= b.s[i]; if(x >= 0) g = 0; else { g = 1; x += 10; } c.s[c.len++]=x; } c.clean(); return c; } HP operator /(const HP &b) { HP c, f = 0; for(int i = len-1; i >= 0; i--) { f = f*10; f.s[0] = s[i]; while(f >= b) { f = f - b; c.s[i]++; } } c.len = len; c.clean(); return c; } HP operator % (const HP &b) { HP r = *this / b; r = *this - r*b; return r; } HP operator /= (const HP &b) { *this = *this / b; return *this; } HP operator %= (const HP &b) { *this = *this % b; return *this; } bool operator < (const HP& b) const { if(len != b.len) return len < b.len; for(int i = len-1; i >= 0; i--) if(s[i] != b.s[i]) return s[i] < b.s[i]; return false; } bool operator > (const HP& b) const { return b < *this; } bool operator <= (const HP& b) { return !(b < *this); } bool operator == (const HP& b) { return !(b < *this) && !(*this < b); } bool operator != (const HP &b) { return !(*this == b); } HP operator += (const HP& b) { *this = *this + b; return *this; } bool operator >= (const HP &b) { return *this > b || *this == b; } }; istream& operator >>(istream &in, HP& x) { string s; in >> s; x = s.c_str(); return in; } ostream& operator <<(ostream &out, const HP& x) { out << x.str(); return out; } HP dp[101][1001]; int main(){ dp[0][0] = 1; for(int i = 1; i < 51; ++i) for(int j = 0; j <= i*9; ++j) for(int k = 0; k < 10; ++k) dp[i][j + k] = dp[i][j + k] + dp[i-1][j]; int n,s; while(~scanf("%d%d",&n,&s)){ if(s&1) puts("0"); else cout<<dp [s>>1]*dp [s>>1]<<endl; } return 0; }
View Code
相关文章推荐
- 别让你的简历15秒后被扔掉的秘籍
- 信息系统项目管理师2015年9月28日作业
- The Pilots Brothers' refrigerator(dfs)
- hdu 5454 Excited Database(线段树)
- 基于zbus的MySQL透明代理(<100行)
- JSP中forward和redirect的区别
- TCP/IP简介,转载自实验楼
- Add and Search Word - Data structure design
- HDU5492 Find a path DP
- 编程之美
- 前端开发工程师应该关注什么?
- 导航地图4_地理编码和反编码
- Arduino debug 实时单步调试.
- DevOps实战:Graphite监控上手指南
- hdu 5491 The Next(ICPC合肥赛)
- 2016华为上机题二(数字频率)
- 设置默认登陆账户
- mysql同一个表和不同表的一列复制到另一列的方法
- Crazy English on the road
- Jquery datatable jsonp 显示后台json数据