【JZOJ6421】匹配
description

analysis
对于普通树形\(DP\)可以设\(f[i][0/1],g[i][0/1]\)表示\([1,i]\)的线段树的最大值、方案数
\(0\)表示不选择根与某个儿子相连,\(1\)表示选择根与某个儿子相连,由\({i\over 2},i-{i\over 2}\)转移得到
转移很不好想,这是\(70pts\)的方法,注意有很多节点信息是不需要知道的
一棵线段树最多只会有\(\log\)个不相同的节点,剩下的可以只搞一次得到
对于\(DP\)的优化,记忆化搜索加哈希就可以记下已经弄过的节点了
code
#pragma GCC optimize("O3")
#pragma G++ optimize("O3")
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define ha 9260817
#define mod 998244353
#define china 19491001
#define ll long long
#define reg register ll
#define fo(i,a,b) for (reg i=a;i<=b;++i)
#define fd(i,a,b) for (reg i=a;i>=b;--i)
using namespace std;
ll n,T,now;
struct node{ll x,f[2],g[2];}tmp,hash[ha+5];
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while (ch<'0' || '9'<ch){if (ch=='-')f=-1;ch=getchar();}
while ('0'<=ch && ch<='9')x=x*10+ch-'0',ch=getchar();
return x*f;
}
inline bool judge(ll x)
{
for (now=x%ha*china%ha;;now=now==ha?0:now+1)
{
if (!hash[now].x)return 0;
if (hash[now].x==x)return 1;
}
}
inline node dfs(ll x)
{
if (judge(x))return hash[now];
ll l=x/2,r=x-l;
node tmpp,tmpl=dfs(l),tmpr=dfs(r);
tmpp.x=x,tmpp.f[0]=tmpp.f[1]=tmpp.g[0]=tmpp.g[1]=0;
fo(j,0,1)fo(k,0,1)
{
tmpp.f[0]=max(tmpp.f[0],tmpl.f[j]+tmpr.f[k]);
if (j!=1 || k!=1)tmpp.f[1]=max(tmpp.f[1],tmpl.f[j]+tmpr.f[k]+1);
}
fo(j,0,1)fo(k,0,1)
{
if (tmpl.f[j]+tmpr.f[k]==tmpp.f[0])(tmpp.g[0]+=tmpl.g[j]*tmpr.g[k])%=mod;
if (j!=1 || k!=1)
{
ll cnt=(!j && !k?2:1)*tmpl.g[j]*tmpr.g[k]%mod;
if (tmpl.f[j]+tmpr.f[k]+1==tmpp.f[1])(tmpp.g[1]+=cnt)%=mod;
}
}
for (now=x%ha*china%ha;;now=now==ha?0:now+1)if (!hash[now].x){hash[now]=tmpp;return tmpp;}
}
int main()
{
freopen("match.in","r",stdin);
freopen("match.out","w",stdout);
T=read();
hash[china%ha].x=1,hash[china%ha].g[0]=1,hash[china%ha].g[1]=0;
hash[2*china%ha].x=2,hash[2*china%ha].f[1]=1,hash[2*china%ha].g[0]=1,hash[2*china%ha].g[1]=2;
while (T--)n=read(),tmp=dfs(n),printf("%lld %lld\n",tmp.f[1],tmp.f[0]==tmp.f[1]?(tmp.g[0]+tmp.g[1])%mod:tmp.g[1]);
return 0;
}
【JZOJ6421】匹配的更多相关文章
- javascript匹配各种括号书写是否正确
今天在codewars上做了一道题,如下 看上去就是验证三种括号各种嵌套是否正确书写,本来一头雾水,一种括号很容易判断, 但是三种怎么判断! 本人只是个前端菜鸟,,不会什么高深的正则之类的. 于是,在 ...
- scanf类型不匹配造成死循环
int i = 0; while (flag) { printf("please input a number >>> "); scanf("% ...
- 使用注解匹配Spring Aop切点表达式
Spring中的类基本都会标注解,所以使用注解匹配切点可以满足绝大部分需求 主要使用@within()/@target @annotaton() @args()等... 匹配@Service类中的所有 ...
- .net使用正则表达式校验、匹配字符工具类
开发程序离不开数据的校验,这里整理了一些数据的校验.匹配的方法: /// <summary> /// 字符(串)验证.匹配工具类 /// </summary> public c ...
- webpack配置别名alias出现的错误匹配
@(webpack) webpack是一款功能强大的前端构建工具,不仅仅是针对js,它也可通过各种loader来构建相关的less,html,image等各种资源,将webpack配合流程制定工具gu ...
- perl 如何匹配ASCII码以及ASCII码转换
匹配ASCII码: /[:ascii:]/ ASCII码转换为数字: ord() 数字转换为ASCII码: chr()
- SQL连接操作符介绍(循环嵌套, 哈希匹配和合并连接)
今天我将介绍在SQLServer 中的三种连接操作符类型,分别是:循环嵌套.哈希匹配和合并连接.主要对这三种连接的不同.复杂度用范例的形式一一介绍. 本文中使用了示例数据库AdventureWorks ...
- [LeetCode] Wildcard Matching 外卡匹配
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
随机推荐
- MySQL的一次优化记录 (IN子查询和索引优化)
这两天实习项目遇到一个网页加载巨慢的问题(10多秒),然后定位到是一个MySQL查询特别慢的语句引起的: SELECT * FROM ( SELECT DISTINCT t.vc_date, t.c_ ...
- 【Luogu】【关卡1-8】BOSS战-入门综合练习2(2017年10月)【AK】------都是基础题
P1426 小鱼会有危险吗 我个人觉得这个题目出的不好,没说明白,就先只粘贴的AC代码吧 #include <bits/stdc++.h> using namespace std; int ...
- Nodejs进阶:密码加盐
原理:就是在密码特定位置插入特定字符串后,再对修改后的字符串进行md5运算. demo var crypto=require("crypto"); function cryptPw ...
- Web UI开发神器—Kendo UI for jQuery数据管理之过滤操作
Kendo UI for jQuery最新试用版下载 Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support f ...
- 让微信小程序每次请求的时候不改变session_id的方法
让微信小程序每次请求的时候不改变session_id的方法 每次微信小程序请求的时候都会改变session id, 还好他的请求方法内可以设置header头 所以只需要在启动程序后第一次请求服务器获得 ...
- css自适应问题
1. 图片自适应 <img src="app-logo.png" srcset="app-logo.png 175w,app-logo-double.png 365 ...
- java中的final关键字的用法
一. 什么是final关键字? final在Java中是一个保留的关键字,可以声明成员变量.方法.类以及本地变量.一旦你将引用声明作final,你将不能改变这个引用了,编译器会检查代码,如果你试图将变 ...
- QT之QCustomPlot(一)
QDetectPlot::QDetectPlot(QWidget *parent) : QCustomPlot(parent) { QVector<), y(); // initialize w ...
- 数论+乱搞——cf181B
/* 2-type B|D^k 3-type B|D-1 11-type B|D+1 6-type B质因子分解, 7-type 其他情况 3-type: (a*(D^4-1)+b*(D^3-1)+. ...
- Python100天打卡-Day10
实现动画效果要实现动画效果,本身的原理也非常简单,就是将不连续的图片连续的播放,只要每秒钟达到了一定的帧数,那么就可以做出比较流畅的动画效果.import pygame def main(): # 初 ...