Cut Pieces

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

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

题目意思就是给了n个数,排列下,使得所有段的和最大。

比如样例

1 3 2

有以下几种涂色:

1 1 1   = 1段

1 1 2   = 2段

1 2 1   = 3段

1 2 2   = 2段

1 3 1   = 3段

1 3 2   = 3段

所以答案就是14.

其实所有段的形成,都是相邻两个数不同导致的。

假设所有的数的乘积是 S

可以看出第一个数肯定每次都可以贡献,可以贡献  S 个

后面的数,假设a,b是相邻的,不妨设a<b   那么a b这个相邻的可以贡献(ab-a)*(S/ab) 。

也就是S-S/b

后面有n-1个相邻的数,而且前面一项的和就是 n*S.   后面一项要减掉n-1个S/bi

所以一个较大的数可以当两场相邻的中较大的。

所以前面比较大的数每个选2次,来-S/bi

这样就解决了。

取模和除法,可以使用逆元。

题解说也可以不用逆元,确实,只要记录下前缀和后缀乘积。

 /*
* Author: kuangbin
* Created Time: 2013/8/8 11:52:58
* File Name: 1001.cpp
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
using namespace std;
const int MOD = 1e9+; //求ax = 1( mod m) 的x值,就是逆元(0<a<m)
long long inv(long long a,long long m)
{
if(a == )return ;
return inv(m%a,m)*(m-m/a)%m;
}
const int MAXN = ;
int a[MAXN]; int main()
{
int T;
int n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
long long S = ;
for(int i = ;i < n;i++)
{
scanf("%d",&a[i]);
S *= a[i];
S %=MOD;
}
long long ans = S*n%MOD;
sort(a,a+n);
reverse(a,a+n);
int cnt = n-;
for(int i = ;i < n;i++)
{
if(cnt == )break;
long long tmp = S*inv(a[i],MOD)%MOD;
ans -= tmp;
ans = (ans%MOD+MOD)%MOD;
cnt--;
if(cnt == )break;
ans -= tmp;
ans = (ans%MOD+MOD)%MOD;
cnt--;
if(cnt == )break;
}
printf("%I64d\n",ans);
}
return ;
}

代码君

HDU 4655 Cut Pieces(2013多校6 1001题 简单数学题)的更多相关文章

  1. HDU 4696 Answers (2013多校10,1001题 )

    Answers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total S ...

  2. HDU 4666 Hyperspace (2013多校7 1001题 最远曼哈顿距离)

    Hyperspace Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  3. HDU 4643 GSM (2013多校5 1001题 计算几何)

    GSM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submiss ...

  4. HDU 4686 Arc of Dream (2013多校9 1001 题,矩阵)

    Arc of Dream Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  5. hdu 4655 Cut Pieces(想法题)

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

  6. HDU 4705 Y (2013多校10,1010题,简单树形DP)

    Y Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submiss ...

  7. HDU 4704 Sum (2013多校10,1009题)

    Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submi ...

  8. HDU 4699 Editor (2013多校10,1004题)

    Editor Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  9. HDU 4678 Mine (2013多校8 1003题 博弈)

    Mine Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submis ...

随机推荐

  1. python基础===codecs打开文件,解决文件编码格式的问题

    codecs https://docs.python.org/3/library/codecs.html 我们经常用open打开文件的时候会出现各式各样的错误,编码格式的问题,等等~真的很烦 现在尽量 ...

  2. 《LINUX3.0内核源代码分析》第二章:中断和异常 【转】

    转自:http://blog.chinaunix.net/uid-25845340-id-2982887.html 摘要:第二章主要讲述linux如何处理ARM cortex A9多核处理器的中断.异 ...

  3. 005 JAVA多线程和并发基础面试问答(转载)

    原文链接:http://ifeve.com/java-multi-threading-concurrency-interview-questions-with-answers/ 多线程和并发问题是Ja ...

  4. 2014ACM/ICPC亚洲区北京站题解

    本题解不包括个人觉得太水的题(J题本人偷懒没做). 个人觉得这场其实HDU-5116要比HDU-5118难,不过赛场情况似乎不是这样.怀疑是因为老司机带错了路. 这套题,个人感觉动态规划和数论是两个主 ...

  5. 004_ssh连接慢的问题的解决?

    <1>群中同学遇到的问题,我之前在uuwatch也遇到了同样的问题? 问个问题师兄们 突然之间 公司服务器连接很慢 连一个shell需要10几秒钟 服务器就在公司全是内网服务器, 我也不知 ...

  6. [LabVIEW架构]ActorFramework(一)

    前言 小黑结婚回来第二周了,每天忙于程序设计,时间比较紧张,所以文章一直没出来,也算憋大招了. 近期小黑将与大家一起认识一下ActorFramework,既是对自己一段时间写AF程序的总结,也是梳理, ...

  7. 在ubuntu上配置LAMP架构

    1. 安装MySQL /* ubuntu默认进入系统是普通用户 所以在真实工作中,我们会得到root的授权. 所以我们需要用sudo做一切只有root才能完成的操作. */ [root@LAMP ~] ...

  8. IDEA 内存设置

    -server -Xms2g -Xmx2g -XX:NewRatio=3 -Xss16m -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled - ...

  9. winscp上传出现时间戳提示错误

    文件ngx_http_access_module.c上传成功,但是在设置权限和/或时间戳时发生错误.具体内容上图:         我们可以选择 ‘中止’,文件是可以上传成功的,就是每次都会提示这个信 ...

  10. java中的抽象方法与抽象类

    在继承时,会遇到一个问题.如果很多子类都要继承父类的一个方法,但是实现的逻辑都不一样. 这时候父类只提供了方法名,但是没有具体的方法体. 例如,男孩类和女孩类都继承人类这一个父类.人类有爱好这个方法, ...