说起这道题, 真是一把辛酸泪.

题意

将一个正整数 $n(\le 10^{1000})$ 分解成不超过50个回文数的和.

做法

构造.

队友UHC提出的一种构造方法, 写起来比较方便一些, 而且比较巧妙. 可惜我码力太弱, 现场没调出来.

大体的想法是:

将一个数"等"分成两半. 如果长度为奇数可以采取下面两种处理方法之一.

  1. 在开头补0.
  2. 后一半 (低位) 的长度向下取整.

我采取第2种处理方式.

设前一半的长度为$h_1$, 后一半的长度为 $h_2$.

然后将前一半长

(写不下去了..., 贴代码...)

Implementation

#include <bits/stdc++.h>
using namespace std; using num=vector<int>;
using iter=num::iterator;
using riter=num::reverse_iterator; bool Less(const num &a, const num &b){ // a and b are of same length
for(auto it1=a.rbegin(), it2=b.rbegin(); it1!=a.rend(); ++it1, ++it2)
if(*it1 != *it2){
return *it1 < *it2;
}
return false;
} void borrow(num &a){
for(int i=a.size()/2; i<a.size(); i++)
if(a[i]){
for(--a[i]; i; a[--i]+=9); //?
break;
}
while(a.size() && *a.rbegin()==0)
a.pop_back();
} void split(num &lo, num &hi, const num &a){
auto mid=a.begin()+a.size()/2;
lo={a.begin(), mid};
hi={mid, a.end()};
} num operator-(num a, num b){ // a and b are of same length && a >= b
num res;
for(int i=0; i<a.size(); i++){
if(a[i]<b[i]){
a[i]+=10;
a[i+1]-=1;
}
res.push_back(a[i]-b[i]);
} for(; res.size() && *res.rbegin()==0; )
res.pop_back(); if(res.size()){ //error-prone
for(int i=0; i<res.size()-1; i++){
res[i+1]+=res[i]/10;
res[i]%=10;
} for(; *res.rbegin()>9; ){
int t=*res.rbegin();
*res.rbegin()%=10;
res.push_back(t/10);
}
} for(auto x:res){
assert(x>=0 && x<10);
} return res;
} vector<num> res; void out(const num &a){
for(auto it=a.rbegin(); it!=a.rend(); ++it)
cout<<*it;
cout<<endl;
} void solve(num &a){
res.clear();
num lo, hi;
int ones=0; while(a.size()>1){
// out(a);
split(lo, hi, a);
num rhi(hi.rbegin(), hi.rbegin()+lo.size());
if(Less(lo, rhi)){
borrow(a);
++ones;
continue;
}
a=lo-rhi;
// There is no advantage to using {} initialization, and one trap,
// when using auto to get the type determined by the initializer.
// The trap is that if the initializer is a {}-list, we may not want its
// type deduced.
//initializer_list<num> xx={rhi};
//cout<<typeid(xx).name()<<endl;
auto tmp=rhi;
//cout<<typeid(tmp).name()<<endl;
for(auto &x: hi)
tmp.push_back(x);
res.push_back(tmp);
} if(a.size()) res.push_back(a); assert(res.size() + ones <= 50); printf("%d\n", res.size()+ones);
for(auto &x: res)
out(x);
while(ones--)
puts("1");
} char s[1<<10]; int main(){ // num x, y;
// in g++:
// direct-list-intialization of 'auto' requires exactly one element
// for deduction to 'std::initializer_list', use copy-list-intialization (i.e. add '=' before the '{')
// auto xx{x, y}; int T, cas=0;
num a;
for(cin>>T; T--; ){
scanf("%s", s);
a.clear();
int n=strlen(s);
for(int i=n-1; i>=0; --i)
a.push_back(s[i]-'0');
printf("Case #%d:\n", ++cas);
solve(a);
}
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 求解第一个小于n的回文数

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

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

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

  4. D - Ugly Problem HDU - 5920

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

  5. hdu-5920 Ugly Problem(贪心+高精度)

    题目链接: Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  6. HDU 3549 Flow Problem(最大流)

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

  7. hdu 5106 Bits Problem(数位dp)

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

  8. Ugly Problem

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

  9. HDU 3374 String Problem (KMP+最大最小表示)

    HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory ...

随机推荐

  1. 从idea上通过路径去导入项目

    这里我用git来演示导入. 首先确定你要导入项目的路径.(我这里用码云路径图片做演示) 1.选择 2.填写

  2. web性能优化——简介

    简介 性能优化的第一准则:加缓存.几乎绝大部分优化都围绕这个来进行的.让用户最快的看到结果. 性能优化的第二准则:最小原则.绝不提供多余的信息.比如,静态资源(图片.css.js)压缩,图片的滚动加载 ...

  3. 【技巧】“Plugin execution not covered by lifecycle configuration...“异常的处理

    问题现象: 在Eclipse(JEE mars)中新建maven project,选择archetype为:maven-archetype-plugin,结果生成的project存在错误:“Plugi ...

  4. SSH登录之后运行命令报错的解决办法-- Failed to connect to Mir: Failed to connect to server socket: No such file or directory

    问题描述: Failed to connect to Mir: Failed to connect to server socket: No such file or directory 解决方案: ...

  5. Ubuntu更改右键菜单

    方法/步骤1.这是我们在桌面文件夹ubuntugege上打开的右键菜单,你说你在~/.gnome2/nautilus-scripts/添加的右键菜单项目但它就是没有显示呀,于是你觉得Ubuntu 12 ...

  6. 在Word2013中多次应用格式刷

    顾名思义,格式刷是为了方便需要跨区域操作时候,能快速的应用格式到相应文本.那么怎么使用word进行格式刷的多次使用呢.我们先来看单次的,这个比较容易,只要在先需要的格式单击一次格式刷,再到需要的文本执 ...

  7. CSS Hack技术介绍及常用的Hack技巧

    一.什么是CSS Hack? 不同的浏览器对CSS的解析结果是不同的,因此会导致相同的CSS输出的页面效果不同,这就需要CSS Hack来解决浏览器局部的兼容性问题.而这个针对不同的浏览器写不同的CS ...

  8. Asp.Net Form验证不通过,重复登录

    问题产生根源: 当然,其实应该需要保持线上所有机器环境一致!可是,写了一个小程序.使用的是4.5,aysnc/await实在太好用了,真心不想把代码修改回去. so,动了念头,在这台服务器上装个4.5 ...

  9. [转]JSON 入门指南

    原文地址:http://www.ibm.com/developerworks/cn/web/wa-lo-json/ 尽管有许多宣传关于 XML 如何拥有跨平台,跨语言的优势,然而,除非应用于 Web ...

  10. E(X+Y), E(XY), D(X + Y)

    \(X, Y\)为两个随机变量, \(p_X(x), p_Y(y)\)分别为\(X, Y\)的概率密度/质量函数, \(p(x, y)\)为它们的联合概率密度. \(E(X + Y) = E(X) + ...