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 ...
随机推荐
- python 函数 动态参数 和嵌套
1.动态参数 是可以接收任意的参数.一种方式, 1,位置的动态传参, 写法是: *参数名 接收的参数是tuple类型举个例子:def yue(*food): print(food)yue(" ...
- 粘包-socketserver实现并发
- Android:四大架构的优缺点,你真的了解吗?
声明|转载于作者:KunMinX原文链接:https://www.jianshu.com/p/9ef813d5c1af 前言 前不久刚结束对 20 模块项目的第 3 轮重构,一路见证 MVC.MVP. ...
- Redis的消息发布和订阅
Redis的消息发布和订阅 Author:SimpleWu GitHub-redis 什么是消息发布和订阅? Redis 发布订阅(pub/sub)是一种进程间的消息通信模式: 发送者(pub)发送消 ...
- 五.Bash Shell编程基础入门实战
知识回顾 运行脚本我们一般用sh 不用单独去加执行权限 OLDBOY=10只适用当前环境 局部变量 export OLDBOY把它设置为临时的环境变量应为已经=10了所以不用export OLDBOY ...
- select下拉框使用完毕后,重置按钮使其清空
需求描述:select下拉框后边有两个按钮,一个查询,一个重置,点击重置,select会清空之前选择的那个查询条件 解决思路:卧槽,这不so easy 么,用那个jQ封装的trigger函数搞定啊,对 ...
- react 中子组件调用父组件的方法
1.在父组件中定义方法,并绑定在子组件上 // 在子组件中调用父组件中的方法 import React,{Component} from 'react'; import Child from './c ...
- python(3):文件操作/os库
文件基本操作 r,以读模式打开, r+=r+w, w, 写模式(清空原来的内容), w+=w+r, a , 追加模式, a+=a+r, rb, wb, ab, b表示以二进制文件打开 想在一段文 ...
- cf1025c 思维题
/* bwwwbwwbw wwbwwwbwb 不管从哪里断开翻转.翻转后的串再整体翻转一定是2s的子串 */ #include<bits/stdc++.h> using namespace ...
- 第三周学习总结-Java
2018年7月29日 这是暑假第三周.这一周我把找到的Java教学视频看完了. 本周学到了Java剩余的基础知识,比如:抽象类.接口.内部类.几种常用类.IO流.多态.多线程等等. 因为没有书,所以我 ...