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


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

这样就解决了。

 

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

 

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

 

 1 /*
 2  * Author:  kuangbin
 3  * Created Time:  2013/8/8 11:52:58
 4  * File Name: 1001.cpp
 5  */
 6 #include <iostream>
 7 #include <cstdio>
 8 #include <cstdlib>
 9 #include <cstring>
10 #include <cmath>
11 #include <algorithm>
12 #include <string>
13 #include <vector>
14 #include <stack>
15 #include <queue>
16 #include <set>
17 #include <time.h>
18 using namespace std;
19 const int MOD = 1e9+7;
20 
21 //求ax = 1( mod m) 的x值,就是逆元(0<a<m)
22 long long inv(long long a,long long m)
23 {
24     if(a == 1)return 1;
25     return inv(m%a,m)*(m-m/a)%m;
26 }
27 const int MAXN = 1000010;
28 int a[MAXN];
29 
30 int main()
31 {
32     int T;
33     int n;
34     scanf("%d",&T);
35     while(T--)
36     {
37         scanf("%d",&n);
38         long long S = 1;
39         for(int i = 0;i < n;i++)
40         {
41             scanf("%d",&a[i]);
42             S *= a[i];
43             S %=MOD;
44         }
45         long long ans = S*n%MOD;
46         sort(a,a+n);
47         reverse(a,a+n);
48         int cnt = n-1;
49         for(int i = 0;i < n;i++)
50         {
51             if(cnt == 0)break;
52             long long tmp = S*inv(a[i],MOD)%MOD;
53             ans -= tmp;
54             ans = (ans%MOD+MOD)%MOD;
55             cnt--;
56             if(cnt == 0)break;
57             ans -= tmp;
58             ans = (ans%MOD+MOD)%MOD;
59             cnt--;
60             if(cnt == 0)break;
61         }
62         printf("%I64d\n",ans);
63     }
64     return 0;
65 }
代码君

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2013-08-08 19:15  kuangbin  阅读(928)  评论(1编辑  收藏  举报

导航

JAVASCRIPT: