hdu 4655 Cut Pieces(想法题)

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 a i. So there are a total of prod(a i) 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 a i) 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, a 1, ..., a n.  
Sum of all n <= 10 6.   All numbers in the input are positive integers no larger than 10 9.
 

 

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 10 9+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次啦!!!

 

 

 1 #include<algorithm>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<iostream>
 5 #define MAX 1000010
 6 #define mod 1000000007
 7 using namespace std;
 8 typedef long long ll;
 9 
10 int ext_gcd(int a,int b,int &x,int &y)
11 {
12     if(b==0){x=1;y=0;return a;}
13     int d=ext_gcd(b,a%b,x,y),t;
14     t=x;x=y;y=t-a/b*y;
15     return d;
16 }
17 int inv(int a,int mo)
18 {
19     int x,y,dx,g;
20     g=ext_gcd(a,mo,x,y);
21     dx=mo/g;
22     return (x%dx+dx)%dx;
23 }
24 
25 int a[1000010];
26 int main()
27 {
28     int head,tail,T,i,n,jnm;
29     ll ji,ans;
30     scanf("%d",&T);
31     while(T--)
32     {
33         scanf("%d",&n);
34         ji=1;
35         for(i=1;i<=n;i++)
36         {
37             scanf("%d",&a[i]);
38             ji*=a[i];
39             ji%=mod;
40         }        
41         sort(a+1,a+n+1);
42         ans=ji;        
43         head=n-n/2+2;
44         tail=n;
45         if(n&1)//区分奇偶的区别!!!!
46             ans=((ans+2*(ji-ji*inv(a[head-1],mod)%mod))%mod+mod)%mod;
47         else
48             ans=((ans+1*(ji-ji*inv(a[head-1],mod)%mod))%mod+mod)%mod;
49         for(i=head;i<=tail;i++)
50             ans=((ans+2*(ji-ji*inv(a[i],mod)%mod))%mod+mod)%mod;
51         if(n==1)ans=a[1];
52         printf("%lld\n",ans);
53     }    
54     return 0;
55 }
View Code

 

转载于:https://www.cnblogs.com/skykill/p/3250131.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值