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


思路:一个数能够从比他大的数减,也能够从比它小的数的倍数加。

import java.util.*;
public class Main {
static long ones[]=new long[17];
static long dfs(long n,int i){
int k=(int) (n/ones[i]);
n%=ones[i];
long count=k*i;
if(n==0) return count;
else
return count+Math.min(i+dfs(ones[i]-n,i-1),dfs(n,i-1));
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
long n=scan.nextLong();
ones[0]=0;
for(int i=1;i<=16;i++){
ones[i]=ones[i-1]*10+1;
}
System.out.println(dfs(n,16));
} }



One-Based Arithmetic的更多相关文章

  1. {ICIP2014}{收录论文列表}

    This article come from HEREARS-L1: Learning Tuesday 10:30–12:30; Oral Session; Room: Leonard de Vinc ...

  2. Algebraic Kernel ( Arithmetic and Algebra) CGAL 4.13 -User Manual

    1 Introduction Real solving of polynomials is a fundamental problem with a wide application range. T ...

  3. Modular Arithmetic ( Arithmetic and Algebra) CGAL 4.13 -User Manual

    1 Introduction Modular arithmetic is a fundamental tool in modern algebra systems. In conjunction wi ...

  4. Algebraic Foundations ( Arithmetic and Algebra) CGAL 4.13 -User Manual

    理解: 本节主要介绍CGAL的代数结构和概念之间的互操作.与传统数论不同,CGAL的代数结构关注于实数轴的“可嵌入”特征.它没有将所有传统数的集合映射到自己的代数结构概念中,避免使用“数的类型”这一术 ...

  5. Optimizing subroutine calls based on architecture level of called subroutine

    A technique is provided for generating stubs. A processing circuit receives a call to a called funct ...

  6. General Decimal Arithmetic 浮点算法

    General Decimal Arithmetic http://speleotrove.com/decimal/ General Decimal Arithmetic [ FAQ | Decima ...

  7. Adaptively handling remote atomic execution based upon contention prediction

    In one embodiment, a method includes receiving an instruction for decoding in a processor core and d ...

  8. PatentTips - Scheduling compute kernel workgroups to heterogeneous processors based on historical processor execution times and utilizations

    BACKGROUND OF THE INVENTION  1. Field of the Invention  The present invention relates generally to h ...

  9. Graphics-Processing Architecture Based on Approximate Rendering

    BACKGROUND The present invention generally relates to the processing of graphics data, and particula ...

  10. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

随机推荐

  1. eclipse中导入jsp等工程使用过程中常遇问题

    1.导入的工程JSP文件出现报错的情况 这个一般不怎么影响文件的执行,这些文件飘红主要是因为eclipse的校验问题. 具体错误信息:Multiple annotations found at thi ...

  2. 《天书夜读:从汇编语言到windows内核编程》十一 用C++编写内核程序

    ---恢复内容开始--- 1) C++的"高级"特性,是它的优点也是它的缺点,微软对于使用C++写内核程序即不推崇也不排斥,使用C++写驱动需注意: a)New等操作符不能直接使用 ...

  3. 重构手法之Extrct Method(提炼函数)

    返回总目录 本节包含3个手法: 1.Extract Method(提炼函数) 2.Inline Method(内联函数) 3.Inline Temp(内联临时变量) Extract Method(提炼 ...

  4. C语言的第一个程序 “hello world!”

    1,C语言的简介        C语言是一门通用计算机编程语言,应用广泛.C语言的设计目标是提供一种能以简易的方式编译.处理低级存储器.产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言. ...

  5. Eclipse中启动tomcat从console跳回servers

  6. 【Java进阶】---map集合排序

    map集合排序         这篇文章讲的不仅仅是map排序,比如把对象按某一属性排序,它都可以解决这些问题.   比如,有N个对象,每个对象有个属性就是成绩,成绩分:优秀,良好,合格.那我们如何按 ...

  7. HCTF

    题目:魂斗罗 介绍:这个是HCTF里面的杂项,很好玩的, 1,这个看链接可以下载(http://139.224.54.27/gogogo/hundouluo.nes),然后在网上下载一个虚拟器(htt ...

  8. MyBatis《1》

     MyBatis入参考文档:http://mybatis.org/mybatis-3/zh/  1.使用MyBatis前的准备 1.增加Maven依赖 <dependency> <g ...

  9. Tomcat在修改代码后不会自动reload解决办法

    1.找到使用的Tomcat安装目录conf配置文件夹下context.xml文件 2.找到<Context>节点,改为<Context reloadable="true&q ...

  10. 软件测试管理QC

    一.QC简介 1)是HP公司的产品,是B/S结构的产品 2)在QC服务器中,打开IE浏览器,在地址栏中输入QC服务器的网址或者IP地址. 查看虚拟机的IP地址: 本地连接-属性-TCP/IP协议(重点 ...