Cut Pieces

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 728    Accepted Submission(s): 303

Problem Description
Suppose we have a sequence of n blocks. Then we paint the blocks. Each block should be painted a single color and block i can have color 1 to color ai. So there are a total of prod(ai) different ways to color the blocks.  Consider one way to color the blocks. We call a consecutive sequence of blocks with the same color a "piece". For example, sequence "Yellow Yellow Red" has two pieces and sequence "Yellow Red Blue Blue Yellow" has four pieces. What is S, the total number of pieces of all possible ways to color the blocks? 
This is not your task. Your task is to permute the blocks (together with its corresponding ai) so that S is maximized.
 
Input
First line, number of test cases, T.  Following are 2*T lines. For every two lines, the first line is n, length of sequence; the second line contains n numbers, a1, ..., an
Sum of all n <= 106.  All numbers in the input are positive integers no larger than 109.
 
Output
Output contains T lines.  Each line contains one number, the answer to the corresponding test case.  Since the answers can be very large, you should output them modulo 109+7. 
 
Sample Input
1
3
1 2 3
 
Sample Output
14

Hint

Both sequence 1 3 2 and sequence 2 3 1 result in an S of 14.

 
Source
 
Recommend
zhuyuanchen520
 

对于一个序列:a,b,c,d,e,f....

设sum=a*b*c*d*....;

对于a,b,当a与b不同的时候可以增加(ab-min(a,b))*(sum/ab)=(s-s/max(a,b));

max(a,b)越大,增加的也就越大!所以当然让大的数多取,

比如:n=7的时候

对于序列1 2 3 4 5 6 7

我可以排列成

1 5 2 6 3 7 4;

这样我就使得最大的三个数(5,6,7)都取了两次,从而答案是最优的!!!

n=6的时候

对于序列1 2 3 4 5 6

可以排列成

1 5 2 6 3

还有个4就只能放边上了!!!

即5,6都取了两次,而4只能取1次啦!!!

 #include<algorithm>
#include<cstdio>
#include<cstring>
#include<iostream>
#define MAX 1000010
#define mod 1000000007
using namespace std;
typedef long long ll; int ext_gcd(int a,int b,int &x,int &y)
{
if(b==){x=;y=;return a;}
int d=ext_gcd(b,a%b,x,y),t;
t=x;x=y;y=t-a/b*y;
return d;
}
int inv(int a,int mo)
{
int x,y,dx,g;
g=ext_gcd(a,mo,x,y);
dx=mo/g;
return (x%dx+dx)%dx;
} int a[];
int main()
{
int head,tail,T,i,n,jnm;
ll ji,ans;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
ji=;
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
ji*=a[i];
ji%=mod;
}
sort(a+,a+n+);
ans=ji;
head=n-n/+;
tail=n;
if(n&)//区分奇偶的区别!!!!
ans=((ans+*(ji-ji*inv(a[head-],mod)%mod))%mod+mod)%mod;
else
ans=((ans+*(ji-ji*inv(a[head-],mod)%mod))%mod+mod)%mod;
for(i=head;i<=tail;i++)
ans=((ans+*(ji-ji*inv(a[i],mod)%mod))%mod+mod)%mod;
if(n==)ans=a[];
printf("%lld\n",ans);
}
return ;
}

hdu 4655 Cut Pieces(想法题)的更多相关文章

  1. HDU 4655 Cut Pieces(2013多校6 1001题 简单数学题)

    Cut Pieces Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total ...

  2. HDU 4655 Cut Pieces(数学分析题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4655 题意:给出n个石子,第i个石子可以染ai种颜色.对于每种颜色,比如颜色1221,我们称有3段.连 ...

  3. hdu 4655 Cut Pieces 找规律

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4655 题意:给你一组整数,代表每个木块所能涂成的颜色种数(编号1~ai),相邻的两块所能涂成的颜色如果是一 ...

  4. hdu 4655 Cut Pieces

    这个解题报告讲的很详细了!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #in ...

  5. HDU 4655 Cut Pieces 找规律+简单计数

    解法参考:http://blog.csdn.net/a601025382s/article/details/9840125 #include <cstdio> #include <c ...

  6. HDU - 5806 NanoApe Loves Sequence Ⅱ 想法题

    http://acm.hdu.edu.cn/showproblem.php?pid=5806 题意:给你一个n元素序列,求第k大的数大于等于m的子序列的个数. 题解:题目要求很奇怪,很多头绪但写不出, ...

  7. HDU 4972 Bisharp and Charizard 想法题

    Bisharp and Charizard Time Limit: 1 Sec  Memory Limit: 256 MB Description Dragon is watching NBA. He ...

  8. CodeForces 111B - Petya and Divisors 统计..想法题

    找每个数的约数(暴力就够了...1~x^0.5)....看这约数的倍数最后是哪个数...若距离大于了y..统计++...然后将这个约数的最后倍数赋值为当前位置...好叼的想法题.... Program ...

  9. HDU 4762 Cut the Cake (2013长春网络赛1004题,公式题)

    Cut the Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

随机推荐

  1. UOJ37. 【清华集训2014】主旋律

    http://uoj.ac/problem/37 题解 题目是让我们求出有多少个边集可以使这张图强连通. 先补集转化一下,求这张图不强连通的方案数. 我们考虑这样的图缩完点之后的情况,既然不强连通,那 ...

  2. CCPC E Problem Buyer

    题目描述 有一些区间,还有一些点. 问最小的k使得选出任意k个区间,每个点都可以匹配上区间,一个区间只能匹配一次. 题解 考虑对于每一个点,我们有\(x\)个区间包含它,那么答案的一个下界是\(n-x ...

  3. 深入理解JVM虚拟机11:Java内存异常原理与实践

    本文转自互联网,侵删 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutori ...

  4. Qt之zip压缩/解压缩(QuaZIP)

    摘要: 简述 QuaZIP是使用Qt/C++对ZLIB进行简单封装的用于压缩及解压缩ZIP的开源库.适用于多种平台,利用它可以很方便的将单个或多个文件打包为zip文件,且打包后的zip文件可以通过其它 ...

  5. 对象数组(JSON) 根据某个共同字段 分组

    1.在前端页面用js处理 转载https://www.cnblogs.com/rysinal/p/5834446.html 为了第二种方法对比,特意把id 转化为pid var arr = [ {&q ...

  6. 【zabbix】zabbix 高可用架构的实现

    https://www.jianshu.com/p/249d47b089b4?utm_campaign=maleskine&utm_content=note&utm_medium=se ...

  7. 查看Linux中lanmp的各软件编译参数

    转载 如何查看已经安装的nginx.apache.mysql和php的编译参数 Mysql查看mysql编译参数 Mysql5.4及之前版本查看编译安装参数,进入到mysql安装目录的bin下面 $ ...

  8. 十九、python内置函数汇总

    '''内置函数abs():取绝对值all():每个元素都为真,才是真any():有一个元素为真即为真bin():十进制转二进制hex():十进制转十六进制int():所有的转成十进制oct():十进制 ...

  9. 2013 AAAI: Uncorrelated Lasso

    Si-Bao Chen, Chris Ding, Bin Luo and Ying Xie. Uncorrelated Lasso. AAAI, 2013. 第一作者是安徽大学陈思宝副教授. 第二作者 ...

  10. Powershell 邮件发送

    目录 目录 前言 Send-MailMessage NETMail 使用OutLook发送邮件 前言 最近领导想在winServer2012上搞个自动发送邮件的计划任务,下面有几种发送邮件的方式. 如 ...