题意

一个长度为 \(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. Django之Django终端打印SQL语句

    Django之Django终端打印SQL语句 在Django项目中,settings.py文件中,在最后添加如下代码即可实现在Django终端打印SQL语句. LOGGING = { 'version ...

  2. 【问题解决方案】之 关于某江加密视频swf专用播放器仍无法播放的问题

    前言: 从pt上下载了一些语言学习的视频之后一直打不开,百度谷歌了若干种方法仍然无解.无奈放弃. 某日从百度知道里又看到一个方法,试了一下,居然灵了.呜呼哀哉.赶紧记下来. 原方法链接:https:/ ...

  3. 解决mysql1336

    1.mysql字符集与插入数据字符集不匹配 USE 数据库名称SHOW VARIABLES LIKE 'character%'SET character_set_server=utf8;SET cha ...

  4. babel(一)

    一.babel npm babel src/index.js -d lib 二.@babel/core   @babel/cli @babel/core  转换语法核心 @babel/cli   执行 ...

  5. 城市联动 - 自动生成SQL语句

    字段比较简单/  如果有需要可以自己定制字段和排序/ 一共二级城市联动, 本人业务需要, 所以就两层, 网上关于三层的挺多, 有需要可以借鉴/ 废话不多说, 先看效果图, 代码在下面 <?php ...

  6. hive字符函数

  7. python之路--内置模块02

    一. namedtuple 命名元组->类似创建了一个类 from collections import namedtuple # 类 p = namedtuple("Point&qu ...

  8. linux audit审计(8)--ausearch搜索audit日志文件

    ausearch这个工具,可以针对指定的事件来搜索audit日志文件.默认情况下,ausearch搜索/var/log/audit/audit.log这个文件. The ausearch utilit ...

  9. 使用synchronized 实现ReentrantLock(美团面试题目)

    刚看到这个题目的时候无从下手,因为觉得synchronized和lock在加锁的方式上有很大不同,比如,看看正常情况下synchronized时如何加锁的. 方式一: public synchroni ...

  10. 如何抓取电商的数据 & Python

    如何抓取电商的数据 & Python https://www.zhihu.com/question/40720286 https://www.zhihu.com/question/382455 ...