此处有目录↑
DZY Loves 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.
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.
( 1≤t≤300,2≤n≤300,1≤ai≤300 )
2 3 1 2 3 3 100 100 100
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
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 .
t testcases follow. Each testcase contains two positive integers n,k in a line.
( 1≤t≤50,2≤n,k≤109 )
4 3 4 3 2 9 3 666666 2
-1 2 24 110888111
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)+⋯+(a+k−1)。
首先,有解的充要条件是sum(1,k)≤n(如果没取到等号的话把最后一个k扩大就能得到合法解)。
然后观察最优解的性质,它一定是一段连续数字,或者两段连续数字中间只间隔1个数。这是因为1≤a<=b−2时有ab<(a+1)(b−1),如果没有满足上述条件的话,我们总可以把最左边那段的最右一个数字作为a,最右边那段的最左一个数字作为b,调整使得乘积更大。
可以发现这个条件能够唯一确定n的划分,只要用除法算出唯一的a使得sum(a,k)≤n<sum(a+1,k)就可以得到首项了。
时间复杂度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);
}
}