码迷,mamicode.com
首页 > 移动开发 > 详细

Apple - Hdu5160

时间:2016-05-25 18:08:35      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:

Problem Description
We are going to distribute apples to n children. Every child has his/her desired number of apple A1,A2,A3,?AnAi indicates the i-th child’s desired number of apple. Those children should stand in a line randomly before we distribute apples. Assume that the i-th position is occupied by pith child, i.e. from left to right the id of the children are p1,p2,p3,?pn ,So their desired number of apple are Ap1,Ap2,Ap3,?Apn . We will distribute Ap1 apples to the left most child,and for the i-th (i>1)child,if there exists a j which makes j < i and Apj>Api true, we will distribute no apples to him/her,otherwise we will distribute Api apples to him/ner. So for a certain permutation there exist a certain number of apples we should distribute. Now we want to know how many apples we should distribute for all possible permutation.
Note: two permutations are considered different if and only if there exists at least a position in which two children’s desired number of apple are different.
 

 

Input
Multi test cases,the first line contains an integer T which indicates the number of test cases. Then every case occupies two lines.
For each case, the first line contains an integer n which indicates there are n children.
The second line contain n integers A1,A2,A3,?An indicate n children’s desired number of apple.
[Technique specification]
All numbers are integer
1<=T<=20
1<=n<=100000
1<=Ai<=1000000000
 

 

Output
For each case,output occupies one line,the output format is Case #x: ans, x is the data number starting from 1,ans is the total number of apple modular 1000000007.
 

 

Sample Input
2 2 2 3 3 1 1 2
 

 

Sample Output
Case #1: 8 Case #2: 9
Hint
For the second case, all possible permutation and corresponding distributed apples are (1,1,2),4 (1,2,1),3 (2,1,1),2 So the total number of apple is 2+3+4=9
 

 

Source
 

 

Recommend
heyang
 
简单题意
给你一堆数字,要计算所有不同排列的权值(两个排列为不一样的排列当且仅当至少有一个位置上的数字不同)
权值的计算方法:对于每个位置上的数字,要是这个数字之前不存在比这个数字大的数字,权值就加上这个数字
胡说题解
先排序,然后我们对于每一种数字,我们枚举有多少个被计算到权值里面,然后排列组合出这种情况的不同排列个数
技术分享
 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 const long long p=1e9+7;
 6 long long a[100100],fac[100100],ans;
 7 int n,t;
 8 
 9 long long power(long long a,long long b){
10     if(a<=1)return a;
11     if(b==0)return 1;
12     if(b==1)return a;
13     long long tmp=power(a,b>>1);
14     tmp=(tmp*tmp)%p;
15     if(b&1==1)tmp=(tmp*a)%p;
16     return tmp;
17 }
18 
19 long long C(int m,int n){
20     if(m==0)return 1;
21     if(n<0)return 0;
22     long long tmp=(fac[n]*power(fac[m],p-2))%p;
23     tmp=(tmp*power(fac[n-m],p-2))%p;
24     return tmp;
25 }
26 
27 int main(){
28     scanf("%d",&t);
29     int i,j,s,k,l;
30     fac[0]=1;
31     for(i=1;i<=100000;i++)fac[i]=(fac[i-1]*i)%p;
32     for(l=1;l<=t;l++){
33         ans=0;
34         scanf("%d",&n);
35         for(i=1;i<=n;i++)scanf("%I64d",&a[i]);
36         a[n+1]=0;
37         sort(a+1,a+1+n);
38         s=k=0;
39         long long div=1;
40         for(i=1;i<=n;i++){
41             ++k;
42             if(a[i]!=a[i+1]){
43                 long long tmp,ss=0;
44                 tmp=(fac[s]*fac[k])%p;
45                 tmp=(tmp*fac[n-s-k])%p;
46                 tmp=(tmp*C(s,n))%p;
47                 for(j=1;j<=k;j++)ss+=(((a[i]*j)%p)*C(k-j,n-s-j-1))%p;
48                 ss%=p;
49                 ans+=(ss*tmp)%p;
50                 ans%=p;
51                 div=(div*fac[k])%p;
52                 s+=k;
53                 k=0;
54             }
55         }
56         ans=(ans*power(div,p-2))%p;
57         printf("Case #%d: %I64d\n",l,ans);
58     }
59     return 0;
60 }
AC代码

 

Apple - Hdu5160

标签:

原文地址:http://www.cnblogs.com/Randolph87/p/5199439.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!