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. RedisTemplate访问Redis数据结构(四)——Set

    Redis的Set是string类型的无序集合.集合成员是唯一的,这就意味着集合中不能出现重复的数据,Redis 中 集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1). SetOper ...

  2. 通过java反射机制,修改年龄字段的值

    需求:将生日转为年龄 /** * 获取年龄值 */ public List getAgeInfo(List list) throws Exception { if (null == list || l ...

  3. A* 算法求第 K 短路

    一种具有 \(f(n)=g(n)+h(n)\) 策略的启发式算法能成为 A* 算法的充分条件是: 搜索树上存在着从起始点到终了点的最优路径. 问题域是有限的. 所有结点的子结点的搜索代价值 \(> ...

  4. python 调用C++ DLL,传递int,char,char*,数组和多维数组

    ctypes 数据类型和 C数据类型 对照表 ctypes type C type Python type c_bool _Bool bool (1) c_char char 1-character ...

  5. 十八、浏览器不能打开jenkins报告,报错Opening Robot Framework report failed

    解决方案一:(推荐) 打开jenkins----系统管理---输入:  System.setProperty("hudson.model.DirectoryBrowserSupport.CS ...

  6. java实现多种加密模式的AES算法-总有一种你用的着

    https://blog.csdn.net/u013871100/article/details/80100992 AES-128位-无向量-ECB/PKCS7Padding package com. ...

  7. Map 接口有哪些类

    Map接口 Map提供了一种映射关系,其中的元素是以键值对(key-value)的形式存储的,能够实现根据key快速查找value:Map中的键值对以Entry类型的对象实例形式存在:建(key值)不 ...

  8. Linux_FTP服务器

    目录 目录 FTP FTP Server FTP configuration Global config Anonymous user FTP Config Virtual user FTP Loca ...

  9. PAT 1001 A+B Format (20 point(s))

    题目: 我一开始的思路是: 用math.h中的log10函数来计算位数(不建议这么做,因为会很慢,而且会出一点别的问题): 用pow函数根据要插入分号的位置来拆分a+b成一个个数字(例如res / p ...

  10. pycharm社区版安装及遇到的问题

    1. 在官网上下载pycharm社区版安装包. 2. 按照该教程进行安装: https://jingyan.baidu.com/article/f00622286e92f4fbd2f0c855.htm ...