Balanced Number 数位dp
题意: 给出求ab之间有多少个平衡数 4139为平衡数 以3为轴 1*1+4*2==9*1
思路很好想但是一直wa :
注意要减去前导零的情况 0 00 000 0000 不能反复计算
#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define REP(i,N) for(int i=0;i<(N);i++)
#define CLR(A,v) memset(A,v,sizeof A)
//////////////////////////////////
#define inf 0x3f3f3f3f
#define N 3700+5
#define MID N/2
ll dp[][][N];
ll a[];
ll dfs(int pos,int t,int state,bool lead,bool limit )
{
if(!pos)return state==; if(!limit&&!lead&&dp[pos][t][state+MID]!=-)return dp[pos][t][state+MID];
ll ans=;
int up=limit?a[pos]:;
rep(i,,up)
{
ans+=dfs(pos-,t,state+i*(pos-t), lead&&i==,limit&&i==a[pos]);
}
if(!limit&&!lead)dp[pos][t][state+MID]=ans;
return ans;
} ll solve(ll x)
{
int pos=;
ll ans=;
while(x)
{
a[++pos]=x%;
x/=;
}
rep(i,,pos)
ans+=dfs(pos,i,,true,true);
return ans-pos+;//去除前导零
}
int main()
{
int cas;
CLR(dp,-);
RI(cas);
while(cas--)
{
ll a,b;
cin>>a>>b;
printf("%lld\n",solve(b)-solve(a-));
}
return ;
}
或者
#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define REP(i,N) for(int i=0;i<(N);i++)
#define CLR(A,v) memset(A,v,sizeof A)
//////////////////////////////////
#define inf 0x3f3f3f3f
#define N 3700+5
#define MID N/2
ll dp[][][N];
ll a[];
ll dfs(int pos,int t,int state,bool lead,bool limit )
{
if(!pos)return !lead&&state==; if(!limit&&!lead&&dp[pos][t][state+MID]!=-)return dp[pos][t][state+MID];
ll ans=;
int up=limit?a[pos]:;
rep(i,,up)
{
ans+=dfs(pos-,t,state+i*(pos-t), lead&&i==,limit&&i==a[pos]);
}
if(!limit&&!lead)dp[pos][t][state+MID]=ans;
return ans;
} ll solve(ll x)
{
if(x<)return ;
if(x==)return ;
int pos=;
ll ans=;
while(x)
{
a[++pos]=x%;
x/=;
}
rep(i,,pos)
ans+=dfs(pos,i,,true,true);
return ans+;//去除前导零
}
int main()
{
int cas;
CLR(dp,-);
RI(cas);
while(cas--)
{
ll a,b;
cin>>a>>b;
printf("%lld\n",solve(b)-solve(a-));
}
return ;
}
Balanced Number 数位dp的更多相关文章
- HDU 3709 Balanced Number (数位DP)
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- hdu3709 Balanced Number (数位dp+bfs)
Balanced Number Problem Description A balanced number is a non-negative integer that can be balanced ...
- HDU3709:Balanced Number(数位DP+记忆化DFS)
Problem Description A balanced number is a non-negative integer that can be balanced if a pivot is p ...
- HDU3709 Balanced Number —— 数位DP
题目链接:https://vjudge.net/problem/HDU-3709 Balanced Number Time Limit: 10000/5000 MS (Java/Others) ...
- hdu3709 Balanced Number 数位DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题目大意就是求给定区间内的平衡数的个数 要明白一点:对于一个给定的数,假设其位数为n,那么可以有 ...
- 多校5 HDU5787 K-wolf Number 数位DP
// 多校5 HDU5787 K-wolf Number 数位DP // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d // f 用作标记,当现在枚举的数小 ...
- Balanced Numbers (数位DP)
Balanced Numbers https://vjudge.net/contest/287810#problem/K Balanced numbers have been used by math ...
- SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]
题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...
- SPOJ10606 BALNUM - Balanced Numbers(数位DP+状压)
Balanced numbers have been used by mathematicians for centuries. A positive integer is considered a ...
随机推荐
- 洛谷P4859 已经没有什么好害怕的了 [DP,容斥]
传送门 思路 大佬都说这是套路题--嘤嘤嘤我又被吊打了\(Q\omega Q\) 显然,这题是要\(DP\)的. 首先思考一下性质: 为了方便,下面令\(k=\frac{n+k}{2}\),即有恰好\ ...
- springmvc框架原理分析和简单入门程序
一.什么是springmvc? 我们知道三层架构的思想,并且如果你知道ssh的话,就会更加透彻的理解这个思想,struts2在web层,spring在中间控制,hibernate在dao层与数据库打交 ...
- IBM X 3650 M3服务器RAID0设置
1 进入磁盘整列设置窗口 1.1 开机在提示符页面下按[F1]进入BIOS设置 1.2 依次进入子菜单[System Settings]à[Adapters and UEFI Drivers] 1.3 ...
- ACM-ICPC 2018 焦作赛区网络预赛 G Give Candies
There are NNN children in kindergarten. Miss Li bought them NNN candies. To make the process more in ...
- Confluence 6 用户宏示例 - Hello World
下面示例显示了如何创建一个用户宏,在这个用户宏中显示文本 'Hello World!' 和任何用户在宏内容中输入的内容. Macro name helloworld Visibility Visibl ...
- python 爬虫简化树状图
- npm常用模块
https://blog.csdn.net/heliumlau/article/details/70577727
- trade war
问题 C: trade war 时间限制: 1 Sec 内存限制: 128 MB 题目描述 2018年的春天,特朗普这个不靠谱的的家伙悍然向中国发起了贸易战,贸易战是一场没有赢家的战争,美国向中国商 ...
- add web server(nginx)
#!/bin/bash # # Web Server Install Script # Last Updated 2012.09.24 # ##### modify by WanJie 2012.09 ...
- linux基础练习题(3)
关卡三 练习题 在家路径下创建A文件夹 在上一步创建的A文件夹中,创建B/C/D文件夹 在上一步所在的路径中,创建C/E/F文件夹 使用目录树查看文件夹结构 A/B/C中创建一个hello.py文件 ...