传送门:


http://codeforces.com/problemset/problem/398/E

题解:


首先答案不超过2。

最长环=1时,ans=0

最长环=2时,ans=1

否则,ans=2

考虑有长度大于2的环时如何两步出解。

那么第一步肯定是把大环拆成若干长度不超过2的环。

不妨确定一个x,设它指向y,指向它的是z,那么肯定将y、z交换,这样x、y在一个环里,然后剩下一个len-2的环,不过因为z不能再动了,所以对这个环的拆分就唯一了,一直下去可以把环拆开,并且只考虑这个环的方案数是len。

有多个环时,这些环的选择时可以相交的。

现在有两个环,

一定有一步交换位于不同环上的两个点,如果依然想拆成若干长度不超过2的环,那么剩下的交换也是唯一的。

由于对称问题,也只有len种。

显然两个环的时候必须长度相等才有解。

然后我并不会证更多环没有解。

然后就可以设个\(f[i][j]\)表示长度为i的有j个环的方案数

\(f[i][j]=f[i][j-1]*i+f[i][j-2]*(j-1)*i\),复杂度是调和级数

那么接下来\(O(k!)\)暴力的话也TLE了。

考虑确定每个点就是把若干条链拼起来,那么就只用集合划分了。

Code:


#include<bits/stdc++.h>
#define fo(i, x, y) for(int i = x, B = y; i <= B; i ++)
#define ff(i, x, y) for(int i = x, B = y; i < B; i ++)
#define fd(i, x, y) for(int i = x, B = y; i >= B; i --)
#define ll long long
#define pp printf
#define hh pp("\n")
using namespace std; const int mo = 1e9 + 7; ll ksm(ll x, ll y) {
ll s = 1;
for(; y; y /= 2, x = x * x % mo)
if(y & 1) s = s * x % mo;
return s;
} const int N = 1e6 + 5; int n, k, a[N], r[N], q[N];
ll fac[15];
vector<ll> f[N], nf[N];
int b[N], b0, cnt[N];
int c[N], c0, d[N];
ll ans, sum, s2; void dg(int x) {
if(x > b0) {
ll s = sum, s3 = s2;
fo(i, 1, c0) {
s3 += c[i] > 2;
s = s * nf[c[i]][cnt[c[i]]] % mo;
cnt[c[i]] ++;
s = s * f[c[i]][cnt[c[i]]] % mo;
}
ll xs = 1;
fo(i, 1, c0) xs = xs * fac[d[i] - 1] % mo;
ans = (ans + (s3 ? s : 1) * xs) % mo;
fo(i, 1, c0) cnt[c[i]] --;
return;
}
fo(i, 1, c0) {
c[i] += b[x];
d[i] ++;
dg(x + 1);
d[i] --;
c[i] -= b[x];
}
c[++ c0] = b[x]; d[c0] = 1;
dg(x + 1);
d[c0] = 0; c0 --;
} int main() {
freopen("determination.in", "r", stdin);
freopen("determination.out", "w", stdout);
fac[0] = 1; fo(i, 1, 15) fac[i] = fac[i - 1] * i % mo;
scanf("%d %d", &n, &k);
fo(i, 1, n) scanf("%d", &a[i]), r[a[i]] ++;
fo(i, 1, n) {
f[i].resize(n / i + 1);
nf[i].resize(n / i + 1);
f[i][0] = 1;
fo(j, 1, n / i) {
f[i][j] = f[i][j - 1];
if(j >= 2) f[i][j] = (f[i][j] + f[i][j - 2] * (j - 1)) % mo;
f[i][j] = f[i][j] * i % mo;
}
ll s = 1;
fo(j, 1, n / i) s = s * f[i][j] % mo;
s = ksm(s, mo - 2);
fd(j, n / i, 0) nf[i][j] = s, s = s * f[i][j] % mo;
s = 1;
fo(j, 1, n / i) {
nf[i][j] = nf[i][j] * s % mo;
s = s * f[i][j] % mo;
}
}
fo(i, 1, n) if(!r[i]) {
int x = i; q[x] = 1;
b[++ b0] = 0;
do {
b[b0] ++;
x = a[x];
q[x] = 1;
} while(x != 0);
}
fo(i, 1, n) if(!q[i]) {
int x = i, len = 0;
do {
len ++;
x = a[x];
q[x] = 1;
} while(x != i);
cnt[len] ++;
}
sum = 1;
fo(i, 1, n) sum = sum * f[i][cnt[i]] % mo, s2 += cnt[i] * (i > 2);
dg(1);
pp("%lld", ans);
}

CF 398 E(动态规划)的更多相关文章

  1. CF 848E(动态规划+分治NTT)

    传送门: http://codeforces.com/problemset/problem/848/E 题解: 假设0-n一定有一条边,我们得到了一个方案,那么显然是可以旋转得到其他方案的. 记最大的 ...

  2. 四角递推(CF Working out,动态规划递推)

    题目:假如有A,B两个人,在一个m*n的矩阵,然后A在(1,1),B在(m,1),A要走到(m,n),B要走到(1,n),两人走的过程中可以捡起格子上的数字,而且两人速度不一样,可以同时到一个点(哪怕 ...

  3. CF 1096D Easy Problem [动态规划]

    题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 有一长度为n的字符串,每一字符都有一个权值,要求现在从中取出若干个字符,使得字符串中没 ...

  4. CF思维联系–CodeForces - 225C. Barcode(二路动态规划)

    ACM思维题训练集合 Desciption You've got an n × m pixel picture. Each pixel can be white or black. Your task ...

  5. CF 494 F. Abbreviation(动态规划)

    题目链接:[http://codeforces.com/contest/1003/problem/F] 题意:给出一个n字符串,这些字符串按顺序组成一个文本,字符串之间用空格隔开,文本的大小是字母+空 ...

  6. CF 414B Mashmokh and ACM 动态规划

    题意: 给你两个数n和k.求满足以下条件的数列有多少个. 这个数列的长度是k: b[1], b[2], ……, b[k]. 并且 b[1] <= b[2] <= …… <= b[k] ...

  7. CF思维联系– Codeforces-987C - Three displays ( 动态规划)

    ACM思维题训练集合 It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in ...

  8. 【DP专辑】ACM动态规划总结

    转载请注明出处,谢谢.   http://blog.csdn.net/cc_again?viewmode=list          ----------  Accagain  2014年5月15日 ...

  9. 【CF932G】Palindrome Partition(回文树,动态规划)

    [CF932G]Palindrome Partition(回文树,动态规划) 题面 CF 翻译: 给定一个串,把串分为偶数段 假设分为了\(s1,s2,s3....sk\) 求,满足\(s_1=s_k ...

随机推荐

  1. vue 插槽 slot

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. rem_2

    html{ font-size:625%; } body{ font-size:0.16rem; }

  3. Collections 工具类常见方法

    Collections 工具类常用方法: 排序 查找,替换操作 同步控制(不推荐,需要线程安全的集合类型时请考虑使用 JUC 包下的并发集合) 排序操作 void reverse(List list) ...

  4. go声明和初始化

    go声明和初始化 当我们第一次看见变量和声明时,我们仅仅看见一些内置的类型,比如整型和字符串.现在我们将学习结构体,并且我们会深入学习包括指针的内容. 通过一种最简单的方式去创建一个结构体值类型: g ...

  5. Yii2中GridView

    Yii2 GridView与dropdownList结合的用法 http://www.yiichina.com/tutorial/473 <?=$form->field($model, ' ...

  6. Shiro学习(12)与Spring集成

    Shiro的组件都是JavaBean/POJO式的组件,所以非常容易使用spring进行组件管理,可以非常方便的从ini配置迁移到Spring进行管理,且支持JavaSE应用及Web应用的集成. 在示 ...

  7. 前台页面中json和字符串相互转化

    比如我有两个变量,我要将a转换成字符串,将b转换成JSON对象: var a={"name":"tom","sex":"男&quo ...

  8. winform界面设计

    http://www.cnblogs.com/wuhuacong/  这位大师给了我指导方向 http://officeribbon.codeplex.com 提供了ribbon界面的控件 动态web ...

  9. caller.arguments.callee.eval

    ------------------------------------ 1.函数的调用方式,与this的指向问题,原型对象中的this 2.对象创建的几种方式! 3.str.replace 页面初始 ...

  10. Android Telephony分析(七) ---- 接口扩展(异步转同步)

    本文是基于上一篇<Android Telephony分析(六) —- 接口扩展(实践篇)>来写的.上一篇介绍的接口扩展的方法需要实现两部分代码:1. 从APP至RIL,发送请求:2. 从R ...