codeforces 440C. One-Based Arithmetic 解题报告
题目链接:http://codeforces.com/problemset/problem/440/C
题目意思:给出一个数你,问需要用到的1来组成这个数的最少数量是多少。
我一开始对每个数只从 “+”的方向找,也就是假设对于4873,由千位开始配1,接着从百位,然后十位,最后个位。具体过程:4444 ---> 4888 ---> 4877 ---> 4873。对于test 3 的72我就悲剧了。因为最少数量应该是111 - 3*11 = 78, 78 - 5*1 = 72(15个1即可);而不是11*7 = 77, 77 - 5*11 = 72(19个1)(我的方法正是后者),得不到最少数量。
正确做法应该用搜索来做。唉~~~我对于dfs中递归总是很头痛,本来想利用人家的代码来调试(我只会用VC6调)清楚,怎么知道老是穿插一些汇编代码,调到int r1 = dfs(Abs(num-p1*ones[d]), d-1); 就看不清楚了= =......
这个代码我只能看懂一部分,希望有能之士看明白之后可以提点提点^_^...不过也好,知道自己有什么薄弱的地方赶快弥补弥补,这个递归总是要弄明白的,先放下这道题,苦练搜索题:dfs + bfs!!!
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; typedef long long LL;
const int maxn = + ;
LL ones[maxn], n; void Init()
{
ones[] = ;
for (int i = ; i < ; i++)
ones[i] = ones[i-] * + ;
} LL Abs(LL tmp)
{
return (tmp < ? -tmp : tmp);
} LL dfs(LL num, int d)
{
if (d == )
return num;
int p1 = num / ones[d];
int p2 = p1 + ;
int r1 = dfs(Abs(num-p1*ones[d]), d-);
int r2 = dfs(Abs(num-p2*ones[d]), d-);
return min(p1*(d+)+r1, p2*(d+)+r2);
} int main()
{
while (scanf("%lld", &n) != EOF)
{
Init();
cout << dfs(n, ) << endl;
}
return ;
}
调试版(VC6)(读者请忽略)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int maxn = + ;
__int64 ones[maxn], n; void Init()
{
ones[] = ;
for (int i = ; i < ; i++)
ones[i] = ones[i-] * + ;
//for (int i = 0; i < 16; i++)
// cout << "ones[" << i << "] = " << ones[i] << endl;
} __int64 Min(__int64 a, __int64 b)
{
return (a > b ? b : a);
} __int64 Abs(__int64 tmp)
{
return (tmp < ? -tmp : tmp);
} __int64 dfs(__int64 num, int d)
{
if (d == )
return num;
int p1 = num / ones[d];
int p2 = p1 + ;
int r1 = dfs(Abs(num-p1*ones[d]), d-);
int r2 = dfs(Abs(num-p2*ones[d]), d-);
return Min(p1*(d+)+r1, p2*(d+)+r2);
} int main()
{
while (scanf("%I64d", &n) != EOF)
{
Init();
printf("%I64d\n", dfs(n, ));
}
return ;
}
codeforces 440C. One-Based Arithmetic 解题报告的更多相关文章
- Codeforces Educational Round 92 赛后解题报告(A-G)
Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...
- codeforces 476C.Dreamoon and Sums 解题报告
题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...
- Codeforces Round #382 (Div. 2) 解题报告
CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...
- codeforces 507B. Amr and Pins 解题报告
题目链接:http://codeforces.com/problemset/problem/507/B 题目意思:给出圆的半径,以及圆心坐标和最终圆心要到达的坐标位置.问最少步数是多少.移动见下图.( ...
- codeforces 500B.New Year Permutation 解题报告
题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...
- codeforces B. Xenia and Ringroad 解题报告
题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...
- codeforces 462C Appleman and Toastman 解题报告
题目链接:http://codeforces.com/problemset/problem/461/A 题目意思:给出一群由 n 个数组成的集合你,依次循环执行两种操作: (1)每次Toastman得 ...
- codeforces 460A Vasya and Socks 解题报告
题目链接:http://codeforces.com/problemset/problem/460/A 题目意思:有一个人有 n 对袜子,每天早上会穿一对,然后当天的晚上就会扔掉,不过他会在 m 的倍 ...
- codeforces 567D.One-Dimensional Battle Ships 解题报告
题目链接:http://codeforces.com/problemset/problem/567/D 题目意思:给出 1 * n 的 field,编号从左至右依次为 1,2,...,n.问射 m 枪 ...
随机推荐
- MATLAB(1)
前言 之前经常用MATLAB,却不小心停留在了舒适区,连基本的调试方法都没有掌握.本文主要是对MATLAB程序调试中的一般方法进行总结,也是自己学习的记录.全文大致分为三个段落: 1)代码内调试: 2 ...
- [其他] 关于C语言中使用未声明函数的问题
在c语言中,碰到一个.c文件,无.h头文件,在另一.c文件调用函数时,并没有进行声明extern, 此时编译器不会报错,会默认去查找同名的函数,这样会存在一些问题,查了些资料,稍微总结了下: 总结: ...
- PatentTips - Write Combining Buffer for Sequentially Addressed Partial Line Operations
SUMMARY OF THE INVENTION The present invention pertains to a write combining buffer for use in a mic ...
- 标准C程序设计七---25
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- set_include_path() &&get_include_path()用法
function initialize(){ set_include_path(get_include_path().PATH_SEPARATOR . "core/"); ...
- Redis命令行之Hash
一.Redis之Hash简介 1. Hash是一个string类型的field和value的映射表,适合用于存储对象. 2. 每个hash可以存储232-1个键值对(40多亿). 二.Redis之Ha ...
- android开启线程,异步处理数据实例
package com.example.sywang2; import com.zds.os.R; import android.os.Bundle; import android.os.Handle ...
- Codeforces 961 D Pair Of Lines
题目描述 You are given nn points on Cartesian plane. Every point is a lattice point (i. e. both of its c ...
- Spring中使用byName实现Beans自动装配
以下内容引用自http://wiki.jikexueyuan.com/project/spring/beans-auto-wiring/spring-autowiring-byname.html: 此 ...
- decorate all function in all module
需求: 有package db_api,其下有很多 module 如 plane.py ship.py ufo.py.这些module内定义了方法如 plane.fly(), ship.float() ...