Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 190 Accepted Submission(s): 74
Special Judge
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.
For each test case, there is only one line describing the given integer s (1≤s≤101000).
import java.math.BigInteger; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int tcase = sc.nextInt(); int t = 1; while(tcase-->0){ String str = sc.next(); String [] ans = new String[1000]; System.out.println("Case #"+(t++)+":"); int n = 1; if(ispalindromic(str)) { System.out.println(1); System.out.println(str); continue; } while(true){ String str1=str; if(str.length()==1||ispalindromic(str)){ ans[n++] = str; break; } if(str.length()%2==0){ int len = str.length()/2; if(len==1&&str.charAt(0)=='1'){ if(str.compareTo("11")>=0) str1 = "11"; else str1 = "9"; }else{ String temp =""; for(int i=0;i<len;i++){ temp+=str.charAt(i); } temp = new BigInteger(temp).subtract(BigInteger.ONE).toString(); len = temp.length(); for(int i=len-1;i>=0;i--){ temp+= temp.charAt(i); } str1 = temp; } }else{ int len = str.length()/2; if(len==1&&str.charAt(0)=='1'){ BigInteger b = new BigInteger(str); for(int i=b.intValue();i>=1;i--){ b = b.subtract(BigInteger.ONE); if(ispalindromic(b.toString())){ str1 = b.toString(); break; } } }else{ String temp =""; for(int i=0;i<len;i++){ temp+=str.charAt(i); } temp = new BigInteger(temp).subtract(BigInteger.ONE).toString(); len = temp.length(); temp+=str.charAt(len); for(int i=len-1;i>=0;i--){ temp+= temp.charAt(i); } str1 = temp; } } ans[n++] = str1; BigInteger big1 = new BigInteger(str); BigInteger big2 = new BigInteger(str1); BigInteger big = big1.subtract(big2); str = big.toString(); } System.out.println(n-1); for(int i=1;i<n;i++){ System.out.println(ans[i]); } } } static boolean ispalindromic(String s){ int len = s.length(); for(int i=0,j=len-1;i<=j;i++,j--){ if(s.charAt(i)!=s.charAt(j)) return false; } return true; } }