One-Based Arithmetic
One-Based Arithmetic
0.5 seconds
256 megabytes
standard input
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.
The first line of the input contains integer n (1 ≤ n < 1015).
Print expected minimal number of digits 1.
121
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的更多相关文章
- {ICIP2014}{收录论文列表}
This article come from HEREARS-L1: Learning Tuesday 10:30–12:30; Oral Session; Room: Leonard de Vinc ...
- 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 ...
- 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 ...
- Algebraic Foundations ( Arithmetic and Algebra) CGAL 4.13 -User Manual
理解: 本节主要介绍CGAL的代数结构和概念之间的互操作.与传统数论不同,CGAL的代数结构关注于实数轴的“可嵌入”特征.它没有将所有传统数的集合映射到自己的代数结构概念中,避免使用“数的类型”这一术 ...
- 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 ...
- General Decimal Arithmetic 浮点算法
General Decimal Arithmetic http://speleotrove.com/decimal/ General Decimal Arithmetic [ FAQ | Decima ...
- 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 ...
- 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 ...
- Graphics-Processing Architecture Based on Approximate Rendering
BACKGROUND The present invention generally relates to the processing of graphics data, and particula ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
随机推荐
- C# 取Visio模型信息的简易方法
最近的一个项目,要求导出Visio图纸,因为是建筑类的,所以,需要设置墙壁,门,房间等信息的参数. 拿墙壁为例,选中墙壁模型,右键属性,会弹出以下对话框. 需要设置墙长.墙壁厚度等一些列信息. 现在C ...
- C#中结构体定义并转换字节数组
最近的项目在做socket通信报文解析的时候,用到了结构体与字节数组的转换:由于客户端采用C++开发,服务端采用C#开发,所以双方必须保证各自定义结构体成员类型和长度一致才能保证报文解析的正确性,这一 ...
- MUI开发记录——我的考勤
已经好久没有更新技术博客了,因为最近一直在做跨平台web app应用的开发,由于是刚做这个,也没太多经验同大家分享,可我是一个闲不住的人,我还是决定于百忙之中抽空整理一篇,记录下开发历程......— ...
- JAVA调用matlab代码
做实验一直用的matlab代码,需要嵌入到java项目中,matlab代码拼拼凑凑不是很了解,投机取巧采用java调用matlab的方式解决. 1. matlab版本:matlabR2014a ...
- Maven2的配置文件settings.xml
简介: 概览 当Maven运行过程中的各种配置,例如pom.xml,不想绑定到一个固定的project或者要分配给用户时,我们使用settings.xml中的settings元素来确定这些配置.这包含 ...
- java多线程创建-Thread,Runnable,callable和threadpool
java创建多线程的方式有许多种,这里简要做个梳理 1. 继承Thread类 继承java.lang.Thread类,创建本地多线程的类,重载run()方法,调用Thread的方法启动线程.示例代码如 ...
- MD5 SHA1 HMAC HMAC_SHA1区别
MD5.SHA1.HMAC.HMAC_SHA1区别 引言 什么是MD5,什么是SHA1,如何校验这些Hash.还有拿单个apk文件的MD5,SHA1讯问是不是原版的问题,在这里,让我们先来了解 ...
- HTML的用法
今天学习了HTMIL,标签.这个东西吧,没什么很难的,就是得多练多打.今天的一些个人心得: 标签:成对存在的名称 标签注意:1.标签名放在<> 2.标签成对存在的 3.结束标签有斜杠/ 例 ...
- 在动态链接库dll中弹出对话框
在动态链接库dll中弹出对话框步骤: 1.添加Dialog资源,然后在资源视图的对话框界面右击添加类,输入类名MyDlg,使得其继承与CDialogEx.(继承CDialog应该也可以)2.在新生成的 ...
- 关于Excel数据批量导入数据库的案例
写这个案例主要是感觉这个功能挺实用,很多地方会用得到的,废话就不多说了,直接上对应的源码. 这个案例我运用的是Winform窗体程序实现数据的导入. 首先是数据库的登陆界面如下: 源码如下: usin ...