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的更多相关文章
随机推荐
- Core Data 概述
Core Data是一个模型层的技术.Core Data帮助你建立代表程序状态的模型层.Core Data也是一种持久化技术,它能将模型对象的状态持久化到磁盘,但它最重要的特点是:Core Data不 ...
- js监听键盘回车
//监听回车 $(document).keydown(function(e) { ) { $("#btnLogin").click(); } }) //input绑定回车 $('# ...
- NoSQL数据库之国产开源产品:SequoiaDB 分析前言
随着互联网技术的发展,面对海量数据的存储和分析,传统关系型数据库已经无法满足,由此衍生出一种与关系型数据库区别开的数据库NoSQL(Not Only SQL). 国外做的比较成熟的NoSQL有Mong ...
- [AaronYang]C#人爱学不学[2]
1. 记事本写C#,脱离vs 新建记事本,名字为 helloworld.cs using System; namespace Hello{ public class HelloWorldSay{ st ...
- Spring配置文件详解:<context:annotation-config/>和<context:component-scan base-package=""/>和<mvc:annotation-driven />
<context:annotation-config/> 在基于主机方式配置Spring时,Spring配置文件applicationContext.xml,你可能会见<contex ...
- [转]不再以讹传讹,GET和POST的真正区别
原文地址:http://www.nowamagic.net/librarys/veda/detail/1919 如果有人问你,GET和POST,有什么区别?你会如何回答? 我的经历 前几天有人问我这个 ...
- 重启猫(modem)的方法
重启猫(modem)的方法 家里上网还是古老的"猫+路由器"模式,换路由器后就要reset猫,其步骤为: 断开猫电源 用针头或笔尖按住reset小孔,持续30秒 针抵住小孔的同时连 ...
- CSS文字排版
一.font-size 我来试一试:为第一段中的“胆小如鼠”设置字号为:20px,字体颜色为:red. <!DOCTYPE HTML> <html> <head> ...
- codevs1358 棋盘游戏
题目描述 Description 这个游戏在一个有10*10个格子的棋盘上进行,初始时棋子位于左上角,终点为右下角,棋盘上每个格子内有一个0到9的数字,每次棋子可以往右方或下方的相邻格子移动,求一条经 ...
- Syntax error, annotations are only available if source level is 1.5
在项目上右键 -> Properties -> Java Compiler