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. 3D Computer Grapihcs Using OpenGL - 07 Passing Data from Vertex to Fragment Shader

    上节的最后我们实现了两个绿色的三角形,而绿色是直接在Fragment Shader中指定的. 这节我们将为这两个三角形进行更加自由的着色——五个顶点各自使用不同的颜色. 要实现这个目的,我们分两步进行 ...

  2. 170906-MyBatis续

    ===============================================Dynamic SQL========================================== ...

  3. 利用python进行数据分析--pandas入门2

    随书练习,第五章  pandas入门2 # coding: utf-8 # In[1]: from pandas import Series,DataFrame import pandas as pd ...

  4. Spring Boot 集成 JPA 的步骤

    Spring Boot 集成 JPA 的步骤 配置依赖 compile group: 'org.springframework.boot', name: 'spring-boot-starter-da ...

  5. 设计模式之动态代理(Java的JDK动态代理实现)

    先来看一下思维导图: 对于JDK的动态代理,孔浩老师说学习的方法是把它记下来. 先写一个主题接口类,表示要完成的一个主题. package com.liwei.dynaproxy; /** * 要代理 ...

  6. functional-page-navigator 组件

    functional-page-navigator 组件:是一个非常强大的组件,用于跳转插件的功能页 functional-page-navigator组件的属性: version:类型 字符串 跳转 ...

  7. 运行jar包shell脚本

    #!/bin/sh #该文件必须放在jar包的目录下,因为是以相对路径来运行的.不放jar包目录的话,可以直接在jar_name参数写绝对路径 #start 设置三个参数 #环境 profile=te ...

  8. Vultr CentOS下后台跑node

    在Mac或者Windows下简直易如反掌.几行命令搞定的事情,但因为使用的是远程SSH连接纯命令行处理,所以需要记录下来怎么弄. 比如, 1. 怎么在什么都没有的CentOS里下载Node安装包? 2 ...

  9. Delphi XE2 之 FireMonkey 入门(25) - 数据绑定: TBindingsList: 表达式的灵活性及表达式函数

    Delphi XE2 之 FireMonkey 入门(25) - 数据绑定: TBindingsList: 表达式的灵活性及表达式函数 绑定表达式中可以有简单的运算和字符串连接, 但字符串需放在双引号 ...

  10. Jmeter学习前的基本了解

    参考:九州八神的软件测试视频资料---来自于网络 jmeter基于java,跨平台的. 下载:官网http://jmeter.apache.org/         最好下载最新版,有一些新的功能.注 ...