CF_EDU51 E. Vasya and Big Integers
传送门:https://codeforces.com/contest/1051/problem/E
题意:
把一个数分成许多小段,每一段的值在L和R间。问有多少种分法。
思路 :
首先,需要快速处理出每个位子 ,最近和最远可以组合的区间。如何处理这个区间,由于时限比较紧张,可以用哈希的方法,因为是长度固定,所以可以用二分确定两端长度前缀,再比较大小的方法。当然哈希要用比较好的双哈希。
然后,我们可以从后往前dp,每个位子的答案 = 这个位子对应区间的值的和。
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = ;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
}
/*-----------------------showtime----------------------*/
const int maxn = 1e6+;
const int base = ;
const int base2 = ;
const int MOD = 1e9+;
//const ll MOD2 = 1e16+17;
int dple[maxn],dpri[maxn];
int lenle,lenri;
ll dp[maxn],sum[maxn];
struct hash
{
char s[maxn];
ll h1[maxn],qp1[maxn];
ull h2[maxn],qp2[maxn];
ll p1[maxn],p2[maxn],len,val1[maxn],val2[maxn]; void init()
{
h1[]=val1[]=;
qp1[]=p1[]=;
len=strlen(s+);
for(int i=;i<=len;i++)
{
qp1[i]=(qp1[i-]*base)%MOD;
h1[i]=(h1[i-]*base%MOD+s[i])%MOD;
} h2[]=val2[]=;
qp2[]=p2[]=;
for(int i=;i<=len;i++)
{
qp2[i]=(qp2[i-]*base2);
h2[i]=(h2[i-]*base2+s[i]);
}
}
ll get_hash1(int l,int r)
{
return ((h1[r]-h1[l-]*qp1[r-l+])%MOD +MOD )% MOD;
}
ull get_hash2(int l,int r)
{
return ((h2[r]-h2[l-]*qp2[r-l+]));
}
}A,L,R; bool checkle(int l1, int r1) {
int le = l1, ri = r1;
while(le <= ri){
int mid = (le + ri) >> ;
if(A.get_hash1(le,mid) == L.get_hash1(le-l1+,mid-l1+) && A.get_hash2(le,mid) == L.get_hash2(le-l1+,mid-l1+)){
le = mid+;
}
else ri = mid - ;
}
if(le == r1 + ) return true;
return A.s[le] >= L.s[le - l1 + ];
} bool checkri(int l1,int r1) {
int le = l1, ri = r1; while(le <= ri){
int mid = (le + ri) >> ; if(A.get_hash1(le,mid) == R.get_hash1(le-l1+,mid-l1+) &&A.get_hash2(le,mid) == R.get_hash2(le-l1+,mid-l1+) ){
le = mid+;
}
else ri = mid-;
}
if(le == r1 + ) return true;
return A.s[le] <= R.s[le - l1 + ];
} int main(){
scanf("%s%s%s", A.s + , L.s + ,R.s + );
A.init(); L.init(); R.init(); ll len = A.len;
int n = len;
lenle = L.len; lenri = R.len;
for(int i=; i<=n; i++){
int le = i, ri = n;
dple[i] = -; dpri[i] = -;
if(A.s[i] == '') {
if(L.s[] == '') dple[i] = i,dpri[i] = i;
else dple[i] = -,dpri[i] = -;
continue;
} if(i + lenle - <= n && checkle(i, i + lenle - )) dple[i] = i + lenle - ;
else if(i + lenle<=n)dple[i] = i + lenle; if(i + lenri - <= n&& checkri(i, i + lenri -)) dpri[i] = i + lenri - ;
else dpri[i] = min(n,i+lenri-); } sum[n+] = ;
for(int i=n; i>=; i--){
int pl = dple[i] + ,pr = dpri[i] + ; dp[i] = ((sum[pl] - sum[pr + ] ) % mod + mod )%mod;
sum[i] = (sum[i+] + dp[i]) % mod;
} cout<<dp[]<<endl;
return ;
}
CF_EDU51 E. Vasya and Big Integers的更多相关文章
- Codeforces 1051E Vasya and Big Integers&1051F The Shortest Statement
1051E. Vasya and Big Integers 题意 给出三个大整数\(a,l,r\),定义\(a\)的一种合法的拆分为把\(a\)表示成若干个字符串首位相连,且每个字符串的大小在\(l, ...
- Codeforces 1051E. Vasya and Big Integers
题意:给你N个点M条边,M-N<=20,有1e5个询问,询问两点的最短距离.保证没有自环和重边. 题解:连题目都在提示你这个20很有用,所以如果是颗树的话那任意两点的最短距离就是求一下lca搞一 ...
- CodeForces - 1051E :Vasya and Big Integers(Z算法 & DP )
题意:给定字符串S,A,B.现在让你对S进行切割,使得每个切割出来的部分在[A,B]范围内,问方案数. 思路:有方程,dp[i]=Σ dp[j] (S[j+1,i]在合法范围内). 假设M和 ...
- ExKMP(Z Algorithm) 讲解
目录 问题引入 CaiOJ 1461 [EXKMP]最长共同前缀长度 算法讲解 匹配过程 next 的求解 复杂度证明 代码解决 一些例题 UOJ #5. [NOI2014]动物园 CF1051E V ...
- 2018SDIBT_国庆个人第一场
A - Turn the Rectangles CodeForces - 1008B There are nn rectangles in a row. You can either turn eac ...
- 2018.9.20 Educational Codeforces Round 51
蒟蒻就切了四道水题,然后EF看着可写然而并不会,中间还WA了一次,我太菜了.jpg =.= A.Vasya And Password 一开始看着有点虚没敢立刻写,后来写完第二题发现可以暴力讨论,因为保 ...
- CF 1008C Reorder the Array
You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it ...
- DP 题集 2
关于 DP 的一些题目 String painter 先区间 DP,\(dp[l][r]\) 表示把一个空串涂成 \(t[l,r]\) 这个子串的最小花费.再考虑 \(s\) 字符串,\(f[i]\) ...
- CodeForces Educational Codeforces Round 51 (Rated for Div. 2)
A:Vasya And Password 代码: #include<bits/stdc++.h> using namespace std; #define Fopen freopen(&q ...
随机推荐
- ld: library not found for -
这几天在做微信登录,总是遇到这个问题,详细如下: ld: library not found for -lWeChatSDK clang: error: linker command failed w ...
- 并发栅栏CyclicBarrier---简单问2
并发栅栏CyclicBarrier---简单问 背景:前几天在网上看到关于Java并发包java.concurrent中一个连环炮的面试题,整理下以备不时之需. CyclicBarrier简介: 栅栏 ...
- poj 1503 高精度加法
把输入的数加起来,输入0表示结束. 先看我Java代码,用BigINteger类很多东西都不需要考虑,比如前导0什么的,很方便.不过java效率低点,平均用时600ms,C/C++可以0ms过. im ...
- vue中使用vue-amap(高德地图)
因为项目要求调用高德地图,就按照官方文档按部就班的捣鼓,这一路上出了不少问题. 前言: vue-cli,node环境什么的自己安装设置推荐一个博客:https://blog.csdn.net/wula ...
- 【Java例题】5.1 多项式计算
1. 计算下列多项式的值. pn=an*x^n+...+a1*x+a0其中,"^"表示乘方. x.n以及ai(i=0,1,...,n-1)由键盘输入. package chapte ...
- 802.11学习笔记1-WIFI参数含义
研究下wifi参数的含义 #The word of "Default" must not be removed Default CountryRegion= CountryRegi ...
- Linux文件及目录管理
1.Linux文件目录树 /:根目录,linux文件系统的最顶端和入口 bin:存放用户二进制文件(如:ls,cd,mv等),实则/user/bin的硬链接(相当于Windows系统的快捷方式) bo ...
- Spring 集成Kafka(完整版)
前面的文章我们已经完成了Kafka基于Zookeeper的集群的搭建了.Kafka集群搭建请点我.记过几天的研究已经实现Spring的集成了.本文重点 jar包准备 集成是基于spring-integ ...
- python第三课--函数
函数的作用 编程大师Martin Fowler先生曾经说过:“代码有很多种坏味道,重复是最坏的一种!”,要写出高质量的代码首先要解决的就是重复代码的问题.例如3次求阶乘: m = int(input( ...
- Appium+python自动化(三十一)- 元芳,你怎么看? - 日志收集-logging(超详解)
简介 生活中的日志是记录你生活的点点滴滴,让它把你内心的世界表露出来,更好的诠释自己的内心世界,而电脑里的日志是有价值的信息宝库. 日志文件是专门用于记录系统操作事件的记录文件或文件集合,操作系统有操 ...