- 1010230 is a valid 7-digit number;
- 1000198 is not a valid number;
- 0001235 is not a 7-digit number, it is a 4-digit number.
You may assume that 2 ≤ K ≤ 10; N ≥ 2; N + K ≤ 1800.
这个题目和之写过的dp 一样 只不过数位比较大,必须用大数,然而本人并不会大数,于是乎找了一位学长ac的模板
蓝儿奇怪的是, 学长AC代码窝交的时候就变成的RE地址不够 ,这看脸的社会啊; 蓝儿把大小开到题目要求的极限的大小之后 MLE 了。 窝感到了这个世界深深地恶意。蓝儿,窝又在网上找到了一个只适用这个题目的大数模板的AC代码,果蓝RE数组不够大,这看颜的世界啊,他们是怎么AC的。窝感受到了世界深深地恶意。然而新的模板改了以后就可以ac了。。。。
学长的模板 看去来就好高大上啊。。。。。。蓝儿AC不了,不知道是不是哪里抄错了么
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 200;
struct big{
int s[N],len;
big(){
memset(s, 0, sizeof(s));
len = 1;
}
big (int num) { *this = num;}
big (const char *num) { *this = num;}
big operator = (const int num){
char s[N];
sprintf(s, "%d", num);
*this = s;
return *this;
}
big operator = (const char *num){
len = strlen(num);
while(len > 1&& num[0] == '0') num++,len--;
for(int i = 0; i < len; i++) s[i] = num[len-i-1]-'0';
return *this;
}
void deal()
{
while(len > 1 && !s[len-1]) len--;
}
big operator + (const big &a) const{
big ret;
ret.len = 0;
int top = max(len,a.len), add = 0;
for(int i = 0; add || i < top; i++)
{
int now = add;
if( i < len) now += s[i];
if( i < a.len) now += a.s[i];
ret.s[ret.len++] = now%10;
add = now/10;
}
return ret;
}
string str() const{
string ret = "";
for(int i = 0; i < len; i++) ret = char(s[i]+'0')+ret;
return ret;
}
};
istream & operator >> (istream& in,big &x){
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator << (ostream & out, const big &x) {
out << x.str();
return out;
}
big dp[220][12];
int n,k;
void input()
{
scanf("%d%d",&n,&k);
}
void slove()
{
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];
}
}
}
big ans = 0;
for(int i = 0; i < k; i++)
ans = ans + dp[n][i];
cout<<ans<<endl;
}
int main()
{
input();
slove();
return 0;
}
网上得到后修改 了的AC代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 1800;
struct big{
int s[N],len;
};
big dp[1800];
int n,k;
void trans(int x)
{
for(int i = 1; i <= dp[x].len; i++)
{
dp[x].s[i+1]+=dp[x].s[i]/10;
dp[x].s[i]%=10;
}
while(dp[x].s[dp[x].len+1] > 0)
{
dp[x].len++;
dp[x].s[dp[x].len+1] = dp[x].s[dp[x].len]/10;
dp[x].s[dp[x].len]%=10;
}
}
int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(dp,0,sizeof(dp));
dp[1].len = 1; dp[1].s[1] = k - 1; trans(1);
dp[2].len = 1; dp[2].s[1] = (k-1)*k; trans(2);
for(int i = 3; i <= n; i++)
{
dp[i].len = dp[i-1].len;
for(int j = 1; j <= dp[i-1].len; j++)
{
dp[i].s[j] = dp[i-1].s[j] + dp[i-2].s[j];
}
trans(i);
for(int j = 1;j <= dp[i].len; j++) dp[i].s[j]*=(k-1);
trans(i);
}
for(int i = dp[n].len; i >= 1; i--)
printf("%d",dp[n].s[i]);
printf("\n");
}
return 0;
}