您的位置:首页 > 其它

51nod 1425 减减数

2016-12-13 09:05 330 查看
1425 减减数


题目来源: CodeForces

基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题


 收藏


 关注

初始给定一个整数n。每次可以对其做一个操作,这个操作是将n减去他其中的某一位。得到新的一个数字n’,然后继续操作,直到他变成0为止。

比如24这个例子,24 → 20 → 18 → 10 → 9 → 0

Input
单组测试数据。
第一行有一个整数n(0 ≤ n ≤ 10^12)


Output
输出一个整数表示使得n变成0最少的操作步数。.


Input示例
24


Output示例
5


自己代码还没有理清= = 

晕乎着过了- -  大致就是二维map 记忆化搜索

#include <iostream>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <map>
using namespace std;
map<long long ,long long >d[100];
map<long long ,long long >p[100];
long long part(long long );
void dfs(long long n,long long s)
{

if(d[s]
) return ;
if(n<=0) return ;
if(n%10==n)
{
d[s]
=1;
return ;
}
long long x=part(n);
long long y=part(n-x);
if(x==n) while(x>9) x/=10;
while(y>9) y/=10;
if(y<s) y=s;
dfs(x,y);
if(x<s) x=s;
long long z=0;
long long w;
if(p[y][x]<y&&p[y][x])
{
//cout<<z<<' '<<n<<endl;
z=p[y][x];
w=n-x+z-y;
dfs(n-x+z-y,s);
d[s]
+=d[y][x]+d[s][n-x+z-y];
p[y]
=p[y][n-x+z-y];
}
else
{
if((n-x)%10==n-x)
{
if(n-x>0) d[s][n-x]=1;
else d[s][n-x]=0;
if(n-x>0)p[s][n-x]=n-x;
//cout<<s<<' '<<n-x<<endl;
}
else
{
dfs(n-x,s);
}
w=n-x;
d[s]
+=d[y][x]+d[s][n-x];
p[s]
=p[s][n-x];
}
p[s]
=p[s][w];
//cout<<s<<' '<<n<<' '<<n-x<<' '<<w<<' '<<' '<<x<<' '<<d[s]
<<' '<<p[s]
<<' '<<y<<' '<<x<<' '<<p[y][x]<<endl;

}

long long part(long long n)
{
long long s=0;
long long m=0,cc=0,ms=1;
while(n>0)
{
s=s+(n%10)*ms;
if(n%10>m)
{
m=n%10;
cc=s;
}
n=n/10;
ms*=10;
}
return cc;
}

int main()
{
long long n;
while(cin>>n)
{
dfs(n,0);
cout<<d[0]
<<endl;
long long i=0;
while(i<100)
{
d[i].clear();
i++;
}i=0;
while(i<100)
{
p[i].clear();
i++;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: