Codeforces Round #429 (Div. 2) E. On the Bench
2 seconds
256 megabytes
standard input
standard output
A year ago on the bench in public park Leha found an array of n numbers. Leha believes that permutation p is right if for all 1 ≤ i < n condition, that api·api + 1 is not perfect square, holds. Leha wants to find number of right permutations modulo 109 + 7.
First line of input data contains single integer n (1 ≤ n ≤ 300) — length of the array.
Next line contains n integers a1, a2, ... , an (1 ≤ ai ≤ 109) — found array.
Output single integer — number of right permutations modulo 109 + 7.
3
1 2 4
2
7
5 2 4 2 4 1 1
144
For first example:
[1, 2, 4] — right permutation, because 2 and 8 are not perfect squares.
[1, 4, 2] — wrong permutation, because 4 is square of 2.
[2, 1, 4] — wrong permutation, because 4 is square of 2.
[2, 4, 1] — wrong permutation, because 4 is square of 2.
[4, 1, 2] — wrong permutation, because 4 is square of 2.
[4, 2, 1] — right permutation, because 8 and 2 are not perfect squares.
题意 :求相邻的元素相乘不为平方数的方案数(a[0]=a[1]=1, 视 a[0] 与 a[1] 不同)
思路 :
每个数可以表示为 p1^a1 * p2^a2 * .....
如果 两个数A,B相乘为平方数 则 a1%2 = a1' %2 , a2%2 = a2'%2 .....
即 对应质因子的幂次 奇偶性相同 这样就可以划分出T组
然后题目就转化为 T种物品 相同种类物品不能放在相邻 求方案数
这题就变成原题 :https://csacademy.com/contest/archive/task/distinct_neighbours/statement/
http://acm.hdu.edu.cn/showproblem.php?pid=6116
做法为dp
dp [ i ] [ j ] 表示 插入第 i 组的物品 出现了 左右为相同物品的空隙个数为 j 的方案数
那 dp [ T ] [ 0 ] 就是最终答案了
附: cs官方题解 (原题的题解)
First we group all the distinct values in the array. Then we can solve the problem using dynamic programming:
Let dp[i][j] = the number of distinct arrays that can be obtained using the elements of the first i groups such that there are exactly j pairs of consecutive positions having the same value. The answer can be found in dp[distinctValues][0].
Now let's say the sum of frequences of the first i values is X. This means the arrrays we can build using the elements from these i groups have size X, so we can insert the elments of group i + 1 in X + 1 gaps: before the first element, after the last, and between any two consecutive. We can fix the number k of gaps where we want to insert at least one element from group i + 1, but we also need to fix the number l of these k gaps which will be between elements that previously had the same value. State dp[i][j] will update the state dp[i + 1][j - l + frequence[i + 1] - k].
The number of ways of choosing k gaps such that exactly l are between consecutive elements having the same value can be computed easily using combination formulas. We are left with finding out the number of ways of inserting frequence[i + 1] elements in k gaps. This is also a classical problem with a simple answer: Comb(frequence[i + 1] - 1, k - 1).
具体转移方程见代码 :
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <functional> #define mp make_pair
#define pb push_back
#define mes(a,b) memset(a,b,sizeof(a))
#define mes0(a) memset(a,0,sizeof(a))
#define lson l,mid,pos<<1
#define rson mid+1,r,pos<<1|1
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define fi first
#define se second
#define sss(a) a::iterator
#define all(a) a.begin(),a.end() using namespace std; typedef double DB;
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<long long ,int> pli;
typedef pair<int,long long > pil;
typedef pair<string,int> psi;
typedef pair<long long ,long long > pll; const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const int maxn = +;
const int mod = 1e9+;
LL dp[][];
LL C[][];
LL fact[];
int cnt[];
int a[];
int vis[];
int sz;
int check(LL x)
{
LL l=,r=1e9;
LL now=l;
while (l<=r){
LL mid=(l+r)>>;
if (mid*mid<=x)now=mid,l=mid+;
else r=mid-;
}
return now*now==x;
}
inline LL M(LL x)
{
return x%mod;
}
void init()
{
C[][]=;
fact[]=;
for (int i=;i<=;i++){
C[i][]=;
for (int j=;j<=i;j++){
C[i][j]=M(C[i-][j]+C[i-][j-]);
}
}
for (int i=;i<=;i++)fact[i]=M(fact[i-]*i);
}
void slove()
{
dp[][cnt[]-]=;
int lim=cnt[];
for (int i=;i<sz;i++){
for (int j=;j<lim;j++){ /// dp[i-1][j]
for (int k=;k<cnt[i];k++){/// group[i]分成k+1组 ,cnt[i]-1-k个空隙
for (int m=;m<=min(j,k+);m++){ /// 选了m个左右相同的空隙插入
dp[i][j+cnt[i]--k-m]=M(dp[i][j+cnt[i]--k-m]+dp[i-][j]*C[cnt[i]-][k]%mod*C[j][m]%mod*C[lim--j+][k+-m]);
}
}
}
lim+=cnt[i];
}
LL ans=dp[sz-][];
for (int i=;i<sz;i++){
ans*=fact[cnt[i]];
ans%=mod;
}
cout<<ans<<endl;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
scanf("%d",&n);
for (int i=;i<n;i++){
scanf("%d",a+i);
}
for (int i=;i<n;i++){
if (vis[i]==){
for (int j=i;j<n;j++){
if (check(1LL*a[i]*a[j])){
cnt[sz]++;
vis[j]=;
}
}
sz++;
}
}
init();
slove();
return ;
}
Codeforces Round #429 (Div. 2) E. On the Bench的更多相关文章
- Codeforces Round #429 (Div. 1) C. On the Bench(dp + 组合数)
题意 一个长度为 \(n\) 的序列 \(A\) ,定义一个 \(1\) 到 \(n\) 的排列 \(p\) 是合法的,当且仅当 \(\forall i \in [1, n − 1], A_{p_i} ...
- 【做题】Codeforces Round #429 (Div. 2) E. On the Bench——组合问题+dp
题目大意是给你n个数,求相邻两数相乘不是完全平方数的排列数. 一开始看到这题的时候,本人便想给相乘为完全平方数的数对建边,然后就写萎了... 后来通过集体智慧发现这个重要性质:对于自然数a,b,c,若 ...
- 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 #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] 看到最大的和最小的对应,第二大的和第二小的对应. ...
- 【Codeforces Round #429 (Div. 2) B】 Godsend
[Link]:http://codeforces.com/contest/841/problem/B [Description] 两个人轮流对一个数组玩游戏,第一个人可以把连续的一段为奇数的拿走,第二 ...
随机推荐
- hadoop运行报错Wrong FS: hdfs:/, expected: file:///
内容源自:https://blog.csdn.net/u014470581/article/details/51480600 报错信息: Exception in thread "main& ...
- 系统找不到指定文件 No installed service name 'Apache2'
原因:系统服务中没有apache2服务 解决方法: 开始 --运行 --- 输入“CMD”出来DOS窗口---- 输入 D: 回车 再输入 cd D:/Program Files(x86)/Apach ...
- 如何使用angularjs实现按钮事件
<!DOCTYPE html> <html ng-app="myApp"> <head> <title>angularjs-setV ...
- Android Studio修改项目名和包名
为了提高开发效率,有时候需要使用现有的一些开源项目,记录一下自己修改项目名和包名的方法. 1.首先,修改包名(清单文件里找), ①展开所有包 ②选中想要修改的包,shift+F6(也可右键Refact ...
- 《Linux内核设计与实现》笔记-1-linux内核简单介绍
一.Linux内核相对于传统的UNIX内核的比較: (1):Linux支持动态内核模块. 虽然Linux内核也是总体式结构,但是同意在须要的时候动态哦卸除(rmmod xxx)和载入内核模块(insm ...
- 算法笔记_070:BellmanFord算法简单介绍(Java)
目录 1 问题描述 2 解决方案 2.1 具体编码 1 问题描述 何为BellmanFord算法? BellmanFord算法功能:给定一个加权连通图,选取一个顶点,称为起点,求取起点到其它所有顶 ...
- Can report_aeroo_ooo work with Ubuntu 13.10?
来 自 :https://www.odoo.com/forum/help-1/question/can-report-aeroo-ooo-work-with-ubuntu-13-10-34314 I ...
- 学会使用简单的MySQL操作
第十八章 学会使用简单的MySQL操作 在前面两个章节中已经介绍过MySQL的安装了.可是光会安装还不够.还须要会一些主要的相关操作.当然了,关于MySQL的内容也是非常多的.仅仅只是对于linux系 ...
- linux中的strip命令简介------给文件脱衣服
1.去掉-g,等于程序做了--strip-debug2.strip程序,等于程序做了--strip-debug和--strip-symbol 作为一名Linux开发人员, 如果没有听说过strip命令 ...
- Name与x:Name的关系
小序: 如果想用Google搜包含冒号的内容怎么办?比如我想搜x:Name这个字符串…… 原来,应该是这样——x::Name 这世道,连搜索也要加转义,全民程序员,要不要人活了? 正文: ...