博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Apple - Hdu5160
阅读量:4316 次
发布时间:2019-06-06

本文共 3611 字,大约阅读时间需要 12 分钟。

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
2 #include
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代码

 

转载于:https://www.cnblogs.com/Randolph87/p/5199439.html

你可能感兴趣的文章
zprofiler三板斧解决cpu占用率过高问题(转载)
查看>>
php替换url参数实现商品筛选效果
查看>>
Java 多线程_优先级
查看>>
js实现图片上传及预览---------------------->>兼容ie6-8 火狐以及谷歌
查看>>
WCF4.0 –- RESTful WCF Services (1) (入门)
查看>>
开源管理系统OSSIM设置 语言为中文简体
查看>>
解决winform中mdi子窗体加载时显示最大化最小化按钮的方法
查看>>
Matlab 与 c++对txt 文档的读写格式
查看>>
ATITIT.翻译模块的设计与实现 api attilax 总结
查看>>
Posix消息队列实现机制
查看>>
win8/8.1 免密码登录设置
查看>>
Flask实战第53天:cms编辑轮播图功能完成
查看>>
Android相关的ADB命令
查看>>
c语言typedef关键字的理解
查看>>
vue click事件获取当前元素属性
查看>>
Netty与网络编程
查看>>
mybatis查询语句的背后之参数解析
查看>>
Hadoop工程师面试题(1)--MapReduce实现单表汇总统计
查看>>
如何使用Windows Library文件进行持久化
查看>>
查看和调试Qt源码(动态编译的QT也可进入源码)good
查看>>