传送门.

题解:


最主要的问题是如何判断一个数是否合法,这就需要发现性质了。

这个状态划分还是不太容易想到,

每次加的数\(∈[0,k)\),也就是个位一直在变变变,更高的位每次都是加一,这启发我们状态的划分。

这个时候可以利用数位dp的逐位确定思想,在尝试后,发现可以从高位到低位,因为当高位确定后,高位就不会变了,那么高位的最大值也就确定了。

设\(f[i][p][a]\),\(i\)含义如上,\(i+1\)位后的最大值是p,\(2-i\)位是0,当前个位是\(a\),使第\(i\)位加1后个位变成什么?

\(i=2\)时直接暴力处理,\(f[i]\)可以\(O(k)\)由\(f[i-1]\)推出来,复杂度\(O(n*k^3)\)。

有了f方便处理出\(g[i][p][x][a]\),\(i、p、a\)含义如上,x表示第i位要+x,

这里\(x=0\),g的值就是a,然后g自己推自己,复杂度\(O(n*k^3)\)。

接下来的部分就很傻逼了,带根联通块,直接在dfs序上dp,做到个位的时候,再跳跳看能不能跳到那个位去就好了。

Code:


#include<bits/stdc++.h>
#define fo(i, x, y) for(int i = x, B = y; i <= B; i ++)
#define ff(i, x, y) for(int i = x, B = y; i < B; i ++)
#define fd(i, x, y) for(int i = x, B = y; i >= B; i --)
#define ll long long
#define pp printf
#define hh pp("\n")
using namespace std; const int N = 505; int n, k;
int d[N], x, y;
vector<int> e[N];
#define pb push_back
#define si size() int p[N], q[N], np[N], p0, fa[N]; void dg(int x) {
p[x] = ++ p0; np[p0] = x;
ff(j, 0, e[x].si) {
int y = e[x][j];
if(fa[x] != y) fa[y] = x, dg(y);
}
q[x] = p0;
} const int mo = 1e9 + 7; int f[N][10][10], g[N][10][10][10];
ll h[N][N][10][10]; void add(ll &x, ll y) {
(x += y) >= mo ? x -= mo : 0;
} int T; int main() {
// freopen("buried.in", "r", stdin);
// freopen("buried.out", "w", stdout);
k = 10;
for(scanf("%d", &T); T; T --) {
fo(i, 1, n) e[i].clear();
memset(h, 0, sizeof h);
fo(i, 1, n) fa[i] = 0; p0 = 0;
scanf("%d", &n);
fo(i, 1, n - 1) {
scanf("%d %d", &x, &y);
e[x].pb(y); e[y].pb(x);
}
fo(i, 1, n) scanf("%d", &d[i]);
fo(i, 1, n) sort(e[i].begin(), e[i].end());
dg(1);
//Initialization of F
ff(p, 0, k) {
ff(a, 0, k) if(p || a) {
int x = a;
while(x < k) {
x += max(x, p);
}
f[1][p][a] = x % k;
}
}
//dp F
fo(i, 2, n - 1) {
ff(p, 0, k) {
ff(a, 0, k) {
int x = a;
fo(c, 1, k) x = f[i - 1][max(p, c - 1)][x];
f[i][p][a] = x;
}
}
}
//dp g
fo(i, 2, n) {
ff(p, 0, k) {
ff(a, 0, k) {
g[i][p][0][a] = a;
ff(x, 1, k) g[i][p][x][a] = f[i - 1][max(x - 1, p)][g[i][p][x - 1][a]];
}
}
}
//dp h
ll ans = 0;
fo(j, 1, n) h[1][j][0][1] = 1;
fo(i, 1, n) {
int num = d[np[i]];
int ni = q[np[i]] + 1;
fo(j, 2, n) {
ff(p, 0, k) ff(a, 0, k) if(h[i][j][p][a] && (p || a)) {
add(h[i + 1][j - 1][max(p, num)][g[j][p][num][a]], h[i][j][p][a]);
add(h[ni][j][p][a], h[i][j][p][a]);
}
}
ff(p, 0, k) ff(a, 0, k) if(h[i][1][p][a] && (p || a)) {
int x = a;
while(x < num) x += max(x, p);
if(x == num) add(ans, h[i][1][p][a]);
add(h[ni][1][p][a], h[i][1][p][a]);
}
}
pp("%lld\n", ans);
}
}

CodeChef Max-digit Tree(动态规划)的更多相关文章

  1. 【Codeforces715C&716E】Digit Tree 数学 + 点分治

    C. Digit Tree time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ...

  2. Codeforces 716 E Digit Tree

    E. Digit Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...

  3. 【题解】Digit Tree

    [题解]Digit Tree CodeForces - 716E 呵呵以为是数据结构题然后是淀粉质还行... 题目就是给你一颗有边权的树,问你有多少路径,把路径上的数字顺次写出来,是\(m\)的倍数. ...

  4. Codechef Union on Tree

    Codechef Union on Tree https://www.codechef.com/problems/BTREE 简要题意: 给你一棵树,\(Q\)次询问,每次给出一个点集和每个点的\(r ...

  5. 【Codeforces 715C】Digit Tree(点分治)

    Description 程序员 ZS 有一棵树,它可以表示为 \(n\) 个顶点的无向连通图,顶点编号从 \(0\) 到 \(n-1\),它们之间有 \(n-1\) 条边.每条边上都有一个非零的数字. ...

  6. Codechef Observing the Tree

    Home » Practice(Hard) » Observing the Tree   https://www.codechef.com/problems/QUERY Observing the T ...

  7. HDU 1003 Max Sum【动态规划求最大子序列和详解 】

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  8. CF715C:Digit Tree

    传送门 一句话怎么说来着 算法+高级数据结构=OI 现在我感觉到的是 我会的算法+我会的高级数据结构=WA 这道题提交了三四十次,从刚看题到完全写好花了好几天..,主要死于看错费马小定理的适用条件. ...

  9. CF 716E. Digit Tree [点分治]

    题意:一棵树,边上有一个个位数字,走一条路径会得到一个数字,求有多少路径得到的数字可以整除\(P\) 路径统计一般就是点分治了 \[ a*10^{deep} + b \ \equiv \pmod P\ ...

  10. hdu 1003 Max Sum (动态规划)

    转载于acm之家http://www.acmerblog.com/hdu-1003-Max-Sum-1258.html Max Sum Time Limit: 2000/1000 MS (Java/O ...

随机推荐

  1. goroutine 分析 协程的调度和执行顺序 并发写

    package main import ( "fmt" "runtime" "sync" ) const N = 26 func main( ...

  2. ASP 解析json

    第一个方法是使用 JScript : <script language="jscript" runat="server"> Array.protot ...

  3. 003-js-MD5

    源码 /* global define */ ;(function ($) { 'use strict' /* * Add integers, wrapping at 2^32. This uses ...

  4. python读写ini配置文件

    像邮箱等信息是可以写在配置文件里面的,python有一个配置模块ConfigParser,可以处理配置文件信息 目录 1.配置模块ConfigParser 2.基本应用 1.配置模块ConfigPar ...

  5. rac的一次问题 ORA-01565: error in identifying file '+DATA/bol/spfilebol.ora'

    昨天安装的测试环境的rac--2节点 CentOS release 6.8 (Final) SQL*Plus: Release 11.2.0.4.0 Production 今天测试突然出现问题 在ra ...

  6. hive重要命令

    hadoop dfsadmin -safemode leave hadoop退出安全模式让提示符显示当前库: set hive.cli.print.current.db=true;显示查询结果时显示字 ...

  7. Dynamic Programming and Policy Evaluation

    Dynamic Programming divides the original problem into subproblems, and then complete the whole task ...

  8. 000 (H5*) 知识点总结

    https://note.youdao.com/ynoteshare1/index.html?id=ff02e616917fba868f39241c8383d7c7&type=note 目录 ...

  9. 实验报告一&第三周学习总结

    一.实验报告 1.打印输出所有的"水仙花数",所谓"水仙花数"是指一个3位数,其中各位数字立方和等于该数本身.例如,153是一个"水仙花数" ...

  10. [Markdown] 02 简单应用 第二弹

    目录 4. 插入链接 4.1 Markdown 的方式 用法 1 用法 2 4.2 HTML5 的方法 用法 1 用法 2 5. 插入图片 5.1 使用网络地址 5.2 使用本地地址 5.2.1 小括 ...