ZOJ 3436 July Number(DFS)
题意 把一个数替换为这个数相邻数字差组成的数 知道这个数仅仅剩一位数 若最后的一位数是7 则称原来的数为 July Number 给你一个区间 求这个区间中July Number的个数
从7開始DFS 位数多的数总能由位数小的数推出
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
int july[N], n;
set<int> ans; int pw[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};
//剩余长度, 当前位要填的数,通过什么数来搜索, 路径
void dfs(int len, int d, int bas, int cur)
{
if(d < 0 || d > 9) return; //要填的数不合法
if(!len)
{
july[n++] = cur * 10 + d;
return;
}
int k = pw[len - 1];
dfs(len - 1, d - bas / k, bas % k, cur * 10 + d);
dfs(len - 1, d + bas / k, bas % k, cur * 10 + d);
} int main()
{
ans.insert(7);
set<int>::iterator it;
for(int l = 2; l < 10; ++l)
{
n = 0;
for(it = ans.begin(); it != ans.end(); ++it)
for(int i = 1; i < 10; ++i)
dfs(l - 1, i, *it, 0);
for(int i = 0; i < n; ++i) ans.insert(july[i]);
//printf("%d\n", ans.size());
} int a, b = n = 0;
for(it = ans.begin(); it != ans.end(); ++it)
july[n++] = *it;
while(~scanf("%d%d", &a, &b))
printf("%d\n", upper_bound(july, july + n, b) - lower_bound(july, july + n, a)); return 0;
}
July Number
Time Limit: 2 Seconds Memory Limit: 65536 KB
The digital difference of a positive number is constituted by the difference between each two neighboring digits (with the leading zeros omitted). For example the digital difference of
1135 is 022 = 22. The repeated digital difference, or differential root, can be obtained by caculating the digital difference until a single-digit number is reached. A number whose differential root is 7 is also called July Number. Your job is to tell how
many July Numbers are there lying in the given interval [a, b].
Input
There are multiple cases. Each case contains two integers a and b. 1 ≤ a ≤ b ≤ 109.
Output
One integer k, the number of July Numbers.
Sample Input
1 10
Sample Output
1
Author: HE, Ningxu
Contest: ZOJ Monthly, November 2010
ZOJ 3436 July Number(DFS)的更多相关文章
- [递推+dfs]ZOJ 3436. July Number
题目大意: 将一个数字的相邻两位的差(的绝对值)组成一个新的数字.不断反复.假设最后得到7,就称这个数为July Number,比方9024 – 922 – 70 – 7. 题目要求1e9范围内给定区 ...
- UVA10624 - Super Number(dfs)
题目:UVA10624 - Super Number(dfs) 题目大意:给你n和m要求找出这种m位数,从第n位到第m位都满足前i位是能够被i整除,假设没有这种数,输出-1.有多个就输出字典序最小的那 ...
- 思维+multiset ZOJ Monthly, July 2015 - H Twelves Monkeys
题目传送门 /* 题意:n个时刻点,m次时光穿梭,告诉的起点和终点,q次询问,每次询问t时刻t之前有多少时刻点是可以通过两种不同的路径到达 思维:对于当前p时间,从现在到未来穿越到过去的是有效的值,排 ...
- ZOJ - 3816 Generalized Palindromic Number dfs
Generalized Palindromic Number Time Limit: 2 Seconds Memory Limi ...
- poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)
Sum It Up Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- ZOJ 1008 Gnome Tetravex(DFS)
Gnome Tetravex Time Limit: 10 Seconds Memory Limit: 32768 KB Hart is engaged in playing an inte ...
- ZOJ 2412 Farm Irrigation(DFS 条件通讯块)
意甲冠军 两个农田管内可直接连接到壳体 他们将能够共享一个水源 有11种农田 管道的位置高于一定 一个农田矩阵 问至少须要多少水源 DFS的连通块问题 两个相邻农田的管道能够直接连接的 ...
- UVA11882-Biggest Number(DFS+最优化剪枝)
Problem UVA11882-Biggest Number Accept: 177 Submit: 3117Time Limit: 1000 mSec Memory Limit : 1 ...
- LeetCode Letter Combinations of a Phone Number (DFS)
题意 Given a digit string, return all possible letter combinations that the number could represent. A ...
随机推荐
- 如何学习Linux
为啥我们要学习Linux 技术的价值不在于这个技术有多么高超,而在于技术本身给我们带来什么价值,所以很多时候我们学习一个技术,不能盲目学,是为了使用这个技术,知道这个技术的使用场景,知道这个技术带来的 ...
- 使用perldoc阅读perl文档
perl在安装的时候,就给我们送上一份大礼,组织精美,解释详细的perl百科全书已经安装在你的电脑里面了,遇到问题不要在去搜索那些博客了,还是练练英文,看看perldoc吧,呵呵. 1.用perldo ...
- 关于locate这个NB命令我不得不深入的学习
先看看locate的安装包和生成的文件: [root@NB mlocate]# which locate /usr/bin/locate [root@NB mlocate]# rpm -qf /usr ...
- angular学习笔记(十三)
本篇主要介绍控制器的$scope中的数据是如何被改变的: 以下三种方法,都可以改变$scope中的number值: 1. 表达式: <span ng-click="number=num ...
- vue前端导出zip包
1. npm install jszip /npm install script-loader / npm install file-saver 2.功能代码 require('script-loa ...
- 在eclipse中执行sql
只要你配置好了你的database(在Data Source Explorer中,可以通过window->show view打开) 写好你的sql script,然后配置好profile 右键, ...
- hdu2199(高精度二分模版)
Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and ...
- ExtJs 常用小技巧备忘录
1. ExtJs 给fieldLabel与fieldInput添加样式{给Input标签加入图标}http://www.w3school.com.cn/cssref/pr_background.asp ...
- iOS开发中的错误整理,导航控制器的导航栏取消系统渲染的错误
- FATAL ERROR: Could not find ./bin/my_print_defaults 解决方法
FATAL ERROR: Could not find ./bin/my_print_defaults If you compiled from source, you need to run 'ma ...