jump 转换进制+dp

from Contest1024 - 省选模拟题14

题目大意

MMM站在x=0的地方,她想跳到x=t的地方。MMM每次跳跃可以选择跳到x - k或者x + k的地方,其中k={base^n | base > 1 and n >= 0}。
问MMM最少需要多少次跳跃才能跳到x=t的地方。
t, base(1 < t < 2^63, 1 < base <= 1000)

分析

考虑转化成base进制后dp
f[i]表示从低位开始的前i位满足t的base进制最少跳多少下
因为有减号
f[i][0]表示无借位
f[i][1]表示向前面借了一位来做减法

solution

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
typedef long long LL;
using namespace std;
const int M=101;
const int INF=1e9;

LL t;
int n,bs;
int a[M];
int f[M][2];

int main(){
	scanf("%lld%d",&t,&bs);
	for(;t>0;t/=bs) a[++n]=t%bs;
	f[0][0]=0;
	f[0][1]=INF;
	for(int i=1;i<=n;i++){
		f[i][0]=min(f[i-1][0]+a[i],f[i-1][1]+a[i]+1);//f[i-1][1]+a[i]+1表示之前在这里借了一位,所以这里我要多一位借给后面
		f[i][1]=min(f[i-1][0]+bs-a[i],f[i-1][1]+bs-a[i]-1);//f[i-1][1]+bs-a[i]-1表示借位还没有结束,按照减法退位的原则,这一位可以少减一下
		
	}
	printf("%d
",min(f[n][0],f[n][1]+1));
	return 0;
}
原文地址:https://www.cnblogs.com/acha/p/6409189.html