HDU 6148 Valley Numer (数位DP)
2024-09-04 14:20:12
题意:。。。
析:好久没写数位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;
}
最新文章
- redis大幅性能提升之使用管道(PipeLine)和批量(Batch)操作
- [恶趣味]搞了下局域网内的arp网络欺骗
- GUI基础学习
- Cornerstone无法上传静态库文件(.a文件)
- 7、网页制作Dreamweaver(悬浮动态分层导航)
- Centos使用dd命令制作U盘启动盘 wodim刻录光盘
- 创业路(VC Pipeline),创业需要融资的阅读
- 015_ICMP专项研究监控
- pyqt5-数据库加载错误解决
- Javascript动态引用CSS文件的2种方法介绍
- 剑指offer——python【第14题】链表中倒数第k个节点
- TF(2): 核心概念
- teleport使用说明
- idea 插件的使用 进阶篇(个人收集使用中的)
- Yahoo邮箱最后登录,成为历史!
- webbrowser载入地图网页出现脚本错误解决
- wex5 onactive不执行的解决办法
- linux -- ubuntu搭建nodejs环境
- [转] Scala Async 库 (Scala future, await, async)
- 嵌入式C语言自我修养 10:内联函数探究