题意:

  定义一个具有2n位的正整数,其前n位之和与后n位之和相等,则为lucky数。给定一个区间,问有多少个正数可以通过修改某一位数从而变成lucky数?注意不能含前导0。

思路:

  我的想法是记录那些非lucky数,再想办法来统计,后来发现有点行不通,无法知道其前后部之和是否相等。如果记录lucky数,然后通过统计每个位上的数来变成lucky数,这更麻烦,因为会重复统计,比如11和22是lucky数,而21可以通过修改1位来变成lucky数,被统计了两次。

  学习了前辈的方法,也强迫一下自己别人的模板。据我对此模板的理解,第一次求解时是直接求解的,但是把所有统计过的都记录起来了,下次若还用到就直接返回就行了。复杂度是108吧。

  这是前辈们的代码,拿来理解一下,顺便适应一下新模板。

 #include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define INF 0x7f3f3f3f
#define LL long long
#define ULL unsigned long long
using namespace std;
const double PI = acos(-1.0);
const int N=;
const int M=;
const int mod=1e9+; int f[N][N][][N][N], bit[N+]; int dfs(int i,int up,int sum,int more,int less,bool e) //e表示:是前缀?
{
if(i==)
return (sum!=M && ( sum+more>=M && sum-less<=M ) ); if(!e && ~f[i][up][sum][more][less]) //已经计算好了(非前缀才行)
return f[i][up][sum][more][less]; int ans=;
int d= i==up? : ; //起始,注意首位不能为0啊
int u= e? bit[i]: ; //终止,注意末位不能超啊
for( ; d<=u; d++) //是否为最后一个取决于参数e
{
int ssum= i>(up>>)? sum+d: sum-d; //单峰形的
int mmore= i>(up>>)? max(more, -d): max(more, d); //前部:可加。后部:可减
int lless= i>(up>>)? max(less, i==up?d-:d): max(less, -d);//前部:可减。后部:可加
ans+=dfs(i-,up, ssum, mmore, lless, e&&d==u);
}
return e? ans: f[i][up][sum][more][less]=ans; //前缀的返回不同
} int cal(int n)
{
if(n<) return ; //仅个位数不可能是lucky数
int len=, ans=;
while(n) //拆数
{
bit[++len]=n%;
n/=;
} for(int i=; i<=len; i+=) //i是数的长度
ans+=dfs(i,i,+M,,,i==len);
return ans;
} int main()
{
//freopen("input.txt","r",stdin);
memset(f,-,sizeof(f));
int L, R;
while( ~scanf("%d%d",&L,&R) )
printf("%d\n", cal(R)-cal(L-) );
return ;
}

AC代码

最新文章

  1. 关于png、jpg、gif切图时的使用感悟
  2. Unity3D之随心所欲的获取对象
  3. 开始VS 2012中LightSwitch系列的第3部分:我该选择哪一个屏幕模板
  4. DateTime时间格式
  5. [BZOJ3504][CQOI2014]危桥(最大流)
  6. jspm 是浏览器包管理工具
  7. andori 动画验证必填项
  8. 2014.first[未填]
  9. 前端教程&amp;开发模块化/规范化/工程化/优化&amp;工具/调试&amp;值得关注的博客/Git&amp;面试-资源汇总
  10. 使用 Bitbucket Pipelines 持续交付托管项目
  11. KoaHub.js:使用ES6/7特性开发Node.js框架(2)
  12. 设计模式--代理模式(C++版)
  13. [Nodejs] node写个hello,world
  14. 如何利用GitHub设计一个炫酷的个人网站(含代码)
  15. ES5-ES6-ES7_Promise对象详解
  16. javascript unicode与GBK2312(中文)编码转换示例
  17. static, const 和 static const 变量的初始化问题
  18. Django - CBV、FBV
  19. 当重写了 httpservlet重写了GenericServlet的init方法时候 必须显示调用GenericServlet的init方法时候 才能在别的方法(父类创建config实例) 例如 doget里面使用servletContext对象 不重写init 则可以直接使用
  20. 用node是踩过的一些坑

热门文章

  1. [poj1811]Prime Test(Pollard-Rho大整数分解)
  2. php学习笔记-PHP中的几个取整函数
  3. 1、CDH集群搭建
  4. bootstrap的popover()的使用
  5. Linux下的eclipse的安装
  6. 超级实用的Chrome插件
  7. C# 写 LeetCode easy #13 Roman to Integer
  8. MySQL下载与安装配置
  9. 免打包:简单、灵活、便捷的APP渠道统计方法
  10. EOS 插件依赖关系