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. 在centos上配置环境

    1.   安装wget [root@localhost ~]# yum -y install wget 2.   在oneinstack官网配置安装环境 wget http://mirrors.lin ...

  2. python分别使用多线程和多进程获取所有股票实时数据

    python分别使用多线程和多进程获取所有股票实时数据   前一天简单介绍了python怎样获取历史数据和实时分笔数据,那么如果要获取所有上市公司的实时分笔数据,应该怎么做呢? 肯定有人想的是,用一个 ...

  3. AMD - Learning JavaScript Design Patterns [Book] - O'Reilly

    AMD - Learning JavaScript Design Patterns [Book] - O'Reilly The overall goal for the Asynchronous Mo ...

  4. Linux函数的使用方法

    [root@a ~]#cat fun.txt #定义函数库文件,方便在别的地方使用 addnum1() { echo $[$1+$2] } addnum2(){ echo $[$1*$2] } del ...

  5. qbzt day1 上午

    内容提要 模拟,贪心 在讲这些东西之前,我们先来了解一个东西:high level 这个东西大体上就是你做题之前要先想清楚自己要写什么,怎么写,然后再写,不要有一点写一点 1.模拟 模拟算法算是很水的 ...

  6. MongoDB学习【一】—MongoDB简介和安装

    一.MongoDB简介 1.MongoDB是什么 MongoDB是一款强大.灵活.且易于扩展的通用型数据库,MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的 ...

  7. beyond compare 4.2.9桌面右键集成的问题修复

    beyond compare 4.2.9桌面右键集成的问题修复 安装后,发现在WIN64时,注册表中注册的DLL库有问题 出错处: [HKEY_CLASSES_ROOT\CLSID\{57FA2D12 ...

  8. Hbse笔记

    1 角色    HMaster    RegionServer        Region:一张table        Hbase为了读写高效 有二级缓存,内存的缓存和磁盘的缓存        HL ...

  9. 【python+selenium自动化】基于Autolt实现上传

    在UI自动化过程中,总会遇到文件上传的操作,一般的,标签为input,可以直接使用sendkeys 如果他仅仅是一个button,那则无法直接sendkeys,则需要用到autoIT这个工具 基于Au ...

  10. 20191003 尚硅谷Spring Cloud教学视频

    视频信息 视频日期:2018-4-19 讲师:尚硅谷周阳 Spring Cloud版本:Dalston.RELEASE 当前版本:Greenwich SR3 微服务.微服务架构.Spring Clou ...