题目链接

思路

这个题可以考虑用全部情况减去不合法的情况,来求解。首先需要知道n个点所组成的图总共有\(C(_n^2)\)种,然后用f[n]表示n个点的图联通的方案数。

然后钦定1在联通图里面,考虑不合法的情况。让j个点联通,其他点可以任意连边,这样就可以保证这张图是不连通的。

所以f数组的转移就是

\[f[n]=C(_n^2) - \sum\limits_{i = 1}^{n-1}{f[i] * C(_{n-i}^2)*C(_{n-1}^{i-1})}
\]

这是n方的转移,然后可以用**T优化。然后我不会

O(n^2)代码

#include<cstdio>
#include<iostream>
#define fi(s) freopen(s,"r",stdin);
#define fo(s) freopen(s,"w",stdout);
using namespace std;
typedef long long ll;
const int N = 5000 + 10,mod = 998244353;
ll C[N][N];
ll read() {
ll x = 0,f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
void pre() {
C[0][0] = 1;
for(int i = 1;i <= N;++i) {
C[i][0] = 1;
for(int j = 1;j <= i;++j) {
C[i][j] = C[i-1][j] + C[i-1][j-1];
C[i][j] >= mod ? C[i][j] -= mod : 0;
}
}
}
ll f[N];
ll qm(ll x,int y) {
ll ans = 1;
for(;y;y >>= 1,x = x * x % mod)
if(y & 1) ans = ans * x,ans %= mod;
return ans;
}
int main() {
int n = read();
pre();
f[1] = f[2] = 1;
for(int i = 3;i <= n;++i) {
f[i] = qm(2,C[i][2]);
for(int j = 1;j < i;++j) {
f[i] = (f[i] - (f[j] * C[i-1][j-1] %mod * qm(2,C[i-j][2]) % mod) + mod) % mod;
}
}
cout<<f[n];
return 0;
}

正解

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pr;
const double pi=acos(-1);
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,n,a) for(int i=n;i>=a;i--)
#define Rep(i,u) for(int i=head[u];i;i=Next[i])
#define clr(a) memset(a,0,sizeof a)
#define pb push_back
#define mp make_pair
ld eps=1e-9;
ll pp=998244353;
ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;}
ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;}
ll read(){
ll ans=0;
char last=' ',ch=getchar();
while(ch<'0' || ch>'9')last=ch,ch=getchar();
while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
if(last=='-')ans=-ans;
return ans;
}
//head
#define N 310000
ll f[N],b1[N],b2[N],inv[N],f2[N],b[N],tt[N],e[N],recf[N],recb[N],Sum[N];
int bel[N];
int n;
ll C(int n,int m){
if(n<m)return 0;
if(m==0 || n==m)return 1;
return b[n]*inv[n-m]%pp*inv[m]%pp;
}
void fnt(ll *a,int n,int fl){
for(int i=n>>1,j=1;j<n;j++){
if(i<j)swap(a[i],a[j]);
int k=n>>1;
for(;k&i;i^=k,k>>=1);
i^=k;
}
ll g=powmod(3,(pp-1)/n,pp);
if(fl==-1)g=powmod(g,n-1,pp);
e[0]=1;
for(int i=1;i<n;i++)e[i]=mo(e[i-1]*g,pp);
for(int m=2,t=n>>1;m<=n;m<<=1,t>>=1)
for(int i=0;i<n;i+=m)
for(int j=i;j<i+(m>>1);j++){
ll u=a[j],v=mo(a[j+(m>>1)]*e[(j-i)*t],pp);
a[j]=u+v;
if(a[j]>=pp)a[j]-=pp;
a[j+(m>>1)]=u-v;
if(a[j+(m>>1)]<0)a[j+(m>>1)]+=pp;
}
}
void Add(ll &a,ll b){
a+=b;
if(a>=pp)a-=pp;
}
int get(){
return (rand()<<10)+rand();
}
int main(){
n=read();
b[0]=inv[0]=1;
rep(i,1,n)b[i]=b[i-1]*i%pp,inv[i]=powmod(b[i],pp-2,pp); f[1]=f2[1]=1;
rep(i,1,n)b1[i]=powmod(2,C(i,2),pp); rep(i,1,n)b2[i]=b1[i]*inv[i]%pp;
int nn=256,n2=nn*2;
rep(i,1,n)bel[i]=i/nn;
int num=0;
ll t=powmod(nn*2,pp-2,pp);
rep(i,1,n){
if(i%nn==0){
rep(j,0,nn*2)tt[j]=0;
rep(j,1,bel[i]-1){
int t1=j*n2,t2=(bel[i]-j)*n2;
rep(k,0,nn*2-1)
tt[k]=(tt[k]+recf[t1+k]*recb[t2+k])%pp;
}
fnt(tt,nn*2,-1);
rep(j,0,nn*2-1)Sum[bel[i]*nn+j]=(Sum[bel[i]*nn+j]+tt[j]*t)%pp;
}
f[i]=mo(b1[i]-Sum[i]*b[i-1],pp);
f2[i]=f[i]*inv[i-1]%pp; for(int j=1;j<i && j<nn;j++)
Sum[i+j]=(Sum[i+j]+f2[j]*b2[i]+f2[i]*b2[j])%pp;
if(i<nn)Add(Sum[i+i],f2[i]*b2[i]%pp); if(i%nn==nn-1){
rep(j,0,nn*2)tt[j]=0;
rep(j,bel[i]*nn,i)tt[j%nn]=f2[j];
fnt(tt,nn*2,1);
rep(j,0,nn*2-1)recf[bel[i]*n2+j]=tt[j]; rep(j,0,nn*2)tt[j]=0;
rep(j,bel[i]*nn,i)tt[j%nn]=b2[j];
fnt(tt,nn*2,1);
rep(j,0,nn*2-1)recb[bel[i]*n2+j]=tt[j];
}
}
cout<<f[n]<<endl;
return 0;
}

一言

那是红得像烈焰,像宝石,像盛夏的初恋,比什么都美好的云朵,仿佛天空的一颗心。 ——阿狸·尾巴

[luoguU48834][count]的更多相关文章

  1. nodejs api 中文文档

    文档首页 英文版文档 本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可. Node.js v0.10.18 手册 & 文档 索引 | 在单一页面中浏览 | JSON格 ...

  2. C#中Length和Count的区别(个人观点)

    这篇文章将会很短...短到比你的JJ还短,当然开玩笑了.网上有说过Length和count的区别,都是很含糊的,我没有发现有 文章说得比较透彻的,所以,虽然这篇文章很短,我还是希望能留在首页,听听大家 ...

  3. [PHP源码阅读]count函数

    在PHP编程中,在遍历数组的时候经常需要先计算数组的长度作为循环结束的判断条件,而在PHP里面对数组的操作是很频繁的,因此count也算是一个常用函数,下面研究一下count函数的具体实现. 我在gi ...

  4. EntityFramework.Extended 实现 update count+=1

    在使用 EF 的时候,EntityFramework.Extended 的作用:使IQueryable<T>转换为update table set ...,这样使我们在修改实体对象的时候, ...

  5. 学习笔记 MYSQL报错注入(count()、rand()、group by)

    首先看下常见的攻击载荷,如下: select count(*),(floor(rand(0)*2))x from table group by x; 然后对于攻击载荷进行解释, floor(rand( ...

  6. count(*) 与count (字段名)的区别

    count(*) 查出来的是:结果集的总条数 count(字段名) 查出来的是: 结果集中'字段名'不为空的记录的总条数

  7. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  8. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  9. [LeetCode] Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

随机推荐

  1. Golang的时间生成,格式化,以及获取函数执行时间的方法

    最近都在通过完成一些列功能强化自己对常用api的熟悉. 然而关于时间的api几乎是最常用的api类型,所以总结一些常用的. 以YY-mm-dd HH:MM:SS.9位 输出当前时间: func mai ...

  2. python函数、模块、包

    一.函数 定义函数: def fun_name(para_list): coding def fun_name(para_list): coding return xxx 使用函数,fun_name( ...

  3. 存储过程中的 SET XACT_ABORT ON 和事务

    在存储过程中写SET XACT_ABORT ON 有什么用? SET XACT_ABORT ON是设置事务回滚的!当为ON时,如果你存储中的某个地方出了问题,整个事务中的语句都会回滚为OFF时,只回滚 ...

  4. Bootstrap之响应式导航栏

    代码: <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8 ...

  5. html5 autoplay不起作用 Uncaught (in promise) DOMException: play() failed because the user didn't interac

    chrome://flags/#autoplay-policy

  6. Lodop部署web网站 客户端本地打印角色

    Lodop用于客户端本地打印,部署到web网站非常简单,此博文介绍的是混合部署方式,该方式兼容所有浏览器,当浏览器支持np插件的时候,使用Lodop插件方式,浏览器不支持np插件,会用C-Lodop服 ...

  7. nginx反向代理(动静分离)

    使用反向代理(动静分离)可以让nginx专注静态内容,把动态请求交给apache来处理,发挥各自的优势,而且整个架构更加清晰: 这里假设你已经搭建好了nginx环境; 为了简单起见,就不用源码编译安装 ...

  8. How to remove popup on boot on Windows 2003

    Administrative Tools\Manage Your Server\Add or remove a role\Add or Remove Programs Local Computer P ...

  9. c++ string去除首尾 空格、\n、\r、\t

    string s = " test "; size_t n = s.find_last_not_of(" \r\n\t"); if (n != string:: ...

  10. Python图形库Turtle

    画笔绘制状态函数 函数 描述 pendown() 放下画笔 penup() 提起画笔,与pendown()配合使用 pensize(width) 设置画笔线条的粗细为指定大小 画笔运动函数 函数 描述 ...