CF440C
|
C. One-Based Arithmetic
time limit per test
0.5 seconds memory limit per test
256 megabytes input
standard input output
standard output Prof. Vasechkin wants to represent positive integer n as a sum of addends, where each addends is an integer number containing only 1s. For example, he can represent 121 as 121=111+11+–1. Help him to find the least number of digits 1 in such sum. Input
The first line of the input contains integer n (1 ≤ n < 1015). Output
Print expected minimal number of digits 1. Sample test(s)
Input
121 Output
6 |
http://codeforces.com/contest/440/problem/C
用一堆由1组成的数来加减得到某数,输入某数,求最少要用多少个1。
深搜!由于这题的性质,每次搜肯定能得到一个解,然后之后搜的时候用的1数大于这个解,就可以跳出,怒剪了一大波枝。(最优性剪枝)
就是从最高位开始,慢慢把位削成0。因为把第i位消除成0后,可以选择数保证这一位不会再变回1,所以就一位一位消下去就行。
深搜只有两种方向,例如第i位是x,第一种方向就是搞x个11111把它消了,另一种是搞一堆11111把它加上去,然后搞一个多一位的111111把它再消掉,这两种都有可能,而其他的方法都不如这2种。
所以哐哐哐就是搜啦!搜啊!
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std; typedef long long ll; int ans,len;
ll one[]={,,,,,,,,,,
1111111111LL,11111111111LL,111111111111LL,1111111111111LL,
11111111111111LL,111111111111111LL,1111111111111111LL};
void dfs(ll x,int sum)
{
int a,b;
if(sum>=ans) return;
if(x==)
{
ans=sum;
return;
}
if(x<) x=-x;
ll y=x;
int t=;
while(y!=) t++,y/=;
ll reta=x,retb=one[t+]-x;
ll h=pow(,t-);
int sa=,sb=t+;
while(reta>=h) reta-=one[t],sa+=t;
while(retb>=h) retb-=one[t],sb+=t;
dfs(reta,sum+sa);
dfs(retb,sum+sb);
return;
} int main()
{
ll x;
while(scanf("%I64d",&x)!=EOF)
{
ans=1e9;
dfs(x,);
printf("%d\n",ans);
}
return ;
}
CF440C的更多相关文章
随机推荐
- [C++] 如何查看DLL有哪些函数
Visual Studio里面自带了一个工具 dumpbin. 打开VS的command line,输入dumpbin可以查看帮助. 我们查看导出函数的话,使用选项/EXPORTS. 如果函数太多,可 ...
- 掌握GCD以及后台永久运行的代码 (使用GCD处理后台线程和UI线程的交互)
一个例子: 在iPhone上做一个下载网页的功能,就是:在iPhone上放一个按钮,单击按钮时,显示一个转动的圆圈,表示正在进行下载,下载完成后,将内容加载到界面上的一个文本控件上. 使用GCD前: ...
- 20145302张薇 GDB调试汇编堆栈过程分析
GDB堆栈跟踪与汇编调试 堆栈跟踪 源代码 使用gcc - g example.c -o example -m32指令在64位的机器上产生32位汇编,然后使用gdb example指令进入gdb调试器 ...
- 错误C2665: “AfxMessageBox”: 2 个重载中没有一个可以转换所有参数类型
第一种方法: AfxMessageBox( "Simple message box. ");如果先定义一个CString 变量,再赋值就没问题CString sTe ...
- Java链式编程接口
在android开发中显示一个AlertDialog时,常采用下列的写法: new AlertDialog.Builder(getApplicationContext()) .setTitle(&qu ...
- [BZOJ1211][HNOI2004]树的计数(Prufer序列)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1211 分析: 关于无根树的组合数学问题肯定想到Prufer序列,类似bzoj1005那 ...
- 小记:Quartz StartNow() 无效
今天遇到一个问题,调度器在启动时无法立刻开始执行任务,代码如下: var trigger = TriggerBuilder.Create() .StartNow() //此处无效 .WithCronS ...
- tomcat虚拟路径的几种配置方法
一般我们都是直接引用webapps下面的web项目,如果我们要部署一个在其它地方的WEB项目,这就要在TOMCAT中设置虚拟路径了,Tomcat的加载web顺序是先加载 $Tomcat_home$\c ...
- 让less编译通过css滤镜
写IE6 hack的时候,发现在less中直接写css滤镜是会报错的,不能编译通过. 解决方法为:用~“”把相关的css代码包裹起来,例如: _top:~"expression(docume ...
- 压缩算法实现之LZ78
LZ78编码 LZ78算法,建立词典的算法. LZ78的编码思想: 不断地从字符流中提取新的缀-符串(String),通俗地理解为新"词条",然后用"代号"也就 ...