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] 看到最大的和最小的对应,第二大的和第二小的对应. ...
随机推荐
- Five Dimensional Points CodeForces - 851C (计算几何+暴力)
C. Five Dimensional Points time limit per test 2 seconds memory limit per test 256 megabytes input ...
- sql中distinct和order by问题的解决方案
需求:根据PID字段对数据去重,根据Sort字段排序,需要显示这个两个字段. 如图,这是原始数据,先排序: 排序后发现两个项是重复的,需要去除一个, 因为Distinct对检查Select里面的每一列 ...
- (Git 学习)Git SSH Key 创建步骤
首先感谢segmentfalut上的朋友对我帮助. 首先:查看你是否有../ssh 这个文件:怎么查看:找到你的git安装目录,在安装目录下查看是否./ssh,以我的为例: 在C盘/Users/11/ ...
- Python之字符串操作
一.字符串特点 内容不可修改 password=' #内容不可修改 二.字符串常用方法 1..strip()方法 去字符串两边的空格和换行符 print(password.strip()) #去掉字符 ...
- ocrosoft 1015 习题1.22 求一元二次方程a*x^2 + b*x + c = 0的根
http://acm.ocrosoft.com/problem.php?id=1015 题目描述 求一元二次方程a*x2 + b*x + c = 0的根.系数a.b.c为浮点数,其值在运行时由键盘输入 ...
- How to Configure Email Notification in Jenkins
How to Configure Email Notification in Jenkins? - The Official 360logica Bloghttps://www.360logica.c ...
- [转帖]csdn windows 下载整理.
特别说明:本帖不提供任何密钥或激活方法,请大家也不要在帖内回复或讨论涉及版权的相关内容,仅提供原版ISO下载链接 https://bbs.csdn.net/topics/391111024?list= ...
- MHA高可用及读写分离
一.MHA简介 二.工作流程 三.MHA架构图 四.MHA工具介绍 五.基于GTID的主从复制 六.部署MHA 七.配置VIP漂移 八.配置binlog-server 九.MySQL中间件Atlas
- NIO和经典IO
NIO未必更快,在Linux上使用Java6完成的测试中,多线程经典I/O设计胜出NIO30%左右 异步I/O强于经典I/O:服务器需要支持超大量的长期连接,比如10000个连接以上,不过各个客户端并 ...
- Spring标签之Bean @Scope
@Bean 的用法 @Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里.添加的bean的id为方法名 定义bean 下面是@Co ...