BestCoder Round #76

此处有目录↑

DZY Loves Balls (枚举)

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Problem Description
DZY loves playing balls.

He has  n  balls in a big box. On each ball there is an integer written.

One day he decides to pick two balls from the box. First he randomly picks a ball from the box, and names it  A . Next, without putting  A  back into the box, he randomly picks another ball from the box, and names it  B .

If the number written on  A  is strictly greater than the number on  B , he will feel happy.

Now you are given the numbers on each ball. Please calculate the probability that he feels happy.
 

Input
First line contains  t  denoting the number of testcases.

t  testcases follow. In each testcase, first line contains  n , second line contains  n  space-separated positive integers  ai , denoting the numbers on the balls.

( 1t300,2n300,1ai300 )
 

Output
For each testcase, output a real number with 6 decimal places. 
 

Sample Input
   
   
2 3 1 2 3 3 100 100 100
 

Sample Output
   
   
0.500000 0.000000

题目大意:一个盒子中有n个球,每个球上有一个整数,第一次抽取球A,不放回再抽取球B,球A上数字大于球B上数字的概率。

大致思路:排序后,枚举第一次抽取的球A,设球上数字小于球A 上数字的球的个数为x,则ans+=(1.0/n)*(x/(n-1));

#include <cstdio>
#include <algorithm>

using namespace std;

int T,n,a[305];
double ans,tmp;

int main() {
    scanf("%d",&T);
    while(T--) {
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
            scanf("%d",a+i);
        sort(a+1,a+1+n);
        ans=0;
        tmp=1.0/(n*(n-1));
        for(int i=2,j=(a[1]==a[2]?0:1);i<=n;++i) {//枚举第一次选第i个数
            ans+=tmp*j;
            if(a[i+1]!=a[i])
                j=i;
        }
        printf("%.6lf\n",ans);
    }
}


报完名就在网上浪,等意识到比赛开始时已经了很久,要不然这回能少掉点 - -

——————————————————1题的旅游分割线———————————————————

DZY Loves Partition (数学)

http://acm.hdu.edu.cn/showproblem.php?pid=5646

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)


Problem Description
DZY loves partitioning numbers. He wants to know whether it is possible to partition  n  into the sum of exactly  k  distinct positive integers.

After some thinking he finds this problem is Too Simple. So he decides to maximize the product of these  k  numbers. Can you help him?

The answer may be large. Please output it modulo  109+7 .
 

Input
First line contains  t  denoting the number of testcases.

t  testcases follow. Each testcase contains two positive integers  n,k  in a line.

( 1t50,2n,k109 )
 

Output
For each testcase, if such partition does not exist, please output  1 . Otherwise output the maximum product mudulo  109+7 .
 

Sample Input
  
  
4 3 4 3 2 9 3 666666 2
 

Sample Output
  
  
-1 2 24 110888111

Hint

In 1st testcase, there is no valid partition.

In 2nd testcase, the partition is 3=1+2. Answer is 1*2 = 2.

In 3rd testcase, the partition is 9=2+3+4. Answer is 2*3*4=24. Note that 9=3+3+3 is not a valid partition, because it has repetition.

In 4th testcase, the partition is 666666=333332+333334. Answer is 333332*333334= 111110888888. Remember to output it mudulo 10^9 + 7, which is 110888111.

题目大意:将n分成k个不相同的正整数,求这k个不同正整数的积的最大值 模10^9+7,若n不能被分成k个不相同的正整数,则输出-1

随便列了几组,便发现所有答案 都是一段连续相同的数或两段连续相同的数 的乘积,便猜测正解应该就是这种形式,但一直卡在如何构造这两段连续相同的数,最后有思路时发现没时间了...

官方这个解法

sum(a,k) = a + (a+1) + \cdots + (a+k-1)sum(a,k)=a+(a+1)++(a+k1)

首先,有解的充要条件是sum(1,k) \le nsum(1,k)n(如果没取到等号的话把最后一个kk扩大就能得到合法解)。

然后观察最优解的性质,它一定是一段连续数字,或者两段连续数字中间只间隔1个数。这是因为1\le a<=b-21a<=b2时有ab<(a+1)(b-1)ab<(a+1)(b1),如果没有满足上述条件的话,我们总可以把最左边那段的最右一个数字作为aa,最右边那段的最左一个数字作为bb,调整使得乘积更大。

可以发现这个条件能够唯一确定nn的划分,只要用除法算出唯一的aa使得sum(a,k)\le n < sum(a+1,k)sum(a,k)n<sum(a+1,k)就可以得到首项了。

时间复杂度O(\sqrt {n} )O(n),这是暴力把每项乘起来的复杂度。

看不懂,数学太烂了 :(

现在已经弱到div2都不能AC 2题了 :(

#include <cstdio>

using namespace std;

const long long mod=1e9+7;

int main() {
    int T,i,j;
    long long n,k,sum,div,mo,ans;
    scanf("%d",&T);
    while(T--) {
        scanf("%I64d%I64d",&n,&k);
        sum=k*(k+1)/2;//假设这k个数从小到大为1~k
        if(sum>n) {
            printf("-1\n");
            continue;
        }
        ans=1;
        sum=n-sum;
        div=sum/k;//剩余的sum给这k个数都可以分得div
        mo=sum%k;//最后剩下的mo给后mo个数都分1
        for(i=0,j=k;i<mo;--j,++i)//后mo个数
            ans=(ans*(j+div+1))%mod;
        for(;j>0;--j)//前面k-mo个数
            ans=(ans*(j+div))%mod;
        printf("%I64d\n",ans);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值