URAL1002——DP——K-based Numbers. Version 2(未AC)
大意:增大了数据,范围,就是求1到n位数用k进制形式的排除两个零在一起的一共多少情况
这题目有毒orz。。。。。超内存怎么搞。。。不过学习了bign(重载),妈妈再也不用担心我不会高精度了~~~
MLE代码。。
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> using namespace std; const int maxn = 2000; struct bign { int len ,s[maxn]; bign(){ memset(s,0,sizeof(s)); len = 1; } bign operator = (const char *num){ len = strlen(num); for(int i = 0; i < len ;i++) s[i] = num[len-i-1] - '0'; return *this; } bign operator = (int num){ char s[maxn]; sprintf(s,"%d", num); *this = s; return *this; } bign(int num) { *this = num;} bign(const char *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 bign &b)const{ bign 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; } }; 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 dp[1900][11]; int n,k; int main() { scanf("%d%d",&n,&k); for(int i = 1; i < k ;i++) dp[1][i] = 1; for(int i = 2; i <= n ;i++){ for(int j = 0 ; j < k ; j++){ for(int l = 0 ; l < k;l++){ if(!j && !l) continue; dp[i][l] = dp[i][l] + dp[i-1][j]; } } } bign ans = 0; for(int i = 0 ; i < k ;i++) ans = ans + dp[n][i]; cout<<ans<<endl; return 0; }