题目链接:

Ugly Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 363    Accepted Submission(s): 134
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
 
 
题意:
 
给一个大数,然后让你找到不超过50个回文数的和为这个数;
 
思路:
 
每次找前边取一半减一,然后再复制另一半,这样很快就好了,手写了了一个大数减法,写成了智障了都;
 
AC代码:
 
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL;
typedef unsigned long long ULL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e4+120;
const int maxn=1e3+220;
const double eps=1e-12; int n,k,cnt;
char s[maxn];
int a[maxn]; struct Big
{
int a[maxn],leng;
}ans[maxn],temp;
Big fun(Big A,Big B)
{
Big C;
mst(C.a,0);
for(int i=A.leng;i<maxn;i++)A.a[i]=0;
for(int i=B.leng;i<maxn;i++)B.a[i]=0;
int hi=max(A.leng,B.leng),lc=0;
for(int i=0;i<hi;i++)
{
if(A.a[i]<B.a[i])
{
A.a[i+1]--;
A.a[i]+=10;
C.a[i]=A.a[i]-B.a[i];
}
else C.a[i]=A.a[i]-B.a[i];
}
for(int i=0;i<=hi;i++)
{
if(C.a[i]<0)
{
C.a[i+1]--;
C.a[i]+=10;
}
}
int flag=1;
for(int i=hi+1;i>=0;i--)
{
if(C.a[i]>0){lc=i;flag=1;break;}
}
if(flag)C.leng=lc+1;
else C.leng=0;
return C;
} Big newBig(Big A)
{
int lenth=A.leng;
Big B,C,D,E;
mst(B.a,0);B.leng=0;
mst(C.a,0);C.leng=0;
mst(D.a,0);D.leng=0;
mst(E.a,0);E.leng=0;
B.leng=lenth;
if(lenth%2==1)
{
int mid=lenth/2;
for(int i=lenth-1;i>=mid;i--)B.a[i]=A.a[i];
for(int i=mid-1;i>=0;i--)B.a[i]=D.a[i]=0;
D.a[mid]=1;
D.leng=mid+1;
E=fun(B,D);
if(E.leng==A.leng-1)
{
C.leng=E.leng;
for(int i=0;i<C.leng;i++)C.a[i]=9;
}
else
{
C.leng=A.leng;
for(int i=C.leng-1;i>=mid;i--)C.a[i]=E.a[i];
for(int i=mid-1;i>=0;i--)C.a[i]=E.a[2*mid-i];
}
}
else
{
int mid=lenth/2;
for(int i=lenth-1;i>=mid;i--)B.a[i]=A.a[i];
for(int i=mid-1;i>=0;i--)B.a[i]=D.a[i]=0;
D.a[mid]=1;D.leng=mid+1;
E=fun(B,D);
if(E.leng!=lenth)
{
C.leng=E.leng;
for(int i=C.leng-1;i>=0;i--)C.a[i]=9;
}
else
{
C.leng=lenth;
for(int i=C.leng-1;i>=mid;i--)C.a[i]=E.a[i];
for(int i=mid-1;i>=0;i--)C.a[i]=E.a[2*mid-i-1];
}
}
return C;
}
void solve()
{
int lenth=temp.leng;
Big A;
while(lenth)
{
if(lenth==2&&temp.a[1]==1)
{
A.leng=1;
A.a[0]=9;
}
else if(lenth==1)
{
if(temp.a[0]==0)break;
A.leng=1;
A.a[0]=temp.a[0];
ans[++cnt]=A;
break;
}
else A=newBig(temp);
ans[++cnt]=A;
temp=fun(temp,A);
lenth=temp.leng;
}
}
int main()
{
int t,Case=0;
read(t);
while(t--)
{
printf("Case #%d:\n",++Case);
scanf("%s",s);
int len=strlen(s),num=0;
cnt=0;
for(int i=len-1;i>=0;i--)temp.a[num++]=s[i]-'0';
temp.leng=len;
solve();
printf("%d\n",cnt);
for(int i=1;i<=cnt;i++)
{
for(int j=ans[i].leng-1;j>=0;j--)printf("%d",ans[i].a[j]);
printf("\n");
}
}
return 0;
}

  

hdu-5920 Ugly Problem(贪心+高精度)的更多相关文章

  1. HDU 5920 Ugly Problem 【模拟】 (2016中国大学生程序设计竞赛(长春))

    Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  2. HDU 5920 Ugly Problem 高精度减法大模拟 ---2016CCPC长春区域现场赛

    题目链接 题意:给定一个很大的数,把他们分为数个回文数的和,分的个数不超过50个,输出个数并输出每个数,special judge. 题解:现场赛的时候很快想出来了思路,把这个数从中间分为两部分,当位 ...

  3. HDU - 5920 Ugly Problem 求解第一个小于n的回文数

    http://acm.hdu.edu.cn/showproblem.php?pid=5920 http://www.cnblogs.com/xudong-bupt/p/4015226.html 把前半 ...

  4. HDU 5920 Ugly Problem

    说起这道题, 真是一把辛酸泪. 题意 将一个正整数 \(n(\le 10^{1000})\) 分解成不超过50个回文数的和. 做法 构造. 队友UHC提出的一种构造方法, 写起来比较方便一些, 而且比 ...

  5. D - Ugly Problem HDU - 5920

    D - Ugly Problem HDU - 5920 Everyone hates ugly problems. You are given a positive integer. You must ...

  6. HDU 4442 Physical Examination(贪心)

    HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...

  7. HDU 3549 Flow Problem(最大流)

    HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...

  8. hdu 5106 Bits Problem(数位dp)

    题目链接:hdu 5106 Bits Problem 题目大意:给定n和r,要求算出[0,r)之间全部n-onebit数的和. 解题思路:数位dp,一个ct表示个数,dp表示和,然后就剩下普通的数位d ...

  9. Ugly Problem

    Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Spec ...

随机推荐

  1. maven-dependency-plugin插件的使用

    maven-dependency-plugin插件的使用   maven-dependency-plugin是 处理与依赖相关的插件.它有很多可用的goal,大部分是和依赖构建.分析和解决相关的goa ...

  2. JVM中显示锁基础AbstractQueuedSynchronizer

    在研究AbstractQueuedSynchronizer的时候我是以ReentrantLock入手的.所以理所当然会设计到一些ReentrantLock的方法.因为网上已经有很多关于AQS的文章了, ...

  3. 【GPU编解码】GPU硬解码---DXVA

    前面介绍利用NVIDIA公司提供的CUVID库进行视频硬解码,下面将介绍利用DXVA进行硬解码. 一.DXVA介绍 DXVA是微软公司专门定制的视频加速规范,是一种接口规范.DXVA规范制定硬件加速解 ...

  4. [WP8] ListBox的Item宽度自动填满

    [WP8] ListBox的Item宽度自动填满 范例下载 范例程序代码:点此下载 问题情景 开发WP8应用程序的时候,常常会需要使用ListBox作为容器来呈现各种数据集合.但是在ListBox呈现 ...

  5. jQuery Mobile笔记

    1.获取jQuery mobile 文件,访问jQuerymobile网站下载 (貌似使用jquery mobile后,jquery会自动在网页中添加一些class类,第一次知道的我是被吓呆的!!) ...

  6. 原型 prototype

    原型 prototype js 的对象比较 由于 js 是解释执行的语言, 那么再代码中出现函数与对象如果重复执行, 会创建多个副本 在代码中重复执行的代码容易出现重复的对象 创建一个 Person ...

  7. javascript的错误处理

    1 onerror事件,实例代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind= ...

  8. 简单几步让Chrome浏览器也能打开Oracle EBS

    2016-12-14更新: Google Chrome浏览器从版本45开始正式禁用NPAPI插件(也就是原本JRE插件的实现架构).所以如果你的浏览器版本已经是45以上了,本文提供的方法将不再适用.以 ...

  9. Android项目实战(十一):moveTaskToBack(boolean ) 方法的使用

    当你开发的程序被按后退键退出的时候, 你肯定不想让他就这么被finish()吧,那么就想把程序退置到后台就可. (类似于PC端,你关闭一个浏览器和你最小化一个浏览器的区别) 参看方法:public b ...

  10. CAS实现单点登入(sso)经典教程

    本教程我已按照步骤实现,不过要深入了解单点登入还需要进一步的学习,掌握其中的精髓. 一.简介 1.cas是有耶鲁大学研发的单点登录服务器 2.本教材所用环境 Tomcat7.2 JDK6 CAS Se ...