求火柴的组成的数字最多能组成多少种数字,典型的递推问题
但是因为结果巨大,要用高精度运算
一开始手写高精度,不仅挫的要死,最后还WA了。
最后学了一下白书上面的bign类,相当方便啊。
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <algorithm> #define N 720 using namespace std; struct bign { int len,s[N]; bign() { memset(s,0,sizeof s); len=1; } bign(int num) {*this=num;} string str()const { string res=""; for(int i=0;i<len;i++) res=(char)(s[i]+'0')+res; if(res=="") res="0"; return res; } bign operator = (const char* num) { int i; len=strlen(num); for (i=0;i<len;i++) { s[i]=num[len-i-1]-'0'; } return *this; } bign operator =(int num) { char s[N]; sprintf(s,"%d",num); *this=s; return *this; } bign operator + (const bign& b )const { bign c; c.len=0; int i,g=0; for (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; } }; 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 f[2010]; bign ans[2010]; int c[]={6,2,5,5,4,5,6,3,7,6}; int n; int main() { for (int i=0;i<=2001;i++) f[i]=0; f[0]=1; for (int i=0;i<=2001;i++) { for (int j=0;j<=9;j++) { if (i+c[j]<=2000 && !(i==0 && j==0)) { f[i+c[j]]=f[i+c[j]]+f[i]; } } } ans[0]=0; for (int i=1;i<=2001;i++) { ans[i]=ans[i-1]+f[i]; } while (scanf("%d",&n)!=EOF) { if (n>=6) cout<<ans[n]+1<<endl;//这里一开始写成直接 ans[n]=ans[n]+1,WA了好多次都没发现啊啊啊,不能改变原有值啊啊。。太粗心了 else cout<<ans[n]<<endl; } return 0; }