HDU 5845 Best Division
$dp$,字典树。
$dp$递推式很容易知道。dp[i]=max{dp[j]+1} a[j]^..^a[i]<=X,并且$[j,i]$长度不能超过$L$。
但是暴力来复杂度极高,所以需要用字典树维护这个东西。将前缀异或和插入到字典树中,然后不断维护$a[i]$位置之前$L$个前缀异或和就好了。
跑了$405ms$,第一次排到第一,有点激动~~~
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar(); x = ;while(!isdigit(c)) c = getchar();
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
} const LL mod=(LL);
const int maxn=;
LL a[maxn],X,P,Q;
int T,n,L,dp[maxn],h;
struct Node { int cnt,mx,nx[]; }s[*maxn];
int sz; bool fail,f[maxn]; int add()
{
s[sz].mx=; s[sz].cnt=;
s[sz].nx[]=s[sz].nx[]=-; sz++;
return sz-;
} void Delete(LL num)
{
int p=;
for(int i=;i>=;i--)
{
int x; LL g=(LL)(<<i); if(num&g) x=; else x=;
int t=p; p=s[p].nx[x]; s[p].cnt--;
if(s[p].cnt==) { s[t].nx[x]=-; return; }
}
} void Insert(LL num,int val)
{
int p=;
for(int i=;i>=;i--)
{
int x; LL g=(LL)(<<i); if(num&g) x=; else x=;
if(s[p].nx[x]==-) s[p].nx[x]=add();
p=s[p].nx[x]; s[p].cnt++; s[p].mx=max(s[p].mx,val);
}
} void Find(LL num)
{
int p=;
for(int i=;i>=;i--)
{
int x; LL g=(LL)(<<i); if(num&g) x=; else x=;
int y; if(X&g) y=; else y=; if(y==)
{
p=s[p].nx[x]; if(p==-) return;
if(i==) h=max(h,s[p].mx),fail=;
}
else
{
if(s[p].nx[x]!=-) h=max(h,s[s[p].nx[x]].mx),fail=;
p=s[p].nx[x^]; if(p==-) return;
if(i==) h=max(h,s[p].mx),fail=;
}
}
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%lld%d",&n,&X,&L);
scanf("%lld%lld%lld",&a[],&P,&Q);
for(int i=;i<=n;i++) a[i]=(a[i-]*P%mod+Q)%mod; sz=; s[sz].cnt=; s[sz].nx[]=s[sz].nx[]=-; sz++; for(int i=;i<=n;i++) f[i]=;
for(int i=;i<=n;i++)
{
if(i-L->=&&f[i-L-] ) Delete(a[i-L-]);
dp[i]=; a[i]=a[i]^a[i-]; if(a[i]<=X) dp[i]=;
h=; fail=; Find(a[i]); if(fail==) dp[i]=h+;
if(dp[i]!=) { Insert(a[i],dp[i]); f[i]=; }
} printf("%d\n",dp[n]);
}
return ;
}
HDU 5845 Best Division的更多相关文章
- 【HDU - 5845】Best Division(xor-trie、01字典树、dp)
BUPT2017 wintertraining(15) #7E 题意 把数组A划分为k个区间,每个区间不超过L长度,每一个区间异或和之和为S.现在求:S不超过X,区间个数的最大值. 且A是这样给你的: ...
- 【HDU】3480 Division
http://acm.hdu.edu.cn/showproblem.php?pid=3480 题意:一个n个元素的集合S要求分成m个子集且子集并为S,要求$\sum_{S_i} (MAX-MIN)^2 ...
- 【HDU 6036】Division Game (NTT+数学)
多校1 1004 HDU-6036 Division Game 题意 有k堆石头(0~k-1),每堆n个.\(n=\prod_{i=0}^{m}p_i^{e_i}\).\(0\le m,k \le 1 ...
- hdu 3718 Different Division
Different Division Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 3480 Division(斜率优化DP)
题目链接:hdu 3480 Division 题意: 给你一个有n个数的集合S,现在让你选出m个子集合,使这m个子集合并起来为S,并且每个集合的(max-min)2 之和要最小. 题解: 运用贪心的思 ...
- HDU 6036 - Division Game | 2017 Multi-University Training Contest 1
/* HDU 6036 - Division Game [ 组合数学,NTT ] | 2017 Multi-University Training Contest 1 题意: k堆石子围成一个圈,数量 ...
- HDU 6036 Division Game
HDU 6036 Division Game 考虑每堆石头最多操作 $ \sum e $ 次,考虑设 $ f(x) $ 表示某一堆石头(最开始都是一样的)操作 $ x $ 次后变成了 $ 1 $ 的方 ...
- hdu 1034 (preprocess optimization, property of division to avoid if, decreasing order process) 分类: hdoj 2015-06-16 13:32 39人阅读 评论(0) 收藏
IMO, version 1 better than version 2, version 2 better than version 3. make some preprocess to make ...
- HDU 3480 Division(斜率优化+二维DP)
Division Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 999999/400000 K (Java/Others) Tota ...
随机推荐
- floor()函数 向下取整 ceil()函数向上取整
floor(x) is the largest integer not greater than x , 也就是,floor(x) 返回的是小于等于x的所有整数中最大的整数,简单的说,就是去掉x的小 ...
- scrapy在ubuntu上安装总结
此文档是本人学习时使用的,采用一个实例作为引导进行安装测试. 实例下载地址如下: https://github.com/sans-serif/scrapy-german-news#introducti ...
- composer install 遇到问题 Problem 1 - phpunit/phpunit 5.7.5 requires php ^5.6 || ^7.0 -> your PHP version (5.5.3 0) does not satisfy that requirement.
$ composer install Loading composer repositories with package information Updating dependencies (inc ...
- KMS服务器搭建
- GourdScan & sqlmapapi
0x01 Windows下配置GourdScan 0x0101 GourdScan项目地址:https://github.com/code-scan/GourdScan PHP环境 + ...
- NTSC色域(CIE1931)计算公式
色域(CIE1931)=ABS(RC[-6]*RC[-3]+RC[-4]*RC[-1]+RC[-2]*RC[-5]-RC[-6]*RC[-1]-RC[-4]*RC[-5]-RC[-2]*RC[-3]) ...
- UVA11549 计算机谜题(Floyd判圈算法)
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #inclu ...
- C#数组与集合
- [HMLY]1.CocoaPods详解----使用
作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/18737437 转载请注明出处 一.什么是cocoaPods 1.为 ...
- css基础和心得(二)
css中的某些样式是具有继承性的.它允许样式不仅应用于某个特定html标签元素 而且应用于其后代.如: p{color:red;} <p>dsffd<spans>sdfasd ...