HDU 5787 K-wolf Number 数位DP
K-wolf Number
Given (L,R,K), please count how many K-wolf numbers in range of [L,R].
Each test case contains three integers L, R and K.
1≤L≤R≤1e18
2≤K≤5
20 100 5
72
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
const int N = 1e5+, M = 6e4+, mod = 1e9+, inf = 1e9+;
typedef long long ll; ll L,R;
ll k,d[N],K; ll dp[][][][][];
bool vis[][][][][]; ll dfs(int dep,int f,int now[],int bo) {
int x = now[], y = now[], z = now[], e = now[];
if(dep < ) return ;
if(f&&vis[dep][x][y][z][e]) return dp[dep][x][y][z][e];
if(f) { vis[dep][x][y][z][e] = true;
ll& ret = dp[dep][x][y][z][e]; for(int i = ; i <= ; ++i) {
int OK = ;
for(int j = ; j < k-; ++j) if(now[j] == i) {OK = ; break;}
if(OK == ) continue;
if(!bo && !i) ret += dfs(dep-,f,now,bo);
else {
int tmpnow[];
for(int j = ; j <= ; ++j) tmpnow[j] = now[j+];
tmpnow[] = ;
tmpnow[k-] = i;
ret += dfs(dep-,f,tmpnow,bo||i);
}
}
return ret; }else {
ll ret = ;
for(int i = ; i <= d[dep]; ++i) { int OK = ;
for(int j = ; j < k-; ++j) if(now[j] == i) {OK = ; break;}
if(OK == ) continue;
if(!bo && !i) ret += dfs(dep-,i<d[dep],now,bo);
else { int tmpnow[];
for(int j = ; j <= ; ++j) tmpnow[j] = now[j+];
tmpnow[] = ;
tmpnow[k-] = i;
ret += dfs(dep-,i<d[dep],tmpnow,bo||i);
} }
return ret;
}
} ll solve(ll x) {
//if(x < 0) return 0;
memset(vis,false,sizeof(vis));
memset(dp,,sizeof(dp));
int len = ;
while(x) {
d[len++] = x % ;
x /= ;
}
int now[];
for(int i = ; i <= ; ++i) now[i] = ;
return dfs(len-,,now,);
} int main()
{ while(~scanf("%I64d%I64d%I64d",&L,&R,&k)) {
//K = pre[k];
printf("%I64d\n",solve(R) - solve(L-));
} /* ll x;
while(~scanf("%I64d%d",&x,&k)) {
K = pre[k];
printf("%I64d\n",solve(x));
}
*/ }
HDU 5787 K-wolf Number 数位DP的更多相关文章
- 多校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 用作标记,当现在枚举的数小 ...
- HDU.4352.XHXJ's LIS(数位DP 状压 LIS)
题目链接 \(Description\) 求\([l,r]\)中有多少个数,满足把这个数的每一位从高位到低位写下来,其LIS长度为\(k\). \(Solution\) 数位DP. 至于怎么求LIS, ...
- hdu 5898 odd-even number 数位DP
传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...
- HDU 3709 Balanced Number (数位DP)
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- HDU 5179 beautiful number 数位dp
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5179 bc(中文): http://bestcoder.hdu.edu.cn/contes ...
- hdu 5898 odd-even number(数位dp)
Problem Description For a number,if the length of continuous odd digits is even and the length of co ...
- HDU 5898 odd-even number (数位DP) -2016 ICPC沈阳赛区网络赛
题目链接 题意:一个数字,它每个数位上的奇数都形成偶数长度的段,偶数位都形成奇数长度的段他就是好的.问[L , R]的好数个数. 题解:裸的数位dp, 从高到低考虑每个数位, 状态里存下到当前位为止的 ...
- 2017"百度之星"程序设计大赛 - 复赛1005&&HDU 6148 Valley Numer【数位dp】
Valley Numer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 4352 - XHXJ's LIS - [数位DP][LIS问题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4352 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
随机推荐
- c里面的fflush函数
NAME fflush - flush a stream SYNOPSIS #include <stdio.h> int fflush(FILE *stream); DESCRIPTION ...
- strcpy C++实现
#include <iostream> #include <assert.h> using namespace std; char *strcpy(char *strDest, ...
- 【leetcode】Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- IDEA 编译时报错 “未结束的字符串文字” “解析时已经达到文件结尾”
Information:Using javac 1.7.0_75 to compile java sourcesInformation:java: Errors occurred while comp ...
- ACM/ICPC 之 拓扑排序范例(POJ1094-POJ2585)
两道拓扑排序问题的范例,用拓扑排序解决的实质是一个单向关系问题 POJ1094(ZOJ1060)-Sortng It All Out 题意简单,但需要考虑的地方很多,因此很容易将code写繁琐了,会给 ...
- centos搭建SVN三部曲
搭建SVN服务,有效的管理代码,以下三步可以快速搞定. 1.安装 #yum install subversion 判断是否安装成功 #subversion -v svnserve, version 1 ...
- Effective C++ -----条款50:了解new 和delete 的合理替换时机
有许多理由需要写个自定的new 和delete ,包括改善效能.对heap 运用错误进行调试.收集heap 使用信息.
- Effective C++ -----条款04:确定对象被使用前已被初始化
为内置型对象进行手工初始化,因为C++不保证初始化它们. 构造函数最好使用成员初值列,而不要在构造函数本体内使用赋值操作.初值列列出的成员变量,其排列次序应该和它们在class中的声明次序相同. 为免 ...
- Enum:EXTENDED LIGHTS OUT(POJ 1222)
亮灯 题目大意:有一个5*6的灯组,按一盏灯会让其他上下左右4栈和他自己灯变为原来相反的状态,要怎么按才会把所有的灯都按灭? 3279翻版题目,不多说,另外这一题还可以用其他方法,比如DFS,BFS, ...
- 【python】解压文件
参考:http://essen.iteye.com/blog/1941489 tarfile模块 具体使用方法: https://docs.python.org/2/library/tarfile.h ...