HDU 4655 Cut Pieces(2013多校6 1001题 简单数学题)
Cut Pieces
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 133 Accepted Submission(s): 62
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.
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.
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.
3
1 2 3
Both sequence 1 3 2 and sequence 2 3 1 result in an S of 14.
题目意思就是给了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题 简单数学题)的更多相关文章
- HDU 4696 Answers (2013多校10,1001题 )
Answers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total S ...
- HDU 4666 Hyperspace (2013多校7 1001题 最远曼哈顿距离)
Hyperspace Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Tota ...
- HDU 4643 GSM (2013多校5 1001题 计算几何)
GSM Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submiss ...
- 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 ...
- hdu 4655 Cut Pieces(想法题)
Cut Pieces Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Tota ...
- HDU 4705 Y (2013多校10,1010题,简单树形DP)
Y Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submiss ...
- HDU 4704 Sum (2013多校10,1009题)
Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submi ...
- HDU 4699 Editor (2013多校10,1004题)
Editor Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
- HDU 4678 Mine (2013多校8 1003题 博弈)
Mine Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submis ...
随机推荐
- Linux线程基础函数
1. 线程标识: (1) 比较两个线程ID: #include <pthread.h> int pthread_equal(pthread_t tid1, pthread_t tid2); ...
- gcc -rpath 指定动态库路径
gcc -rpath 指定动态库路径 http://blog.csdn.net/v6543210/article/details/44809405
- caffe Python API 之LRN
net.mylrn = caffe.layers.LRN(net.pool1,local_size=5,alpha=1e-4,beta=0.75) 输出: layer { name: "my ...
- API(选项/数据 选项/dom)
选项/数据 data 类型: Object | Function 限制: 组件的定义只接受function var data = { a: 1 } // 直接创建一个实例 var vm = new V ...
- P4819 [中山市选]杀人游戏
题目描述 一位冷血的杀手潜入Na-wiat,并假装成平民.警察希望能在NN个人里面,查出谁是杀手.警察能够对每一个人进行查证,假如查证的对象是平民,他会告诉警察,他认识的人,谁是杀手,谁是平民.假如查 ...
- 【JBPM4】任务节点-任务分配assignee
JPDL <process key="task" name="task" xmlns="http://jbpm.org/4.4/jpdl&quo ...
- PHP PDO类 单例
<?php /*//pdo连接信息 $pdo=array("mysql:host=localhost;dbname=demo;charset=utf8","root ...
- javascript高程笔记:逻辑与和逻辑或
逻辑与和或 逻辑与 当 && 前后两个操作数都是布尔值,无可厚非,同时为true才为true.与其他强类型语言不同的是,javascript逻辑与前后的操作数可以应用于任何类型. 而且 ...
- Java篇:IO流
流(IO) 四个基本流: InputStream(输入字节流).OutputStream(输出字节流).writer(输出字符流).reader(输入字符流)-四个基本流都是抽象类不能实例化,固只能用 ...
- gdbserver静态编译
redhat9 编译gdb server(静态编译)下载gdb-6.2a.tar:http://download.chinaunix.net/download.php?id=6680&Reso ...