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
Output
Input示例
Output示例
自己代码还没有理清= =
晕乎着过了- - 大致就是二维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++;
}
}
}
题目来源: 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++;
}
}
}
相关文章推荐
- Execution failed for task ':app:buildInfoDebugLoader'
- 纠结的链接——ln、ln -s、fs.symlink、require
- 200道物理学难题——040喷泉包络线
- 关于socket通讯的server类(C#)
- 使用js抓取今日头条的文章
- Java将科学计数法数据转为字符串的实例
- HDU 2021 发工资咯
- 161212 笔记--无线传感网络中的MAC协议
- ExtJS快速入门指南
- JavaScript 最佳实践:帮你提升代码质量
- android 打包 apk的时候发生"xxx" is not translated in "zh" 的解决方法
- 微信公众号开发day02
- 移动应用接口加密
- 从无到有构建一个Mini Linux(7)
- java之抽象类
- MongoDB学习笔记~数据模型属性为集合时应该为它初始化
- 体育Bank2016会议笔记
- SSH框架之Hibernate的引入、ORM概念、第一个Hibernate配置
- java中将科学计数法转换普通计数法的简单方法
- JavaScript谁动了你的代码