洛谷 Codeforces 这题真是妙的很. 通过看题解,终于知道了\(\sum_n f(n)^k​\)这种东西怎么算. update:经过思考,我对这题有了更深的理解,现将更新内容放在原题解下方. 思路 发现\(\sum_S (f(S))^k\)这东西因为有个\(k\)次方,所以特别难算,考虑拆开: \[ x^k=\sum_{i=0}^k {x \choose i} \times i! \times S(k,i) \] 其中\(S(n,m)\)是第二类斯特林数,即\(n\)个元素放进\(m​\…
Codeforces 题目传送门 & 洛谷题目传送门 首先看到这题我的第一反应是:这题跟这题长得好像,不管三七二十一先把 \(k\) 次方展开成斯特林数的形式,\(f(X)^k=\sum\limits_{i=0}^k\begin{Bmatrix}k\\i\end{Bmatrix}\dbinom{f(X)}{i}\),于是我们只需对所有 \(i\in[0,k]\) 求出 \(\sum\dbinom{f(X)}{i}\) 即可. 然后就不会做了/dk/wq 考虑 \(\dbinom{f(X)}{i}…
题目简述:给定$n \leq 10^5$个节点的树$T = (V, E)$,令$X \subseteq V$表示一个非空节点集合,定义$f(X)$为包含$X$的最小子树的边数.求 $$ \sum_{\emptyset \neq X \subseteq V} (f(X))^k, $$ 其中$k \leq 200$. 解:code 问题转化 我们额外定义$f(\emptyset) = 0$,就不需再单独考虑空集. 利用斯特林数的性质,我们有 $$ x^n = \sum_{k=0}^n k! \beg…
传送门 分析 https://blog.csdn.net/forever_shi/article/details/88048528 代码 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<cctype> #include<cmath> #include<cstdlib&…
题目链接 一道好题. 题意:给定一棵\(n\)个点的树,求: \[\sum_{S\subseteq \{1,2,\dots,n\}}f(S)^k\] 其中\(f(S)\)代表用树边将点集\(S\)连通需要的最少边数.\(n\leq10^5\),\(k\leq 200\),对\(10^9+7\)取模. 吐槽:比赛的时候看到这道题想到的是组合数做法,然而是\(O(nk^2)\)的,但可以用任意模数\(\text{NTT}\)优化到\(O(nk\log k)\),当然由于常数巨大结果可想而知.可以说是…
传送门 题目大意 一棵$n$个点的树,一个点集$S$的权值定义为把这个点击连成一个联通块的最少边数,求: $$ans=\sum_{S\in U}f(S)^k$$ 题解 这题跟gdoi那道题差不多 先把柿子化一下变成 $$ans=\sum_{i=0}^k \begin{Bmatrix}k\\i\end{Bmatrix} i! \sum_{S\in U}\begin{pmatrix}f(S)\\i\end{pmatrix}$$ 然后我们就相当于去统计大小为$i$的边集的贡献 这个可以通过dp来实现…
根本想不到 CF1097G 题意 给出一棵树,定义f(S)为用最少的边连通点集$ S$的边数 求$ \sum\limits f(S)^k$ $ n \leq 10^5 k \leq 200$ 题解 假设$ k=1$有一个清真的树形$ DP$ 在点集的$ LCA$处统计答案即可 对于$ k>1$根据二项式定理 可以$ O(nk^2)$完成转移 但这是过不去的 考虑 $$ x^k=\sum_{i=0}^k \binom{x}{i}S(k,i)i!$$ 其中$ S(i,j)$表示第二类斯特林数 拆开组…
传送门 看到\(k\)次幂求和先用斯特林数拆幂:\(x^k = \sum\limits_{i=1}^k \binom{x}{i}\left\{ \begin{array}{cccc} k \\ i \end{array} \right\}i!\). 那么原式等于\(\sum\limits_{X} \sum\limits_{i=1}^k \binom{f(X)}{i}\left\{ \begin{array}{cccc} k \\ i \end{array} \right\}i! = \sum\l…
传送门:QAQQAQ 题意:有一个数组a和一个数组k,数组a一直保持一个性质:a[i + 1] >= a[i] + k[i].有两种操作:1,给某个元素加上x,但是加上之后要保持数组a的性质.比如a[i]加上x之后,a[i + 1]<a[i] + k[i],那么a[i + 1]就变成a[i] + k[i],否则不变.同理,若a[i + 2]小于了现在的a[i + 1] + k[i + 1],那么a[i + 2]也变成a[i + 1] + k[i + 1],一直保持这个性质.第二章操作,询问数组…
基本定义 第一类斯特林数:$1 \dots n$的排列中恰好有$k$个环的个数:或是,$n$元置换可分解为$k$个独立的轮换的个数.记作 $$ \begin{bmatrix} n \\ k \end{bmatrix}. $$ 第二类斯特林数:将$n$个元素分成$k$个非空集合的方案数.记作 $$ \begin{Bmatrix} n \\ k \end{Bmatrix}. $$ 根据定义,我们有 $$ \sum_{k=0}^n \begin{bmatrix} n \\ k \end{bmatrix…
目录 Codeforces 1097 D.Makoto and a Blackboard(DP 期望) E.Egor and an RPG game(思路 LIS Dilworth定理) F.Alex and a TV Show(bitset) G.Vladislav and a Great Legend(斯特林数 树形DP) H. Codeforces 1097 比赛链接 咕咕了一个月...终于闲的没事想起补了... ABC代码没在本地(而且懒),就不放了... (然而当时C题FST了真是想.…
动态规划 \(dp\)早就已经是经常用到的算法了,于是老师上课主要都在讲题.今天讲的主要是三类\(dp\):树形\(dp\),计数\(dp\),\(dp\)套\(dp\).其中计数\(dp\)是我很不熟的,\(dp\)套\(dp\)是我没接触过,树形\(dp\)难的题我也不是很会做,所以感觉还是收获了不少,于是\(dp\)的总结将主要会以题解的形式呈现. 重要例题及简要题解 \(Gcd\ counting\):设\(f_{u,v}\)代表以\(u\)为根的子树中,点权都能被\(v\)整除的最长链…
C. Sonya and Problem Wihtout a Legend 题目连接: http://codeforces.com/contest/713/problem/C Description Sonya was unable to think of a story for this problem, so here comes the formal description. You are given the array containing n positive integers. A…
E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output Sonya was unable to think of a story for this problem, so here comes the formal description. You are g…
题目链接: C. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output Sonya was unable to think of a story for this problem, so here comes the formal description. You…
[题目链接] http://codeforces.com/problemset/problem/713/C [题目大意] 给出一个数列,请你经过调整使得其成为严格单调递增的数列,调整就是给某些位置加上或者减去某个数,调整的代价是加上或者减去的数的绝对值之和,请你输出最小代价. [题解] 先考虑这样一个问题,如果是非严格单调递增该如何做,我们会发现每次调整,都是调整某个数字为原先数列中存在的数字,最后才是最优的,所以,我们设DP[i][j]表示前i个数字,最后一个数为原先数列排序后第j大的数字的最…
题目链接: http://codeforces.com/contest/1136/problem/E 题意: 初始有a数组和k数组 有两种操作,一,求l到r的区间和,二,$a_i\pm x$ 并且会有一个连锁反应 $$while\left ( a_{i+1}<a_i+k_i \right )a_{i+1}=a_i+k_i,i++ $$ 数据范围: $2 \leq n \leq 10^{5}$$-10^{9} \leq a_i \leq 10^{9}$$-10^{6} \leq k_i \leq…
题目链接:https://codeforces.com/problemset/problem/1136/E 题意: 给出一个 $a[1 \sim n]$,以及一个 $k[1 \sim (n-1)]$,初始保证所有的 $1 \le i \le n-1$ 都满足 $a[i]+k[i] \le a[i+1]$. 现在有两种操作: 第一种是令指定的 $a[i]$ 加上一个非负整数 $x$,此时若有 $a[i] + k[i] > a[i+1]$,则 $a[i+1]$ 变为 $a[i] + k[i]$,往后…
[题目]C. Sonya and Problem Wihtout a Legend [题意]给定n个数字,每次操作可以对一个数字±1,求最少操作次数使数列递增.n<=10^5. [算法]动态规划+前缀和优化 [题解]★令b[i]=a[i]-i,则a[i]递增等价于b[i]不递减. 这样做之后,显然数字加减只能到b[i]中出现的数字,而不会出现其它数字. 令f[i][j]表示前i个数,第i个数字大小为c[j](第j大的数字)的最少操作次数. f[i][j]=abs(b[i]-c[j])+min{f…
C - Sonya and Problem Wihtout a Legend 思路:感觉没有做过这种套路题完全不会啊.. 把严格单调递增转换成非严格单调递增,所有可能出现的数字就变成了原数组出现过的数字. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PII pair<int, int> #define PLI…
C. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output Sonya was unable to think of a story for this problem, so here comes the formal description. You are g…
E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output Sonya was unable to think of a story for this problem, so here comes the formal description. You are g…
题目链接   Sonya and Problem Wihtout a Legend 题意  给定一个长度为n的序列,你可以对每个元素进行$+1$或$-1$的操作,每次操作代价为$1$. 求把原序列变成严格递增子序列的所需最小花费. 考虑$DP$. 首先比较常见的套路就是把每个$a[i]$减去$i$,然后把这个新的序列升序排序,记为$b[i]$. 这里有个结论:最后操作完成之后的每个数都是$b[i]$中的某个数. 然后就可以$DP$了,令$f[i][j]$为前$i$个数操作之后每个数都小于等于$b…
题目链接:http://codeforces.com/contest/713/problem/C 题解:这题也算是挺经典的题目了,这里附上3种解法优化程度层层递进,还有这里a[i]-i<=a[i+1]-(i+1),处理一下. 首先是最基础的dp[i][j]前i位最大值为j的最小值为多少. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include…
题意:给一个序列,可以进行若干次操作,每次操作选择一个数让它+1或-1,问最少要几次操作使得序列变为严格单调递增序列. 题解:首先考虑非严格递增序列,则每个数字最终变成的数字一定是该数组中的某个数.那么O(n^2)复杂度dp一下即可. dp[i][j]表示第i个数变成第j小的数,dp[i][j] = min (dp[i-1][1 ... j])+abs(a[i]-b[j]). 那么对于严格递增序列,将a[i]变成a[i]-i后,再照非严格递增序列跑一遍dp即可. #include<bits/st…
Description Sonya was unable to think of a story for this problem, so here comes the formal description. You are given the array containing \(n\) positive integers. At one turn you can pick any element and increase or decrease it by \(1\). The goal i…
vp的时候没码出来.. 我们用set去维护, 每一块区域, 每块区域内的元素与下一个元素的差值刚好为ki,每次加值的时候我们暴力合并, 可以发现我们最多合并O(n)次. 然后写个线段树就没了. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<…
大意: 给定序列, 每次操作可以任选一个数+1/-1, 求最少操作数使序列严格递增. 序列全-i后转化为求最少操作数使序列非降, 那么贪心可以知道最后$a_i$一定是修改为某一个$a_j$了, 暴力dp即可. #include <iostream> #include <cstdio> #define REP(i,a,n) for(int i=a;i<=n;++i) using namespace std; typedef long long ll; const int N =…
这题是一个贼搞人的线段树 线段树维护的是 区间和a[i - j] 首先对于update的位置可以二分查找 其次update时候的lazy比较技巧 比如更新的是 l-r段,增加的是c 那么这段的值为: a[l] + c, a[l + 1] + k[l] + c, .... a[r] + k[l] + .. + k[r-1] + c lazy 记录的是 a[l] + c - (k[1] + ... + k[l - 1]) 每次pushdown的时候 a[i]_new = lazy + k_prefi…
题意:有一个数组a和一个数组k,数组a一直保持一个性质:a[i + 1] >= a[i] + k[i].有两种操作:1,给某个元素加上x,但是加上之后要保持数组a的性质.比如a[i]加上x之后,a[i + 1]<a[i] + k[i],那么a[i + 1]就变成a[i] + k[i],否则不变.同理,若a[i + 2]小于了现在的a[i + 1] + k[i + 1],那么a[i + 2]也变成a[i + 1] + k[i + 1],一直保持这个性质.第二章操作,询问数组a的区间[l, r]的…