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. 常用的Python代码段

    过滤列表 #filter out empty strings in a sting list list = [x for x in list if x.strip()!=''] 一行一行地读文件 wi ...

  2. Velocity(4)——引入指令和#Parse 指令

    #Include和#Parse都是用于将本地文件引入当前文件的指令,而且被引入的文件必须位于TEMPLATE_ROOT.这两者之间有一些区别. #Include 被#Include引入的文件,其内容不 ...

  3. Python - SIP参考指南 - 介绍

    介绍 本文是SIP4.18的参考指南.SIP是一种Python工具,用于自动生成Python与C.C++库的绑定.SIP最初是在1998年用PyQt开发的,用于Python与Qt GUI toolki ...

  4. java环境变量和tomcat环境变量配置

    一.JAVA环境变量的配置1.首先下载JDK JDK可以在Oracle(甲骨文)公司的官方网站http://www.oracle.com下载2.安装完成后查看JDK安装路径一般是C:\Program ...

  5. JAVA学习摘要

    JAVA关键字 JAVA数据类型 数据类型的使用实例 JAVA注释的使用 使用文档注释时还可以使用 javadoc 标记,生成更详细的文档信息: @author 标明开发该类模块的作者 @versio ...

  6. [转]Oracle执行计划详解

    Oracle执行计划详解 --- 作者:TTT BLOG 本文地址:http://blog.chinaunix.net/u3/107265/showart_2192657.html --- 简介:   ...

  7. 最基本的mysql

    SQL 连接数据库 mysql -u root -p 备份数据库 mysql -u root -p 要备份数据库名>文件位置和文件名 恢复数据库 方式1 mysql -u root -p DBN ...

  8. 秒表计时器以及Stopwatch

    Stopwatch:秒表计时器,用来记录程序的运行时间,通常用来测试代码在时间上的执行效率.(需要引用:System.Diagnostics.) Stopwatch sw=new Stopwatch( ...

  9. css实现六边形图片(最简单易懂方法实现高逼格图片展示)

    不说别的,先上效果: 用简单的div配合伪元素,即可‘画出’这幅六边形图片,原理是三个相同宽高的div,通过定位旋转拼合成一个六边形,再利用背景图层叠,形成视觉上的一张整图.下面咱们一步一步来实现. ...

  10. JavaScript学习笔记(十四)——对象

    在学习廖雪峰前辈的JavaScript教程中,遇到了一些需要注意的点,因此作为学习笔记列出来,提醒自己注意! 如果大家有需要,欢迎访问前辈的博客https://www.liaoxuefeng.com/ ...