Codeforces 840C. On the Bench 动态规划 排列组合
原文链接https://www.cnblogs.com/zhouzhendong/p/CF840C.html
题解
首先,我们可以发现,如果把每一个数的平方因子都除掉,那么剩下的数,不相等的数都可以相邻,相等的数都不能相邻。
也就是说我们把所有数分成了一些集合,同一个集合内的元素不能相邻,不同集合之间的元素可以相邻。
关键部分到了!
设 $dp[i][j]$ 表示前 $i$ 个集合,有 $j$ 对相邻元素相同的方案数。
转移的时候枚举一下把当前集合分成多少段,有多少段插在之前的相同相邻元素之间。
由于所有集合的size 加起来是 n ,所以时间复杂度不是 $O(n^4)$,是 $O(n^3)$ 的。
代码
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define clr(x) memset(x,0,sizeof (x))
#define For(i,a,b) for (int i=a;i<=b;i++)
#define Fod(i,b,a) for (int i=b;i>=a;i--)
using namespace std;
typedef long long LL;
LL read(){
LL x=0,f=0;
char ch=getchar();
while (!isdigit(ch))
f|=ch=='-',ch=getchar();
while (isdigit(ch))
x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return f?-x:x;
}
const int N=305,mod=1e9+7;
int n;
int a[N],vis[N],s[N];
int v[N],vc=0;
int dp[N][N];
bool check(int x,int y){
int g=__gcd(x,y);
x/=g,y/=g;
int sqx=sqrt(x),sqy=sqrt(y);
return sqx*sqx==x&&sqy*sqy==y;
}
void Add(int &x,int y){
if ((x+=y)>=mod)
x-=mod;
}
int C[N][N],Fac[N];
int main(){
n=read();
For(i,1,n)
a[i]=read();
clr(vis);
For(i,1,n)
if (!vis[i]){
int cnt=0;
For(j,i,n)
if (!vis[j]&&check(a[i],a[j]))
vis[j]=1,cnt++;
v[++vc]=cnt;
s[vc]=s[vc-1]+v[vc];
}
for (int i=Fac[0]=1;i<N;i++)
Fac[i]=(LL)Fac[i-1]*i%mod;
for (int i=0;i<N;i++)
C[i][i]=C[i][0]=1;
for (int i=1;i<N;i++)
for (int j=1;j<i;j++)
C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
clr(dp);
dp[0][0]=1;
For(i,1,vc)
For(j,0,s[i-1]+1)
if (dp[i-1][j])
For(k,1,min(v[i],s[i-1]+1))
For(t,0,min(j,k))
Add(dp[i][j+(v[i]-k)-t],(LL)dp[i-1][j]*Fac[v[i]]%mod*C[v[i]-1][k-1]%mod*C[j][t]%mod*C[s[i-1]+1-j][k-t]%mod);
cout<<dp[vc][0]<<endl;
return 0;
}
Codeforces 840C. On the Bench 动态规划 排列组合的更多相关文章
- codeforces 429 On the Bench dp+排列组合 限制相邻元素,求合法序列数。
限制相邻元素,求合法序列数. /** 题目:On the Bench 链接:http://codeforces.com/problemset/problem/840/C 题意:求相邻的元素相乘不为平方 ...
- 【CodeForces】914 H. Ember and Storm's Tree Game 动态规划+排列组合
[题目]H. Ember and Storm's Tree Game [题意]Zsnuoの博客 [算法]动态规划+排列组合 [题解]题目本身其实并不难,但是大量干扰因素让题目显得很神秘. 参考:Zsn ...
- CodeForces 840C - On the Bench | Codeforces Round #429 (Div. 1)
思路来自FXXL中的某个链接 /* CodeForces 840C - On the Bench [ DP ] | Codeforces Round #429 (Div. 1) 题意: 给出一个数组, ...
- [Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理)
[Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理) 题面 一个\(n \times n\)的格子,每个格子里可以填\([1,k]\)内的整数. ...
- [Codeforces 997C]Sky Full of Stars(排列组合+容斥原理)
[Codeforces 997C]Sky Full of Stars(排列组合+容斥原理) 题面 用3种颜色对\(n×n\)的格子染色,问至少有一行或一列只有一种颜色的方案数.\((n≤10^6)\) ...
- 【CodeForces】889 C. Maximum Element 排列组合+动态规划
[题目]C. Maximum Element [题意]给定n和k,定义一个排列是好的当且仅当存在一个位置i,满足对于所有的j=[1,i-1]&&[i+1,i+k]有a[i]>a[ ...
- AtCoder Grand Contest 002 (AGC002) F - Leftmost Ball 动态规划 排列组合
原文链接https://www.cnblogs.com/zhouzhendong/p/AGC002F.html 题目传送门 - AGC002F 题意 给定 $n,k$ ,表示有 $n\times k$ ...
- Codeforces 840C - On the Bench(dp/容斥原理)
Codeforces 题目传送门 & 洛谷题目传送门 这是一道 *2500 的 D1C,可个人认为难度堪比某些 *2700 *2800. 不过嘛,*2500 终究还是 *2500,还是被我自己 ...
- Codeforces 840C On the Bench dp
On the Bench 两个数如果所有质因子的奇偶性相同则是同一个数,问题就变成了给你n个数, 相同数字不能相邻的方案数. dp[ i ][ j ]表示前 i 种数字已经处理完, 还有 j 个位置需 ...
随机推荐
- 【XSY3141】哲学家 计算几何 线段树
题目描述 有一个平面,最开始平面上没有任何点. 你要按顺序加入 \(n\) 个点,求加入每个点后有多少三角形严格包含原点(在边界上不算). \(n\leq 400000\),无重点. 题解 其实这题本 ...
- LOJ #2533. 「CTSC2018」暴力写挂(边分治合并)
题意 给你两个有 \(n\) 个点的树 \(T, T'\) ,求一对点对 \((x, y)\) 使得 \[ depth(x) + depth(y) - (depth(LCA(x , y)) + dep ...
- 【转】如何在 GitHub 上找到你要的代码?
[来源]
- 洛谷 P4302 【[SCOI2003]字符串折叠】
又来填一个以前很久很久以前挖的坑 首先如果先抛开折叠的内部情况不谈,我们可以得到这样的一个经典的区间DP的式子 $ f[l][r]=min(f[l][r],f[l][k]+f[k+1][r])(l&l ...
- Springboot 4.Springboot 集成SwaggerUi
SwaggerUi就是自动生成接口文档的这么一个类似于插件的工具,可以直接访问接口. 首先打开pom文件,将插件引进来,然后增加一个属性<properties>,用来设置版本号的,然后直接 ...
- Java转换流、缓冲流、流操作规律整理
转换流 1.1 OutputStreamWriter类 OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的字符编码表,将要写入流中的字符编码成字 ...
- Python中查看函数相关文档
1.dir查看对象属性 >>> dir(set) ['__and__', '__class__', '__contains__', '__delattr__', '__dir__', ...
- DirectX11 With Windows SDK--01 DirectX11初始化
前言 由于个人觉得龙书里面第4章提供的Direct3D 初始化项目封装得比较好,而且DirectX SDK Samples里面的初始化程序过于精简,不适合后续使用,故选择了以Init Direct3D ...
- SpringBoot系列: Web应用鉴权思路
==============================web 项目鉴权============================== 主要的鉴权方式有:1. 用户名/密码鉴权, 然后通过 Sess ...
- swoole简单demo测试
测试代码 1.server.php: <?php $serv = new swoole_server("0.0.0.0", 9502); $serv->on('conn ...