您的位置:首页 > 其它

Ural 1036 Lucky Tickets

2015-09-29 20:27 281 查看

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的方案数$

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: