题意

一个长度为 \(n\) 的序列 \(A\) ,定义一个 \(1\) 到 \(n\) 的排列 \(p\) 是合法的,当且仅当 \(\forall i \in [1, n − 1], A_{p_i} × A_{p_i+1}\) 不是完全平方数。

求有多少合法的排列,对 \(10^9 + 7\) 取模。

\(n \le 300, A_i \le 10^9\)

题解

对于每个元素去掉它的平方质因子,问题转化为有多少排列 \(p\) 满足 \(\forall i \in [1, n − 1], A_{p_i} \not = A_{p_i+1}\) ,即相邻元素不同。

然后这个就是一个经典模型(多重集合交错排列的经典题目)了。

其中一种做法是 \(dp\) + 容斥。参考了 breezeYoung 的讲解。

令 \(dp[i][j]\) 表示考虑前 \(i\) 个数至多分成 \(j\) 块的个数,那么(此处 \(n_i\) 当前相同的个数):

\[dp[i][j] = \sum_{j = 1} ^ k dp[i - 1][j - k] {n_i-1 \choose j-1} \frac{n_i!}{j!}
\]

这个式子的组合意义就是枚举你当前把 \(n_i\) 个数分成几个独立不同的块,然后对于这些块忽略他们位置的区别。

最后答案要容斥计算:

\[ans = \sum_{i = 0}^{n} (-1)^{n - i} dp[n][i] \times i!
\]

注意前面忽略的区别这里要乘回来他们相应位置的方案。

总结

满足条件的计数还是记得 要容斥啊。转化模型的能力需要提升。

代码

#include <bits/stdc++.h>

#define For(i, l, r) for(register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for(register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl
#define DEBUG(...) fprintf(stderr, __VA_ARGS__) using namespace std; template<typename T> inline bool chkmin(T &a, T b) {return b < a ? a = b, 1 : 0;}
template<typename T> inline bool chkmax(T &a, T b) {return b > a ? a = b, 1 : 0;} inline int read() {
int x(0), sgn(1); char ch(getchar());
for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1;
for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
return x * sgn;
} void File() {
#ifdef zjp_shadow
freopen ("C.in", "r", stdin);
freopen ("C.out", "w", stdout);
#endif
} const int N = 310, Mod = 1e9 + 7; int n, a[N], tot[N], m, dp[N][N]; map<int, int> M; inline int fpm(int x, int power) {
int res = 1;
for (; power; power >>= 1, x = 1ll * x * x % Mod)
if (power & 1) res = 1ll * res * x % Mod;
return res;
} int fac[N], ifac[N];
void Math_Init(int maxn) {
fac[0] = ifac[0] = 1;
For (i, 1, maxn) fac[i] = 1ll * fac[i - 1] * i % Mod;
ifac[maxn] = fpm(fac[maxn], Mod - 2);
Fordown (i, maxn - 1, 1) ifac[i] = 1ll * ifac[i + 1] * (i + 1) % Mod;
} inline int C(int n, int m) {
if (n < 0 || m < 0 || n < m) return 0;
return 1ll * fac[n] * ifac[m] % Mod * ifac[n - m] % Mod;
} int main () { File(); n = read(); Math_Init(n); For (i, 1, n) {
int val = read();
For (j, 2, sqrt(val + .5)) {
int cur = j * j;
while (!(val % cur)) val /= cur;
}
a[i] = val;
if (!M[a[i]]) M[a[i]] = ++ m;
++ tot[M[a[i]]];
} dp[0][0] = 1;
For (i, 1, m) For (j, 1, n)
For (k, 1, min(tot[i], j))
dp[i][j] = (dp[i][j] + 1ll * dp[i - 1][j - k] * C(tot[i] - 1, k - 1) % Mod * fac[tot[i]] % Mod * ifac[k]) % Mod; int ans = 0;
Fordown (i, n, 0)
ans = (ans + (((n - i) & 1) ? -1ll : 1ll) * dp[m][i] * fac[i]) % Mod;
ans = (ans % Mod + Mod) % Mod;
printf ("%d\n", ans); return 0; }

Codeforces Round #429 (Div. 1) C. On the Bench(dp + 组合数)的更多相关文章

  1. 【做题】Codeforces Round #429 (Div. 2) E. On the Bench——组合问题+dp

    题目大意是给你n个数,求相邻两数相乘不是完全平方数的排列数. 一开始看到这题的时候,本人便想给相乘为完全平方数的数对建边,然后就写萎了... 后来通过集体智慧发现这个重要性质:对于自然数a,b,c,若 ...

  2. Codeforces Round #429 (Div. 2) E. On the Bench

    E. On the Bench time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  3. CodeForces 840C - On the Bench | Codeforces Round #429 (Div. 1)

    思路来自FXXL中的某个链接 /* CodeForces 840C - On the Bench [ DP ] | Codeforces Round #429 (Div. 1) 题意: 给出一个数组, ...

  4. CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)

    思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上 ...

  5. CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)

    /* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #includ ...

  6. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  7. 【Codeforces Round #429 (Div. 2) A】Generous Kefa

    [Link]:http://codeforces.com/contest/841/problem/A [Description] [Solution] 模拟,贪心,每个朋友尽量地多给气球. [Numb ...

  8. Codeforces Round #429 (Div. 2/Div. 1) [ A/_. Generous Kefa ] [ B/_. Godsend ] [ C/A. Leha and Function ] [ D/B. Leha and another game about graph ] [ E/C. On the Bench ] [ _/D. Destiny ]

    PROBLEM A/_ - Generous Kefa 题 OvO http://codeforces.com/contest/841/problem/A cf 841a 解 只要不存在某个字母,它的 ...

  9. 【Codeforces Round #429 (Div. 2) C】Leha and Function

    [Link]:http://codeforces.com/contest/841/problem/C [Description] [Solution] 看到最大的和最小的对应,第二大的和第二小的对应. ...

随机推荐

  1. 关于Fatal error: Paletter image not supported by webp 报错

    报错提示 Fatal error: Paletter image not supported by webp 原因是由于图片被非法编辑过(相对PHP来说)造成, 有可能是某些编辑图片的软件的格式与PH ...

  2. Tomcat启用GZIP压缩,提升web性能

    一.前言 最近做了个项目,遇到这么一个问题:服务器返回给客户端的json数据量太大(大概65M),在客户端加载了1分多钟才渲染完毕,费时耗流量,用户体验极其不好.后来网上搜优化的方法,就是Http压缩 ...

  3. Windows 下 Mysql8.0.12 的安装方法

    1. 之前在windows 上面安装了 mysql 5.6 还有 mysql 5.7 遇到了几个坑 , 最近想直接安装最新版的 mysql 8.0.12(较新) 发现还是有坑 跟之前的版本不一样 这里 ...

  4. leetcode资料整理

    注:借鉴了 http://m.blog.csdn.net/blog/lsg32/18712353 在Github上提供leetcode有: 1.https://github.com/soulmachi ...

  5. Django--CRM--一级, 二级 菜单表

    一. 一级菜单表 1. 首先要修改权限表的字段, 在权限表下面加上icon和 is_menu 的字段 2. 展示结果 # 我们既然想要动态生成一级菜单,那么就需要从数据库中拿出当前登录的用户的菜单表是 ...

  6. myisam和innodb的区别对比

    1.MyISAM:默认表类型,它是基于传统的ISAM类型,ISAM是Indexed Sequential Access Method (有索引的顺序访问方法) 的缩写,它是存储记录和文件的标准方法.不 ...

  7. python数学第一天【极限存在定理】

    1.基本回忆 2.两边夹定理 推论1. 基本三角函数的极限 2.极限存在定理 单调有界数列必有极限 (1)单调递增有上界数列必有极限 (2)单调递减有下界数列必有极限 推论1: (1+1/n)^n有极 ...

  8. windows 安装tensorflow

    原文知乎:https://zhuanlan.zhihu.com/p/25778703 前言 看到Rstudio中开始支持Tensorflow,本人是欣喜若狂的,同时TensorFlow官网从16年9月 ...

  9. pip 指定版本

    要用 pip 安装特定版本的 Python 包,只需通过 == 操作符 指定,例如: pip install -v pycrypto==2.3 将安装 pycrypto 2.3 版本.

  10. opencv imdecode和imencode用法

    主要是对内存数据自动编解码 string fname = "D:/image.jpg"; //! 以二进制流方式读取图片到内存 FILE* pFile = fopen(fname. ...