BZOJ 3456 权限题

太菜了推不出式子

我们设$f(n)$表示$n$个点的无向连通图的数量,那么有

$$f(n) = 2^{\binom{n}{2}} - \sum_{i = 1}^{n - 1}\binom{n - 1}{i - 1}f(i)2^{\binom{n - i}{2}}$$

思路就是全部减去不合法的,枚举$1$号点所在的联通块的大小,剩下随便生成一张无向图。

拆开组合数,简单变形一下

$$f(n) = 2^{\binom{n}{2}} - (n - 1)!\sum_{i = 1}^{n - 1}\frac{2^{\binom{n - i}{2}}}{(n - i)!}\frac{f(i)}{(i - 1)!}$$

记$h(i) = \frac{f(i)}{(i - 1)!}$,$g(i) = \frac{2^{\binom{i}{2}}}{i!}$

$$(n - 1)!h(n) = 2^{\binom{n}{2}} - (n - 1)!\sum_{i = 1}^{n - 1}g(i)h(n - i)$$

发现这个式子可以用分治FFT优化,直接上就做完了。

时间复杂度$O(nlog^2n)$。

Code:

#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll; const int N = 3e5 + ;
const ll P = 1004535809LL; int n, lim, pos[N];
ll f[N], g[N], fac[N], inv[N], a[N], b[N]; template <typename T>
inline void read(T &X) {
X = ; char ch = ; T op = ;
for (; ch > ''|| ch < ''; ch = getchar())
if (ch == '-') op = -;
for (; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline ll fpow(ll x, ll y) {
ll res = ;
for (; y > ; y >>= ) {
if (y & ) res = res * x % P;
x = x * x % P;
}
return res;
} template <typename T>
inline void swap(T &x, T &y) {
T t = x; x = y; y = t;
} inline void prework(int len) {
int l = ;
for (lim = ; lim <= len; lim <<= , ++l);
for (int i = ; i < lim; i++)
pos[i] = (pos[i >> ] >> ) | ((i & ) << (l - ));
} inline void ntt(ll *c, int opt) {
for (int i = ; i < lim; i++)
if (i < pos[i]) swap(c[i], c[pos[i]]);
for (int i = ; i < lim; i <<= ) {
ll wn = fpow(, (P - ) / (i << ));
if (opt == -) wn = fpow(wn, P - );
for (int len = i << , j = ; j < lim; j += len) {
ll w = ;
for (int k = ; k < i; k++, w = w * wn % P) {
ll x = c[j + k], y = w * c[j + k + i] % P;
c[j + k] = (x + y) % P, c[j + k + i] = (x - y + P) % P;
}
}
} if (opt == -) {
ll invP = fpow(lim, P - );
for (int i = ; i < lim; i++) c[i] = c[i] * invP % P;
}
} inline ll getC(int x) {
if (x < ) return ;
return (1LL * x * (x - ) / );
} void solve(int l, int r) {
if (l == r) {
f[l] = (fpow(, getC(l) % (P - )) - f[l] * fac[l - ] % P + P) % P;
f[l] = f[l] * inv[l - ] % P;
return;
} int mid = ((l + r) >> );
solve(l, mid); prework(r - l + );
for (int i = ; i < lim; i++) a[i] = b[i] = 0LL;
for (int i = l; i <= mid; i++) a[i - l] = f[i];
for (int i = ; i <= r - l; i++) b[i - ] = g[i];
ntt(a, ), ntt(b, );
for (int i = ; i < lim; i++) a[i] = a[i] * b[i] % P;
ntt(a, -); for (int i = mid + ; i <= r; i++)
f[i] = (f[i] + a[i - l - ] % P) % P; solve(mid + , r);
} int main() {
read(n); fac[] = 1LL;
for (int i = ; i <= n; i++) fac[i] = fac[i - ] * i % P;
inv[n] = fpow(fac[n], P - );
for (int i = n - ; i >= ; i--) inv[i] = inv[i + ] * (i + ) % P; for (int i = ; i <= n; i++)
g[i] = fpow(2LL, getC(i) % (P - )) * inv[i] % P; solve(, n); printf("%lld\n", f[n] * fac[n - ] % P);
return ;
}

Luogu 4841 城市规划的更多相关文章

  1. luogu 2478 [SDOI2010]城市规划 仙人掌上dp.

    LINK:城市规划 以前ls 让写的时候由于看不懂题目+以为在图中的环上dp非常困难所以放弃治疗了. 现在终于能把题目看懂了 泪目... 题目其实就是在说 给出一张图这个有一个非常好的性质 满足每个点 ...

  2. Luogu 魔法学院杯-第二弹(萌新的第一法blog)

    虽然有点久远  还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题  沉迷游戏,伤感情 #include <queue> ...

  3. luogu p1268 树的重量——构造,真正考验编程能力

    题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...

  4. 浅谈城市规划在移动GIS方面的应用发展

    1.概述 城市建设进程加快,城市规划管理工作日趋繁重,各种来源的数据产生各种层出不穷的问题,严重影响城市规划时的准确性,为此全面合理的掌握好各方面的城市规划资料才能做出更加科学的决策.移动端的兴起为规 ...

  5. 【BZOJ-1952】城市规划 [坑题] 仙人掌DP + 最大点权独立集(改)

    1952: [Sdoi2010]城市规划 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 73  Solved: 23[Submit][Status][ ...

  6. [luogu P2170] 选学霸(并查集+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...

  7. [luogu P2647] 最大收益(贪心+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2647 题目描述 现在你面前有n个物品,编号分别为1,2,3,--,n.你可以在这当中任意选择任意多个物品. ...

  8. Luogu 考前模拟Round. 1

    A.情书 题目:http://www.luogu.org/problem/show?pid=2264 赛中:sb题,直接暴力匹配就行了,注意一下读入和最后一句话的分句 赛后:卧槽 怎么只有40 B.小 ...

  9. luogu P2580 于是他错误的点名开始了

    luogu  P2580 于是他错误的点名开始了 https://www.luogu.org/problem/show?pid=2580 题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边 ...

随机推荐

  1. 解决direct2d拖拽窗口闪烁

    响应WM_ERASEBKGND,在OnEraseBkgnd()处返回FALSE,阻止GDI重绘客户区背景色,设置背景色的工作交给Direct2D在Render时设置,否则在Resize时会出现窗口闪烁 ...

  2. Android三种左右滑动效果 手势识别(转)

    本示例演示在Android中实现带渐显按钮的左右滑动效果. 关于滑动效果,在我的上一篇博文中提到过,有兴趣的朋友可以访问: http://www.cnblogs.com/hanyonglu/archi ...

  3. DHCP(三)

    选择阶段:即DHCP客户端选择IP地址的阶段.如果有多台DHCP服务器向该客户端发来DHCP Offer报文,客户端只接受第一个收到的DHCP Offer报文,然后以广播方式发送DHCP Reques ...

  4. awk常见基本使用

    -F 指定分割符号 print 外层的引号必须是单引号 $n不能被解析 [root@bogon ~]# .txt a:b:c:d a1:b1:c1:d1 a2:b2:c2:d2 a_: :c:dddd ...

  5. linux启动自动挂载分区和/etc/fstab简单修复

    让后加的分区能够启动时自动挂载,需要把配置写入文件 /etc/fstab vi /etc/fstab UUID=3f5859e0-592f-42cd-b533-570422fb85be   / ext ...

  6. .net中webconfig自定义配置

    在configuration节点,也就是文件的根节点下,增加如下节点 <appSettings> <!--<add key="propPath" value ...

  7. HTMLTestRunner生成报告 中文展示乱码的问题

  8. TCP之四:TCP 滑动窗口协议 详解

    滑动窗口机制 滑动窗口协议的基本原理就是在任意时刻,发送方都维持了一个连续的允许发送的帧的序号,称为发送窗口:同时,接收方也维持了一个连续的允许接收的帧的序号,称为接收窗口.发送窗口和接收窗口的序号的 ...

  9. 第15届浙江省赛 E LIS

    LIS Time Limit: 1 Second      Memory Limit: 65536 KB      Special Judge DreamGrid is learning the LI ...

  10. mysql 内存体系结构--session

    体系结构tmp_table_size & max_heap_table_size ,这个是会话级的内存结构mysql使用总内存 = global_buffers + thread_buffer ...