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】匹配的更多相关文章

  1. javascript匹配各种括号书写是否正确

    今天在codewars上做了一道题,如下 看上去就是验证三种括号各种嵌套是否正确书写,本来一头雾水,一种括号很容易判断, 但是三种怎么判断! 本人只是个前端菜鸟,,不会什么高深的正则之类的. 于是,在 ...

  2. scanf类型不匹配造成死循环

        int i = 0; while (flag) { printf("please input a number >>> "); scanf("% ...

  3. 使用注解匹配Spring Aop切点表达式

    Spring中的类基本都会标注解,所以使用注解匹配切点可以满足绝大部分需求 主要使用@within()/@target @annotaton() @args()等... 匹配@Service类中的所有 ...

  4. .net使用正则表达式校验、匹配字符工具类

    开发程序离不开数据的校验,这里整理了一些数据的校验.匹配的方法: /// <summary> /// 字符(串)验证.匹配工具类 /// </summary> public c ...

  5. webpack配置别名alias出现的错误匹配

    @(webpack) webpack是一款功能强大的前端构建工具,不仅仅是针对js,它也可通过各种loader来构建相关的less,html,image等各种资源,将webpack配合流程制定工具gu ...

  6. perl 如何匹配ASCII码以及ASCII码转换

    匹配ASCII码:   /[:ascii:]/ ASCII码转换为数字: ord() 数字转换为ASCII码: chr()

  7. SQL连接操作符介绍(循环嵌套, 哈希匹配和合并连接)

    今天我将介绍在SQLServer 中的三种连接操作符类型,分别是:循环嵌套.哈希匹配和合并连接.主要对这三种连接的不同.复杂度用范例的形式一一介绍. 本文中使用了示例数据库AdventureWorks ...

  8. [LeetCode] Wildcard Matching 外卡匹配

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  9. [LeetCode] Regular Expression Matching 正则表达式匹配

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

随机推荐

  1. JNI Hello World

    1.什么是JNI:               JNI(Java Native Interface):java本地开发接口               JNI是一个协议,这个协议用来沟通java代码和 ...

  2. 总结下awk基本用法

    命令格式: awk '{commands} [{other commands}]' awk 'condition{commands} [{other commands}]' 如:awk '$4==&q ...

  3. python 正则匹配

    正则表达式模式 模式字符串使用特殊的语法来表示一个正则表达式: 字母和数字表示他们自身.一个正则表达式模式中的字母和数字匹配同样的字符串. 多数字母和数字前加一个反斜杠时会拥有不同的含义. 标点符号只 ...

  4. 让APK只包含指定的ABI(转)

    转自:http://blog.csdn.net/justfwd/article/details/49308199 现在很多android第三方 sdk是以aar形式提供的,甚至是远程aar,如果这个s ...

  5. sql 合并查询结果

    在使用js报表工具的时候,常常需要提供json数据进行显示, 在sql查询的时候就需要构造合适的查询结果出来; 就用到了合并两个没有关联关系的表数据, SELECT SUM(a1.amount) AS ...

  6. python网络爬虫学习

    网络爬虫 Requests官方中文教程地址:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html Beautiful So ...

  7. 强制位(set uid和set gid)与冒险位(sticky)的作用都有什么

    1.set uid(强制位) 只能对文件进行设置 通过对文件设置uid可以使非文件拥有者或文件所属群组的用户具有执行该文件的权限. 例如ping,默认情况下所有用户都可以使用此命令,但当我们查看pin ...

  8. js文字转语音(speechSynthesis)

    环境: windows 官网网址: https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesis 基础使用: var msg = n ...

  9. 使用gulp管理sass文件

    前提是npm和ruby已经安装好 1. 新建文件夹myproject,cd进入文件夹 再npm init 初始化 2.npm install gulp --save-dev 为项目添加gulp,并将g ...

  10. Cell的复用机制问题总结

    创建方式汇总,注册和不注册Cell注册的两种方式 1.tableView registerNib:(nullable UINib *) forCellReuseIdentifier:(nonnull ...