On the Bench

两个数如果所有质因子的奇偶性相同则是同一个数,问题就变成了给你n个数, 相同数字不能相邻的方案数。

dp[ i ][ j ]表示前 i 种数字已经处理完, 还有 j 个位置需要隔开的方案数。

转移的话, 我们枚举第i + 1种数字分成的段数, 然后枚举有几段插到 j 个空格里面, 然后转移。

最后乘上各种数字个数的阶乘。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n, cnt, a[N], c[N];
int g[N][N], sum[N];
int dp[N][N];
ull hs[N];
map<int, int> Map;
vector<ull> oo; int F[N], Finv[N], inv[N]; void init() {
inv[] = F[] = Finv[] = ;
for(int i = ; i < N; i++) inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod;
for(int i = ; i < N; i++) F[i] = 1LL * F[i - ] * i % mod;
for(int i = ; i < N; i++) Finv[i] = 1LL * Finv[i - ] * inv[i] % mod;
}
int comb(int n, int m) {
if(n < || n < m) return ;
return 1LL * F[n] * Finv[m] % mod * Finv[n - m] % mod;
}
int main() {
init();
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
Map.clear();
for(int j = ; j * j <= a[i]; j++) {
if(a[i] % j) continue;
while(a[i] % j == ) {
Map[j]++;
a[i] /= j;
}
}
if(a[i] > ) Map[a[i]]++;
for(auto& t : Map) {
if(t.se & ) {
hs[i] *= ;
hs[i] += t.fi;
}
}
oo.push_back(hs[i]);
}
sort(ALL(oo));
oo.erase(unique(ALL(oo)), oo.end());
for(int i = ; i <= n; i++) a[i] = lower_bound(ALL(oo), hs[i]) - oo.begin() + ;
for(int i = ; i <= n; i++) c[a[i]]++;
cnt = n;
n = SZ(oo);
for(int i = ; i <= n; i++) sum[i] = sum[i - ] + c[i];
g[][] = ;
for(int i = ; i <= cnt; i++) {
for(int j = ; j <= cnt; j++) {
if(!g[i][j]) continue;
for(int k = ; i + k <= cnt; k++) {
add(g[i + k][j + ], g[i][j]);
}
}
}
dp[][c[] - ] = ;
for(int i = ; i < n; i++) {
for(int j = ; j <= cnt; j++) {
if(!dp[i][j]) continue;
int num = c[i + ];
for(int k = ; k <= num && k <= sum[i] + ; k++) {
int way = g[num][k];
for(int z = max(, k - sum[i] - + j); z <= k; z++) {
add(dp[i + ][j - z + num - k], 1LL * way * comb(j, z) % mod * comb(sum[i] + - j, k - z) % mod * dp[i][j] % mod);
}
}
}
}
int ans = dp[n][];
for(int i = ; i <= n; i++)
ans = 1LL * ans * F[c[i]] % mod;
printf("%d\n", ans);
return ;
} /*
*/

Codeforces 840C On the Bench dp的更多相关文章

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

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

  2. Codeforces 840C - On the Bench(dp/容斥原理)

    Codeforces 题目传送门 & 洛谷题目传送门 这是一道 *2500 的 D1C,可个人认为难度堪比某些 *2700 *2800. 不过嘛,*2500 终究还是 *2500,还是被我自己 ...

  3. codeforces 429 On the Bench dp+排列组合 限制相邻元素,求合法序列数。

    限制相邻元素,求合法序列数. /** 题目:On the Bench 链接:http://codeforces.com/problemset/problem/840/C 题意:求相邻的元素相乘不为平方 ...

  4. Codeforces 840C. On the Bench 动态规划 排列组合

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF840C.html 题解 首先,我们可以发现,如果把每一个数的平方因子都除掉,那么剩下的数,不相等的数都可以相 ...

  5. [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)

    [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...

  6. Codeforces 840C 题解(DP+组合数学)

    题面 传送门:http://codeforces.com/problemset/problem/840/C C. On the Bench time limit per test2 seconds m ...

  7. codeforces 721C (拓排 + DP)

    题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...

  8. codeforces 711C Coloring Trees(DP)

    题目链接:http://codeforces.com/problemset/problem/711/C O(n^4)的复杂度,以为会超时的 思路:dp[i][j][k]表示第i棵数用颜色k涂完后bea ...

  9. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

随机推荐

  1. Shell入门及实践

    解释器 解释器是一种命令解释器,主要作用是对命令进行运行和解释,将需要执行的操作传递给操作系统内核并执行 #!/bin/bash(默认),指定解释器 #!/bin/bash #这是第一个shell脚本 ...

  2. JDK源码分析(7)String

    String String表示字符串,Java中所有字符串的字面值都是String类的实例,例如"ABC".字符串是常量,在定义后不能被改变,字符串缓冲区支持可变的字符串.因为St ...

  3. Connection to Oracle failed. [66000][12505] Listener refused the connection with the following error: ORA-12505, TNS:listener does not currently know of SID given in connect descriptor .

    我安装了Oracle数据库,默认的数据库用户名是system,密码口令是安装过程中你自己设置的.可以先使用命令框,输入 sqlplus system; 然后再输入密码即可. 然后我的数据库连接工具使用 ...

  4. SQLALlchemy数据查询小集合

    SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作.将对象转换成SQL,然后使用数据API执行SQL并获取执行结果.在写项目的过 ...

  5. [物理学与PDEs]第3章习题4 理想磁流体的能量守恒方程

    试证明: 对理想磁流体, 能量守恒方程 (4. 14) 可以写为如下形式: $$\beex \bea \cfrac{\p}{\p t}&\sex{\rho e+\cfrac{1}{2}\rho ...

  6. 锁定表头和固定列(Fixed table head and columns)

    源码: /// <summary> /// 锁定表头和列 /// <para> sorex.cnblogs.com </para> /// </summary ...

  7. 在Windows上安装Arduino-IDE

    Arduino IDE的官方下载地址为:http://arduino.cc/en/Main/Software 也可以从我的网盘下载:win系统 1.8.9版本 链接:https://pan.baidu ...

  8. babel

    一款可以将 ES6 代码转换为 ES5 代码的转译器. 官网:http://babeljs.io/ 中文:https://www.babeljs.cn/

  9. cas单点登录防止登出退出后刷新后退ticket失效报500错

    https://www.cnblogs.com/wangyang108/p/5844447.html

  10. Java基础8-多线程;同步代码块

    作业解析 利用白富美接口案例,土豪征婚使用匿名内部类对象实现. interface White{ public void white(); } interface Rich{ public void ...