Ugly Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 351 Accepted Submission(s): 132
Special Judge
Problem Description
Everyone hates ugly problems.
You are given a positive integer. You must represent that number by sum of palindromic numbers.
A palindromic number is a positive integer such that if you write out that integer as a string in decimal without leading zeros, the string is an palindrome. For example, 1 is a palindromic number and 10 is not.
Input
In the first line of input, there is an integer T denoting the number of test cases.
For each test case, there is only one line describing the given integer s (1≤s≤101000).
Output
For each test case, output “Case #x:” on the first line where x is the number of that test case starting from 1. Then output the number of palindromic numbers you used, n, on one line. n must be no more than 50. en output n lines, each containing one of your palindromic numbers. Their sum must be exactly s.
Sample Input
2
18
1000000000000
Sample Output
Case #1:
2
9
9
Case #2:
2
999999999999
1
Hint
9 + 9 = 18
999999999999 + 1 = 1000000000000
Source
2016中国大学生程序设计竞赛(长春)-重现赛
①:对整数S 将S分解为s和p
p是<=S的最大的回文数
s=S-p
然后再令S=s 递归下去求解
不难发现 s的长度|s|必定<=|S|/2
所以每求一次 s长度至少减半 p的数量不会超过 log2(|S|) < 50
②:构造最大回文
当S=540011
构造p=540045 基于中间对称
当p <= S p就是最大回文
当p > S p的中间的数-1 p=54 (-1) 045
然后在处理一下p的前半段 并让后半段与前半段对称
p=539935
至于s 大数减法可得
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>
//#define CHECK_TIME
string sub(const string&bigger,const string&smaller){//大数减法
string ans(bigger);
for(int i=1;i<=smaller.size();++i)
ans[ans.size()-i]-=smaller[smaller.size()-i]-'0';
for(int i=ans.size()-1;i>0;--i)
if(ans[i]<'0'){
ans[i]+=10;
--ans[i-1];
}
int st=0;//去掉前缀的0
while(ans[st]=='0')
++st;
return st==ans.size()?"0":ans.assign(ans,st,ans.size());
}
string find_max_p(const string&str){//找到<=str的最大的回文串p
int sum=0;
for(int i=0;i<str.size();++i)
sum+=str[i]-'0';
if(sum==1&&str!="1")//属于100000这种情况 直接返回size-1个9即可
return string().assign(str.size()-1,'9');
//让ans前后对称
string ans(str);
int i=0,j=ans.size()-1;
while(i<j){
ans[j]=ans[i];
++i,--j;
}
if(ans<=str)
return ans;
//中间数-1
ans[ans.size()-1-j]=ans[j]-=1;
for(int i=j;i>0;--i)//处理掉可能存在的负数
if(ans[i]>='0'){
ans[ans.size()-1-i]=ans[i];
break;
}
else{
--ans[i-1];
ans[ans.size()-1-i]=ans[i]+=10;
}
ans.back()=ans[0];//头和尾 忘了这个一直WA...
return ans;
}
int main()
{
//freopen("/home/lu/文档/r.txt","r",stdin);
//freopen("/home/lu/文档/w.txt","w",stdout);
int T;
scanf("%d",&T);
string str;
vector<string>ans;
for(int t=1;t<=T;++t){
cin>>str;
ans.clear();
while(str!="0"){
ans.push_back(find_max_p(str));
str=sub(str,ans.back());
}
cout<<"Case #"<<t<<":"<<endl;
cout<<ans.size()<<endl;
for(int i=0;i<ans.size();++i)
cout<<ans[i]<<endl;
}
return 0;
}