题意:给出一个不超出1000位的数,问他可以由哪几个回文数构成(不超过50个)
分析:1.取这个数的前半部分减1得到一个新数
2.用这个新数构造一个回文数(分奇偶讨论)
3.原减去这个这个回文数
4.不断进行前3步,直到满足条件
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 7;
struct big{
int a[maxn];
int lenth;
big(){
lenth = 0;
memset(a, 0, sizeof(a));
}
big(int tmp){
lenth = 0;
while(tmp){
a[lenth++] = tmp%10;
tmp/=10;
}
}
big(char *s){
lenth = 0;
int len = (int)strlen(s);
for(int i=len-1;i>=0;i--){
a[lenth++] = s[i] - '0';
}
}
big operator - (const big& rhs){
big t;
big tt(*this);
int &len = t.lenth;
int i,j;
for(i=0,j=0;i<tt.lenth,j<rhs.lenth;i++,j++){
t.a[len] = tt.a[i] - rhs.a[i];
if(t.a[len]<0){
t.a[len]+=10;
tt.a[i+1] -= 1;
}
len++;
}
for(;i<tt.lenth;i++){
if(tt.a[i]<0){
tt.a[i]+=10;
tt.a[i+1]-=1;
}
t.a[len++] = tt.a[i];
}
while(t.a[len-1]==0)len--;
return t;
}
big trans(){
big t;
big tmp;
big ttt(*this);
int &len = tmp.lenth;
for(int i=lenth/2;i<lenth;i++){
tmp.a[len++] = ttt.a[i];
}
tmp = tmp - big(1);
big tt;
int &llen = tt.lenth;
if(lenth&1){
for(int i=tmp.lenth-1;i>=0;i--){
tt.a[llen++] = tmp.a[i];
}
for(int i=1;i<tmp.lenth;i++){
tt.a[llen++] = tmp.a[i];
}
}else{
for(int i=tmp.lenth-1;i>=0;i--){
tt.a[llen++] = tmp.a[i];
}
for(int i=0;i<tmp.lenth;i++){
tt.a[llen++] = tmp.a[i];
}
}
return tt;
}
void print(){
for(int i=0;i<lenth;i++)
printf("%d",a[i]);
printf("\n");
}
};
vector<big>v;
int main(){
int T,k=1;
scanf("%d",&T);
while(T--){
v.clear();
char s[maxn];
scanf("%s",s);
big t(s);
while(1){
if(t.lenth==1){
v.push_back(t);
break;
}
if(t.lenth==2&&t.a[1]==1){
if(t.a[0]==1)
v.push_back(big(11));
else if(t.a[0]==0){
v.push_back(big(9));
v.push_back(big(1));
}else{
big tmp = t - big(11);
v.push_back(tmp);
v.push_back(big(11));}
break;
}
big tt = t.trans();
v.push_back(tt);
t = t-tt;
}
printf("Case #%d:\n%d\n",k++,v.size());
for(int i=0;i<v.size();i++)
v[i].print();
}
}