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. LeetCode Next Greater Element III

    原题链接在这里:https://leetcode.com/problems/next-greater-element-iii/description/ 题目: Given a positive 32- ...

  2. Django的CSRF机制

    原文链接:http://www.cnblogs.com/lins05/archive/2012/12/02/2797996.html 必须有的是: 1.每次初始化一个项目时,都能看到django.mi ...

  3. fn project 对象模型

    Applications At the root of everything are applications. In fn, an application is essentially a grou ...

  4. k8s api server ha 连接配置问题

    常见的lb 负载有硬件的f5 big-ip  ,同时对于互联网公司大家常用的是nginx  haproxy 了解k8s 集群高可用的都知道 api server  是无状态的(etcd 解决了),但是 ...

  5. maven环境配置详解,及maven项目的搭建及maven项目聚合

    首先:Maven 3.2.1:不同版本中仓库中文件是不一样的,Maven运行,先找用户配置,再找全局配置 1. Maven全局配置:全局统一的配置文件,在maven的安装目录中 2. Maven用户配 ...

  6. 在Linux 64位系统下使用hugepage

    首先,为什么要介绍/使用HugePage? 在步入正题之前,先讲一个非常普遍的数据库性能问题. 众所周知,Oracle数据库使用共享内存(SGA)来管理可以共享的一些资源;比如shared pool中 ...

  7. Spring Boot Oauth2

    Oauth2是描述无状态授权的协议(授权框架),因为是无状态,所以我们不需要维护客户端和服务器之间的会话. Oauth2的工作原理: 此协议允许第三方客户端代表资源所有者访问受保护资源,Oauth2有 ...

  8. 【转】 Pro Android学习笔记(九二):AsyncTask(1):AsyncTask类

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 在Handler的学习系列中,学习了如何h ...

  9. 【转】使用JMeter 完成常用的压力测试(二)

    使用JMeter 完成常用的压力测试 Login.jsp 和welcome.jsp.其中 login.jsp 负责生成 User 对象,并调用 User 的login.当 login 返回为 true ...

  10. git版本控制-详细操作

    - git,软件帮助使用者进行版本的管理 阶段一git 命令: git init 初始化 git config --global user.email "you@example.com&qu ...