M - 上帝造题的八分钟(数位dp,dfs—dp,****)

news/2024/9/23 9:50:38/

链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

我们称一个数为 fufu 数,当且仅当一个数满足下述性质:它的数位中有至少 333 个相邻的数字 333,且数字 000 的个数与数字 111 的个数相差至少为 111 的正整数。

给定一个区间 [l,r][l,r][l,r],你需要求出区间内的 fufu 数的个数。

答案对 109+710^9+7109+7 取模。

输入描述:

输入一行,包括两个正整数 l,r(1≤l≤r≤10^100),表示你要查找的区间。

输出描述:

输出一行,一个正整数,表示区间内满足题目所描述性质的数量。答案对 10^9 +7 取模。

示例1

输入

1 500

输出

0

说明

显然在 [1,500] 内没有 fufu 数。

示例2

输入

1333 1333

输出

1

说明

1333 中有 3 个相邻的数字 3,数字 0 的个数为 0,数字 1 的个数为 1,满足 fufu 数的性质。

示例3

输入

1 20000

输出

20

解析:

这道题显然是一道数位dp题。

f[pos][cp][pre1][pre2][limit][n3][last] 表示:

从高位往低位看,第pos位,0和1的个数差为cp,前一位为pre1,前二位为pre2,上一位是否为最大值,是否含有3个连续的3,当前一位是0还是1。

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
#include<sstream>
#include<deque>
#include<unordered_map>
#include<unordered_set>
#include<bitset>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<long long, long long> PLL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
const int INF = 0x3f3f3f3f;
const LL Mod = 1e9 + 7;
const int N = 100 + 10, M = 4e5 + 10, P = 110;
string l, r;
LL f[105][205][11][11][2][2][2];LL dp(int pos, int cp, int pre1, int pre2, int limit, int n3, int last,vector<int>&num) {LL ret = 0;if (pos == -1)return n3 && (abs(cp - 100) >= 1);if (f[pos][cp][pre1][pre2][limit][n3][last])return f[pos][cp][pre1][pre2][limit][n3][last];int up = limit ? num[pos] : 9;for (int i = 0; i <= up; i++) {if (last && i == 0) {ret += dp(pos - 1, cp, i, pre1, limit && (i == up), n3, 1, num);}else {ret += dp(pos - 1, cp + (i == 0) - (i == 1), i, pre1, limit && (i == up), n3 | (pre1 == 3 && pre2 == 3 && i == 3), 0, num);}ret %= Mod;}return f[pos][cp][pre1][pre2][limit][n3][last] = ret;
}LL work(vector<int>num) {memset(f, 0, sizeof f);return dp(num.size()-1, 100, 0, 0, 1, 0, 1,num);
}bool check(vector<int>num) {int n1 = 0, n0 = 0;for (int i = 0; i < num.size(); i++) {if (num[i] == 1)n1++;else if (num[i] == 0)n0++;}if (!abs(n1 - n0))return 0;int n3 = 0;for (int i = 0; i < num.size(); i++) {if (num[i] == 3) {n3++;if (n3 == 3)return 1;}else {n3 = 0;}}return 0;
}int main() {vector<int>L, R;cin >> l >> r;for (int i = 0; i < l.size(); i++) {L.push_back(l[i] - '0');}for (int i = 0; i < r.size(); i++) {R.push_back(r[i] - '0');}reverse(L.begin(), L.end());reverse(R.begin(), R.end());//cout << "___________________" << check(L) << endl;LL ans = (((work(R) - work(L)) % Mod + check(L)) %Mod+ Mod) % Mod;cout << ans << endl;return 0;
}


http://www.ppmy.cn/news/1433819.html

相关文章

C语言实现双人贪吃蛇项目(基于控制台界面)

一.贪吃蛇 贪吃蛇是一款简单而富有乐趣的游戏&#xff0c;它的规则易于理解&#xff0c;但挑战性也很高。它已经成为经典的游戏之一&#xff0c;并且在不同的平台上一直受到人们的喜爱和回忆。 二.贪吃蛇的功能 游戏控制&#xff1a;玩家可以使用键盘输入设备来控制蛇的移动方…

机器学习--第六次课

前言 先讲课,大概的重点是回归和梯度下降!!! 先回顾一手,pr曲线啥的 正文 回归和分类 回归简单来说就是输出一个数值,就是回归问题,例如预测明天的空气指数,而分类问题就是做选择题 ,也就是,分类,那么回归为啥叫回归呢?可以理解为希望输出的数字回归到真实值附近.他们虽然…

安全小课堂丨什么是暴力破解?如何防止暴力破解

什么是暴力破解&#xff1f; 暴力破解也可称为穷举法、枚举法&#xff0c;是一种比较流行的密码破译方法&#xff0c;也就是将密码进行一一推算直到找出正确的密码为止。比如一个6位并且全部由数字组成的密码&#xff0c;可能有100万种组合&#xff0c;也就是说最多需要尝试10…

一个基于 Vue.js 实现的简单增删改查(CRUD)案例

以下是一个基于 Vue.js 实现的简单增删改查&#xff08;CRUD&#xff09;案例&#xff0c;使用了 Vue CLI 创建项目&#xff0c;并假设你已经安装了 Vue CLI。这个案例将展示如何创建一个简单的待办事项列表&#xff08;Todo List&#xff09;&#xff0c;实现添加、删除、编辑…

项目开发的详细步骤(精华版)

项目开发是指从项目启动到项目交付的全过程&#xff0c;涵盖了需求分析、设计、编码、测试、部署等多个环节。以下为项目开发的详细步骤&#xff1a; 1. 项目启动与规划 项目立项 商业论证&#xff1a;分析项目投资回报率、风险、市场前景等&#xff0c;确定项目价值和可行性…

Element Plus CDN时间选择器英文转中文

本文主要介绍Element Plus时间选择器如何把英文转中文 直接上代码 <link rel="stylesheet" href="element-plus@1.0.2/index.css"> <script src="element-plus@1.0.2/index.js"></script> <script src="element-plus…

Python的一些中级用法

Python的中级用法涵盖了更复杂的编程技巧和概念&#xff0c;包括函数式编程、面向对象编程、模块化设计、文件操作、异常处理等。下面是Python的一些中级用法&#xff1a; 1.列表推导式 使用简洁的语法创建列表。 # 生成一个包含1到10的平方的列表 squares [x**2 for x in …

Vue3的语法糖进行父子组件传值

父组件 // 对于父组件来说&#xff0c;跟Vue2的写法差不多 <template><children :selection"multipleSelection"></children> </template> <script setup lang"ts">// 定义变量 let multipleSelection: Array<object>…