hihoCoder 1033 : 交错和 数位dp
思路:数位dp,dp(i, j, k)表示考虑i位数,每位数可以任意取[0~9],并且这i位数的交错和为j,k=1表示前缀全是0(如000456),k=0表示前缀不为0。注意,前缀是否为0是这道题的一大坑点。在计算交错和的过程中可能会出现负数,这时应该加上一个数让它变成非负整数。f(123) = f(1) - f(23),根据这个来进行状态转移。
AC代码
#include <cstdio>
#include <cmath>
#include <cctype>
#include <bitset>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<long long, long long>
typedef long long LL;
const int maxn = 400 + 5;
const int Non = 200, mod = 1e9+7;
LL dp[20][maxn][2], cnt[20][maxn][2];
int b[25];
int k;
PI dfs(int pre, int d, int flag, int u, int pre_zero) {
if(d == 0) {
if(pre == k) return make_pair(0, 1);
else return make_pair(0, 0);
}
int w = (k-pre)/u + Non;
if(!flag && dp[d][w][pre_zero] != -1)
return make_pair(dp[d][w][pre_zero], cnt[d][w][pre_zero]);
int up = flag ? b[d] : 9;
LL x = 0, y = 0;
for(int i = 0; i <= up; ++i) {
PI pi;
if(pre_zero) {
if(i == 0) pi = dfs(0, d-1, flag&(i==up), 1, 1);
else pi = dfs(i, d-1, flag&(i==up), -1, 0);
}
else pi = dfs(pre + u*i, d-1, flag&(i==up), -u, 0);
//用tmp防止溢出
LL tmp = pi.second * i % mod;
tmp *= ((LL)pow(10, d-1))%mod;
x += tmp + pi.first;
x %= mod;
y += pi.second;
y %= mod;
}
if(!flag) {
dp[d][w][pre_zero] = x;
cnt[d][w][pre_zero] = y;
}
return make_pair(x, y);
}
int getBit(LL x) {
int cur = 1;
while(x) {
b[cur++] = (int)(x%10);
x /= 10;
}
return cur-1;
}
LL solve(LL x) {
if(x <= 0) return 0;
int n = getBit(x);
PI ans = dfs(0, n, 1, 1, 1);
return ans.first;
}
int main() {
memset(dp, -1, sizeof(dp));
memset(cnt, 0, sizeof(cnt));
LL l, r;
while(scanf("%lld%lld%d", &l, &r, &k) == 3) {
LL a = solve(r), b = solve(l-1);
printf("%lld\n", (a-b+mod)%mod);
}
return 0;
}
如有不当之处欢迎指出!
hihoCoder 1033 : 交错和 数位dp的更多相关文章
- [hihocoder 1033]交错和 数位dp/记忆化搜索
#1033 : 交错和 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an - 1 ...
- HihoCoder 1033交错和(数位DP第三题)
(写挂了,有空再补) 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an - 1,定义 ...
- hihoCoder1033 交错和 数位DP
题目:交错和 链接:http://hihocoder.com/problemset/problem/1033# 题意:对于一个十进制整数x,令a0.a1.a2.....an是x从高位到低位的数位,定义 ...
- hihoCoder 1033: 交错和
(1)题目描述: 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an - 1,定义交错 ...
- hihoCoder #1033 : 交错和 (数位Dp)
题目大意: 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an - 1,定义交错和函数: f(x) = a0 - a1 + a2 - ... + ( - 1)n - ...
- 【hihoCoder】1033: 交错和
初探数位dp 介绍了数位类统计的基础知识.以下列出其中的基础点: 基本问题 统计在区间[l, r]中满足条件的数的个数 思路 1. [l, r] 将问题转换为 在[0, r]中满足条件的个数 - 在[ ...
- hihocoder #1301 : 筑地市场 数位dp+二分
题目链接: http://hihocoder.com/problemset/problem/1301?sid=804672 题解: 二分答案,每次判断用数位dp做. #include<iostr ...
- hihoCoder #1770 : 单调数(数位dp)
题面 我们定义一个数是单调数,当且仅当构成这个数每一个数位都是单调不降或不增的. 例如 \(123\) 和 \(321\) 和 \(221\) 和 \(111\) 是单调的,而 \(312\) 不是单 ...
- hihocoder #1301 : 筑地市场 二分+数位dp
#1301 : 筑地市场 题目连接: http://hihocoder.com/problemset/problem/1301 Description 筑地市场是位于日本东京都中央区筑地的公营批发市场 ...
随机推荐
- TypeError: 'encoding' is an invalid keyword argument for this function
python 2.7 问题 data_file = open("F:\\MyPro\\data.yaml", "r", encoding='utf-8') 运行 ...
- 移动端 -webkit-user-select:text; ios10 bug 解决方案
移动端一般body的css.会设置 作用就不解释了: body{ height:100%;min-height:100%; font-family: "微软雅黑",'Helveti ...
- web项目各个clean
project clean:清楚tomcat下的已编译的java类.class文件,包括js但不包括jsp server clean:clean tomcat work dictionary:清除to ...
- asp.net core如何自定义端口/修改默认端口
.net core运行的默认端口是5000,但是很多时候我们需要自定义端口.有两种方式 写在Program的Main方法里面 添加 .UseUrls() var host = new WebHostB ...
- CURL模拟post请求上传文件
贴一段醍醐灌顶的话: 逻辑很简单,设置POST变量$post_data,其中upload指向需要发送的文件.这里要注意,我们之前使用POST都是发送一个字符串,然后在服务器端使用file_get_co ...
- linux配置上网
重装系统之后,配置虚拟机的网络问题花了我三个小时,忘记了网关是vmnet8的IP还是DNS了,搞了很久,后来碰运气碰对了. 寄宿机共享的网络是vmnet8,设置IP,DNS,是vmnet8 的IPv4 ...
- WEB渗透测试之漏扫神器
AppScan 对现代 Web 应用程序和服务执行自动化的动态应用程序安全测试(DAST) 和交互式应用程序安全测试 (IAST).支持 Web 2.0. JavaScript 和 AJAX 框架的全 ...
- 使用babel转换es6编写的程序
配置文件 Babel的配置文件是.babelrc,存放在项目的根目录下.使用Babel的第一步,就是配置这个文件,这是必要的一步. 该文件用来设置转码规则和插件,基本格式如下. { "pre ...
- 17_Python装饰器
一.什么是装饰器 目的:给func()方法,增加一个功能,在fun()执行期间,同时把fun()执行速率机算出来 import time def func(): print('嘻嘻哈哈') start ...
- Cypher查询语言--Neo4j-MATCH(二)
目录 Match 相关节点 接出关系Outgong relationship 定向关系和标识符 通过关系类型匹配 通过关系类型匹配和使用标识符 带有特殊字符的关系类型 多重关系 可变长度的关系 在可变 ...