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] 两个人轮流对一个数组玩游戏,第一个人可以把连续的一段为奇数的拿走,第二 ...
随机推荐
- http://blog.csdn.net/a9529lty/article/details/6454156
http://blog.csdn.net/a9529lty/article/details/6454156
- 破解IDEA Ultimate2017 测试
转载:http://blog.csdn.net/linyanqing21/article/details/72594352 IntelliJ Idea 2017 免费激活方法: 1.到网站 http: ...
- DevExpress TreeList使用
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; us ...
- Autolayout约束动画化-Animating Autolayout Constraints
原文:Animating Autolayout Constraints 作者:@kharrison 译者:CocoaChina--起个名字好难(CC论坛ID) 首发:CocoaChina 记于二零一五 ...
- SQL Server 2008 R2 清空数据库中ldf日志文件
/************************************************************ * Sql Server 2008 R2 清空数据库中ldf日志文件 * 将 ...
- JS设计模式基础
设计模式: 通过封装.继承.多态.组合等技术的反复使用,提炼出一些可重复使用的面向对象设计技巧. 1.多态(’做什么‘和’谁去做‘分开) 多态指同一个实体同时具有多种形式. 同一操作应用于不同的对象上 ...
- android 实现调查问卷-单选-多选
非常久没写东西了.今天来总结下有关android调查问卷的需求实现. 转载请加地址:http://blog.csdn.net/jing110fei/article/details/46618229 先 ...
- Apache James 发送邮件到外网
在config.xml文件中查找到<dnsserver>然后把默认的<server> 127.0.0.1</server> 改成如下形式:<dnsserver ...
- 使用Python SDK管理Azure Load Balancer
概述 下面将演示如何使用Python SDK管理中国区Azure Load balancer.关于Azure负载均衡器的详细功能介绍,请参考官方文档. Code Sample import os fr ...
- angular过滤器使用 自定义过滤器
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...