题目链接

思路

这个题可以考虑用全部情况减去不合法的情况,来求解。首先需要知道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. python之路--BOM和DOM

    一. 介绍 之前学的JS的一些简单的语法并没有和浏览器有任何的交互. 我们要想制作一些我们经常看到的网页的一些交互,我们需要继续学习BOM和DOM相关知识. JavaScript 分为 ECMAScr ...

  2. spec文件中的 %pre %post %preun %postun

    转载http://meinit.nl/rpm-spec-prepostpreunpostun-argument-values RPM has 4 parts where (shell) scripts ...

  3. systemd取消对服务重启的限制

    默认情况下,一个服务在10秒内最多允许启动5次.当超过5次后,会报如下错误: Job for xx.service failed because start of the service was at ...

  4. docker学习笔记二

    知识点: 1)手动构建镜像 2)Dockerfile快速构建镜像 阿里云yum源https://opsx.alibaba.com/mirror 镜像制作nginx镜像实例 创建并运行centos容器 ...

  5. 二、K8S镜像问题

    根据前面错误信息来看我们需要下载的镜像.就当前来说,用户 mirrorgooglecontainers 在 docker hub 同步了所有 k8s 最新的镜像,先从这儿下载,然后修改 tag 即可. ...

  6. 关于SQL查询语句中的LIKE模糊查询的解释

    LIKE语句的语法格式为: select * from 表名 where 字段名 like 对应值(字符串) 注:主要是针对字符型字段的,它的作用是在一个字符型字段列中检索包含对应字符串的. 下面列举 ...

  7. 配置Web.config 元素CustomErrors

    一.customErrors 元素 属性 说明 defaultRedirect 指定出错时将浏览器定向到的默认 URL.如果未指定该属性,则显示一般性错误. 可选的属性. URL 可以是绝对的(如 w ...

  8. codeforces569B

    Inventory CodeForces - 569B Companies always have a lot of equipment, furniture and other things. Al ...

  9. ubuntu终端快捷键

    ctrl+alt+t 新终端 ctrl+shift+t打开新的标签页 ctrl+d关闭终端 ctrl+s 暂停屏幕输出 ctrl+q 继续屏幕输出 ctrl+l 清屏 ctrl+alt+f1 切换到第 ...

  10. 删除本地git的远程分支和远程删除git服务器的分支【转】

    转- 删除本地git的远程分支和远程删除git服务器的分支 在项目中使用git管理代码后,有些时候会创建很多不同名称的分支,以此区分各个分支代码功能. 而随着代码的合并,以前的分支就可能不再需要保存了 ...