DFS Codeforces440C One-Based Arithmetic

传送门:点击打开链接

题意:只能使用由1组成的数字,通过加法和减法得到n。问这些数字的1个个数之和最少是多少。

思路:非常经典的一道暴搜题,每一层有2个分支,层数递减,很容易证明复杂度最坏大约是O(2^16)

#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<algorithm>
#include<functional>
#define fuck(x) cout<<"["<<x<<"]"
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w+",stdout)
using namespace std;
typedef long long LL;
typedef pair<LL, int>PII;

const int MX = 1e6 + 5;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;

LL one[100];
int DFS(LL n, int d) {
    int ret = n / one[d] * d;
    n %= one[d];
    if(!n) return ret;
    return ret + min(DFS(one[d] - n, d - 1) + d, DFS(n, d - 1));
}
int main() {
    for(int i = 1; i <= 16; i++) {
        one[i] = one[i - 1] * 10 + 1;
    }
    LL n; scanf("%I64d", &n);
    printf("%d\n", DFS(n, 16));
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值