HDU 6148 Valley Numer (数位DP)
题意:。。。
析:好久没写数位DP了,几乎就是不会了。。。。
dp[i][last][s] 表示前 i 位上一位是 last,当前的状态是 s,0表示非上升,1 表示非下降,然后就很简单了,只有 0 能转成 1,1就是最后的状态。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000 + 10;
const LL mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r > 0 && r <= n && c > 0 && c <= m;
} LL dp[maxn][11][2];
char s[maxn];
int a[maxn]; LL dfs(int pos, int last, int s, bool is, bool ok){
if(!pos) return !is;
LL &ans = dp[pos][last][s];
if(!is && !ok && ans >= 0) return ans; int n = ok ? a[pos] : 9;
LL res = 0;
if(last == 10){
res += dfs(pos-1, 10, s, 1, ok && 0 == n);
for(int i = 1; i <= n; ++i)
res += dfs(pos-1, i, 0, 0, i == n && ok);
}
else if(s){
for(int i = last; i <= n; ++i)
res += dfs(pos-1, i, s, i == 0 && is, ok && i == n);
}
else{
for(int i = 0; i <= n; ++i)
if(i > last) res += dfs(pos-1, i, 1, is && i == 0, ok && i == n);
else res += dfs(pos-1, i, 0, is && i == 0, ok && i == n);
}
res %= mod;
if(!is && !ok) ans = res;
return res;
} LL solve(char *s){
int len = 0;
n = strlen(s);
for(int i = n-1; i >= 0; --i)
a[++len] = s[i] - '0';
return dfs(len, 10, 0, 1, 1);
} int main(){
ms(dp, -1);
int T; cin >> T;
while(T--){
scanf("%s", s);
printf("%I64d\n", solve(s));
}
return 0;
}
HDU 6148 Valley Numer (数位DP)的更多相关文章
- 【HDU】6148 Valley Numer 数位DP
[算法]数位DP [题意]定义V-number为从左到看单位数字未出现先递增后递减现象的数字,求0~N中满足条件的数字个数.T<=200,lenth(n)<=100 [题解]百度之星201 ...
- 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 6148 Valley Numer (数位DP)题解
思路: 只要把status那里写清楚就没什么难度T^T,当然还要考虑前导零! 代码: #include<cstdio> #include<cstring> #include&l ...
- HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛
普通的数位DP计算回文串个数 /* HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 2-36进制下回文串个数 */ ...
- hdu 5898 odd-even number 数位DP
传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...
- hdu 5106 Bits Problem(数位dp)
题目链接:hdu 5106 Bits Problem 题目大意:给定n和r,要求算出[0,r)之间全部n-onebit数的和. 解题思路:数位dp,一个ct表示个数,dp表示和,然后就剩下普通的数位d ...
- HDU 2089 - 不要62 - [数位DP][入门题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 Time Limit: 1000/1000 MS (Java/Others) Memory Li ...
- HDU 5179 beautiful number 数位dp
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5179 bc(中文): http://bestcoder.hdu.edu.cn/contes ...
- [hdu 2089] 不要62 数位dp|dfs 入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求[n, m]区间内不含4和62的数字个数. 这题有两种思路,直接数位dp和dfs 数位d ...
随机推荐
- 【洛谷】P1474 货币系统 Money Systems(背包dp)
题目描述 母牛们不但创建了它们自己的政府而且选择了建立了自己的货币系统.由于它们特殊的思考方式,它们对货币的数值感到好奇. 传统地,一个货币系统是由1,5,10,20 或 25,50, 和 100的单 ...
- python操作csv
# -*- coding: utf-8 -*- #python 27 #xiaodeng #CSV文件的写入(按行写入) import csv #csv文件,是一种常用的文本格式,用以存储表格数据,很 ...
- CentOS 修改源为163和指定epel源和docker安装
首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-B ...
- git submodule一些操作
checkout指定tag cd /path/to/yoursubmodule git checkout yourTag cd .. git add yoursubmodule git commit ...
- leetcode720
public class Solution { public string LongestWord(string[] words) { var maxlist = new List<string ...
- MPEG-2码流结构分析
MPEG2视频编码定义在 ISO/IEC13818-2中,MPEG2 video sequence如下图所示 我们可以借助Elecard Stream Analyer工具来分析MPEG2视频码流 MP ...
- GPU架构图
找了几张GPU架构图,对理解图形渲染管线很有帮助
- Coursera连接不上(视频无法播放),修改hosts文件
视频问题 如果Coursera网站连接不上,或者视频加载不出来.可以通过如下方式进行配置: 一.找到hosts文件 Windows 系统, hosts文件位于: [C:\Windows\Syste ...
- linux shell 脚本攻略学习 -- head命令详解, tail命令详解
当要查看上千行的大文件时,我们可不会用cat命令把整个文件内容给打印出来,相反,我们可能只需要看文件的一小部分地内容(例如文件的前十行和后十行),我们也有可能需要打印出来前n行或后n行,也有可能打印除 ...
- partial分部类
意义 1.源代码控制 2.将一个类或结构分成不同的逻辑单元 3.代码拆分