思路:

大数01背包。

实现:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #define MAXN 9999  
  5 #define MAXSIZE 10  
  6 #define DLEN 4  
  7 using namespace std;
  8 int n,k;
  9   
 10 class BigNum  
 11 {   
 12 private:   
 13     int a[500];    //可以控制大数的位数   
 14     int len;       //大数长度  
 15 public:   
 16     BigNum(){ len = 1;memset(a,0,sizeof(a)); }   //构造函数  
 17     BigNum(const int);       //将一个int类型的变量转化为大数  
 18     BigNum(const char*);     //将一个字符串类型的变量转化为大数  
 19     BigNum(const BigNum &);  //拷贝构造函数  
 20     BigNum &operator=(const BigNum &);   //重载赋值运算符,大数之间进行赋值运算  
 21   
 22     friend istream& operator>>(istream&,  BigNum&);   //重载输入运算符  
 23     friend ostream& operator<<(ostream&,  BigNum&);   //重载输出运算符  
 24   
 25     BigNum operator+(const BigNum &) const;   //重载加法运算符,两个大数之间的相加运算   
 26     BigNum operator-(const BigNum &) const;   //重载减法运算符,两个大数之间的相减运算   
 27     BigNum operator*(const BigNum &) const;   //重载乘法运算符,两个大数之间的相乘运算   
 28     BigNum operator/(const int   &) const;    //重载除法运算符,大数对一个整数进行相除运算  
 29   
 30     BigNum operator^(const int  &) const;    //大数的n次方运算  
 31     int    operator%(const int  &) const;    //大数对一个int类型的变量进行取模运算      
 32     bool   operator>(const BigNum & T)const;   //大数和另一个大数的大小比较  
 33     bool   operator>(const int & t)const;      //大数和一个int类型的变量的大小比较  
 34   
 35     void print();       //输出大数  
 36 };   
 37 BigNum::BigNum(const int b)     //将一个int类型的变量转化为大数  
 38 {   
 39     int c,d = b;  
 40     len = 0;  
 41     memset(a,0,sizeof(a));  
 42     while(d > MAXN)  
 43     {  
 44         c = d - (d / (MAXN + 1)) * (MAXN + 1);   
 45         d = d / (MAXN + 1);  
 46         a[len++] = c;  
 47     }  
 48     a[len++] = d;  
 49 }  
 50 BigNum::BigNum(const char*s)     //将一个字符串类型的变量转化为大数  
 51 {  
 52     int t,k,index,l,i;  
 53     memset(a,0,sizeof(a));  
 54     l=strlen(s);     
 55     len=l/DLEN;  
 56     if(l%DLEN)  
 57         len++;  
 58     index=0;  
 59     for(i=l-1;i>=0;i-=DLEN)  
 60     {  
 61         t=0;  
 62         k=i-DLEN+1;  
 63         if(k<0)  
 64             k=0;  
 65         for(int j=k;j<=i;j++)  
 66             t=t*10+s[j]-'0';  
 67         a[index++]=t;  
 68     }  
 69 }  
 70 BigNum::BigNum(const BigNum & T) : len(T.len)  //拷贝构造函数  
 71 {   
 72     int i;   
 73     memset(a,0,sizeof(a));   
 74     for(i = 0 ; i < len ; i++)  
 75         a[i] = T.a[i];   
 76 }   
 77 BigNum & BigNum::operator=(const BigNum & n)   //重载赋值运算符,大数之间进行赋值运算  
 78 {  
 79     int i;  
 80     len = n.len;  
 81     memset(a,0,sizeof(a));   
 82     for(i = 0 ; i < len ; i++)   
 83         a[i] = n.a[i];   
 84     return *this;   
 85 }  
 86 istream& operator>>(istream & in,  BigNum & b)   //重载输入运算符  
 87 {  
 88     char ch[MAXSIZE*4];  
 89     int i = -1;  
 90     in>>ch;  
 91     int l=strlen(ch);  
 92     int count=0,sum=0;  
 93     for(i=l-1;i>=0;)  
 94     {  
 95         sum = 0;  
 96         int t=1;  
 97         for(int j=0;j<4&&i>=0;j++,i--,t*=10)  
 98         {  
 99             sum+=(ch[i]-'0')*t;  
100         }  
101         b.a[count]=sum;  
102         count++;  
103     }  
104     b.len =count++;  
105     return in;  
106   
107 }  
108 ostream& operator<<(ostream& out,  BigNum& b)   //重载输出运算符  
109 {  
110     int i;    
111     cout << b.a[b.len - 1];   
112     for(i = b.len - 2 ; i >= 0 ; i--)  
113     {   
114         cout.width(DLEN);   
115         cout.fill('0');   
116         cout << b.a[i];   
117     }   
118     return out;  
119 }  
120   
121 BigNum BigNum::operator+(const BigNum & T) const   //两个大数之间的相加运算  
122 {  
123     BigNum t(*this);  
124     int i,big;      //位数     
125     big = T.len > len ? T.len : len;   
126     for(i = 0 ; i < big ; i++)   
127     {   
128         t.a[i] +=T.a[i];   
129         if(t.a[i] > MAXN)   
130         {   
131             t.a[i + 1]++;   
132             t.a[i] -=MAXN+1;   
133         }   
134     }   
135     if(t.a[big] != 0)  
136         t.len = big + 1;   
137     else  
138         t.len = big;     
139     return t;  
140 }  
141 BigNum BigNum::operator-(const BigNum & T) const   //两个大数之间的相减运算   
142 {    
143     int i,j,big;  
144     bool flag;  
145     BigNum t1,t2;  
146     if(*this>T)  
147     {  
148         t1=*this;  
149         t2=T;  
150         flag=0;  
151     }  
152     else  
153     {  
154         t1=T;  
155         t2=*this;  
156         flag=1;  
157     }  
158     big=t1.len;  
159     for(i = 0 ; i < big ; i++)  
160     {  
161         if(t1.a[i] < t2.a[i])  
162         {   
163             j = i + 1;   
164             while(t1.a[j] == 0)  
165                 j++;   
166             t1.a[j--]--;   
167             while(j > i)  
168                 t1.a[j--] += MAXN;  
169             t1.a[i] += MAXN + 1 - t2.a[i];   
170         }   
171         else  
172             t1.a[i] -= t2.a[i];  
173     }  
174     t1.len = big;  
175     while(t1.a[len - 1] == 0 && t1.len > 1)  
176     {  
177         t1.len--;   
178         big--;  
179     }  
180     if(flag)  
181         t1.a[big-1]=0-t1.a[big-1];  
182     return t1;   
183 }   
184   
185 BigNum BigNum::operator*(const BigNum & T) const   //两个大数之间的相乘运算   
186 {   
187     BigNum ret;   
188     int i,j,up;   
189     int temp,temp1;     
190     for(i = 0 ; i < len ; i++)  
191     {   
192         up = 0;   
193         for(j = 0 ; j < T.len ; j++)  
194         {   
195             temp = a[i] * T.a[j] + ret.a[i + j] + up;   
196             if(temp > MAXN)  
197             {   
198                 temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);   
199                 up = temp / (MAXN + 1);   
200                 ret.a[i + j] = temp1;   
201             }   
202             else  
203             {   
204                 up = 0;   
205                 ret.a[i + j] = temp;   
206             }   
207         }   
208         if(up != 0)   
209             ret.a[i + j] = up;   
210     }   
211     ret.len = i + j;   
212     while(ret.a[ret.len - 1] == 0 && ret.len > 1)  
213         ret.len--;   
214     return ret;   
215 }   
216 BigNum BigNum::operator/(const int & b) const   //大数对一个整数进行相除运算  
217 {   
218     BigNum ret;   
219     int i,down = 0;     
220     for(i = len - 1 ; i >= 0 ; i--)  
221     {   
222         ret.a[i] = (a[i] + down * (MAXN + 1)) / b;   
223         down = a[i] + down * (MAXN + 1) - ret.a[i] * b;   
224     }   
225     ret.len = len;   
226     while(ret.a[ret.len - 1] == 0 && ret.len > 1)  
227         ret.len--;   
228     return ret;   
229 }  
230 int BigNum::operator %(const int & b) const    //大数对一个int类型的变量进行取模运算      
231 {  
232     int i,d=0;  
233     for (i = len-1; i>=0; i--)  
234     {  
235         d = ((d * (MAXN+1))% b + a[i])% b;    
236     }  
237     return d;  
238 }  
239   
240 bool BigNum::operator>(const BigNum & T) const   //大数和另一个大数的大小比较  
241 {   
242     int ln;   
243     if(len > T.len)  
244         return true;   
245     else if(len == T.len)  
246     {   
247         ln = len - 1;   
248         while(a[ln] == T.a[ln] && ln >= 0)  
249             ln--;   
250         if(ln >= 0 && a[ln] > T.a[ln])  
251             return true;   
252         else  
253             return false;   
254     }   
255     else  
256         return false;   
257 }  
258 bool BigNum::operator >(const int & t) const    //大数和一个int类型的变量的大小比较  
259 {  
260     BigNum b(t);  
261     return *this>b;  
262 }  
263   
264 void BigNum::print()    //输出大数  
265 {   
266     int i;     
267     cout << a[len - 1];   
268     for(i = len - 2 ; i >= 0 ; i--)  
269     {   
270         cout.width(DLEN);   
271         cout.fill('0');   
272         cout << a[i];   
273     }   
274     cout << endl;  
275 } 
276 BigNum dp[1005];
277 BigNum solve()
278 {
279     dp[0] = 1;
280     for(int i = 1; i <= k; i++)
281     {
282         for(int j = i; j <= n; j++)
283         {
284             dp[j] = dp[j] + dp[j - i];
285         }
286     }
287     return dp[n];
288 }
289 int main()
290 {
291     cin >> n >> k;
292     BigNum res = solve();
293     res.print();
294 }

 

相关文章: