Codeforces Round #429 (Div. 1) C. On the Bench(dp + 组合数)
题意
一个长度为 \(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\) 当前相同的个数):
\]
这个式子的组合意义就是枚举你当前把 \(n_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 + 组合数)的更多相关文章
- 【做题】Codeforces Round #429 (Div. 2) E. On the Bench——组合问题+dp
题目大意是给你n个数,求相邻两数相乘不是完全平方数的排列数. 一开始看到这题的时候,本人便想给相乘为完全平方数的数对建边,然后就写萎了... 后来通过集体智慧发现这个重要性质:对于自然数a,b,c,若 ...
- 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 ...
- CodeForces 840C - On the Bench | Codeforces Round #429 (Div. 1)
思路来自FXXL中的某个链接 /* CodeForces 840C - On the Bench [ DP ] | Codeforces Round #429 (Div. 1) 题意: 给出一个数组, ...
- CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)
思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上 ...
- CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)
/* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #includ ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- 【Codeforces Round #429 (Div. 2) A】Generous Kefa
[Link]:http://codeforces.com/contest/841/problem/A [Description] [Solution] 模拟,贪心,每个朋友尽量地多给气球. [Numb ...
- 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 解 只要不存在某个字母,它的 ...
- 【Codeforces Round #429 (Div. 2) C】Leha and Function
[Link]:http://codeforces.com/contest/841/problem/C [Description] [Solution] 看到最大的和最小的对应,第二大的和第二小的对应. ...
随机推荐
- Accordion CodeForces - 1101B (实现)
An accordion is a string (yes, in the real world accordions are musical instruments, but let's forge ...
- 课程存储校对:程序设计思想、源程序代码、运行结果截图,以及开发过程中的项目计划日志、时间记录日志、缺陷记录日志(PSP0级记录)。
1.程序设计思想 ⑴将JDBC驱动jar包导入到WEB-INF的lib文件夹下 ⑵建立数据库,在数据库中建表,分别将课程名称.任课教师及上课地点录入到列中 ⑶首先写出加载驱动.关闭资源的工具类和异常处 ...
- nodejs 中的一些方法
fs.unlink(path, [callback(err)]) //删除文件操作. //path 文件路径 //callback 回调,传递一个异常参数err. ndoe中解决跨域问题 expres ...
- toTree
// js实现树级递归, // 通过js生成tree树形菜单(递归算法) const data = [ { id: 1, name: "办公管理", pid: 0 }, { id: ...
- python(Django之Logging、API认证)
一.Loging模块 用于方便的记录日志的模块 import logging logging.basicConfig(filename='log.log', format='%(asctime)s - ...
- MySQL磁盘写入策略以及数据安全性的相关参数
转载自:http://blog.itpub.net/22664653/viewspace-1063134/ innodb_flush_log_at_trx_commit和sync_binlog ...
- npm 设置代理
设置代理 npm config set proxy http://username:password@server:portnpm config set https-proxy http://user ...
- Java的hashCode和equals方法
当然健壮的代码,两个都重写那是最好. 用不到hashCode的, 也没有必要重写hashCode. 个人感觉. 哈希机制的Java集合类,例如 Hashtable, HashMap, HashSet ...
- Delphi 在dbgrideh中表格输入数据时有效性的检查(转)
在数据库系统设计中经常要用到在表格中进行数据录入,如何判断在数据导入时的数据有效性呢?下面介绍几种常用的方法与大家交流. 方法一:Dbgrid是与Table,在Table的Column的OnSetTe ...
- mysql运行sql文件出错
从服务器上备份表数据到本地,使用的工具是Navicat,右键表转储sql文件,但是在本地运行sql文件时一直报异常 [Err] 1064 - You have an error in your SQL ...