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. netty常用使用方式

    最近在重新看netty,在这里总结一些netty的一些常用的使用方式,类似于模板,方便速查. 以netty 4.1.x的API作记录,不同版本可能API有略微差异,需要注意netty5被废弃掉了(辨别 ...

  2. Python之Queue模块

    Queue 1.创建一个“队列”对象 >>> import Queue >>> queue = Queue.Queue(maxsize=100) >>& ...

  3. MySQL 2006 超时

    http://ronaldbradford.com/blog/sqlstatehy000-general-error-2006-mysql-server-has-gone-away-2013-01-0 ...

  4. java.lang.Collections

    java.lang.Collections 此类完全由在collection上进行操作或返回 collection 的静态方法组成.也就是说Collections提供了对Collection集合操作的 ...

  5. 【Java】单词倒序输出

    如何将一段单词倒序输出?把"Hello Java Hello China"变成"China Hello Java Hello"? 看起来好像很简单,只需要把字符 ...

  6. sockt套接字编程

    一.Socket简介 Socket是进程通讯的一种方式,即调用这个网络库的一些API函数实现分布在不同主机的相关进程之间的数据交换. 几个定义: (1)IP地址:即依照TCP/IP协议分配给本地主机的 ...

  7. C# winform 程序开发知识点总结(干货)

    1.数据库连接及操作 在说数据库操作之前,先说一下数据库连接操作字符串的获取 首先,点击服务器资源管理器,接下来选中数据连接右键点击添加连接,填入你要连接的服务器名称,点击单选框使用SQL Serve ...

  8. Python之三目运算符

    Python语言不像Java.JavaScript等这些语言有类似: 判段的条件?条件为真时的结果:条件为假时的结果 这样的三目运算,但是Python也有自己的三目运算符: 条件为真时的结果 if 判 ...

  9. P1156 垃圾陷阱

    题目描述 卡门――农夫约翰极其珍视的一条Holsteins奶牛――已经落了到“垃圾井”中.“垃圾井”是农夫们扔垃圾的地方,它的深度为D(2<=D<=100)英尺. 卡门想把垃圾堆起来,等到 ...

  10. php命令执行脚本

    php -f jiaoben.php &  读入并解释指明的文件.