【洛谷1291】百事世界杯之旅

题意:https://www.luogu.org/problem/show?pid=1291
题解:
假设当前已经得到k个名字,得到第k+1个名字的期望为 nnk , 所以易得从第一个名字到最后一个名字的平均次数就是 ni=1ni , 原来这东西就叫做调和级数啊hhh(久仰大名)
PS:这道题的输出我给满分

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;

int n;

LL gcd(LL a,LL b)
{
    return b?gcd(b,a%b):a;
}

int main()
{
    scanf("%d",&n);
    LL A=0,B=1;
    for(int i=1;i<=n;i++)
    {
        A=A*i+B*n;//A/B+n/i=ai+bn/bi
        B=B*i;
        LL g=gcd(A,B);
        A/=g;
        B/=g;       
    }
    if(B==1) printf("%lld\n",A);
    else
    {
        LL ret=A/B,c=B,rcnt=0,ccnt=0,r=ret;
        while(r) r/=10,rcnt++;
        while(c) c/=10,ccnt++;
        for(int i=1;i<=rcnt;i++) cout<<" ";
        cout<<A-B*ret<<endl<<ret;
        for(int i=1;i<=ccnt;i++) cout<<"-";
        cout<<endl;
        for(int i=1;i<=rcnt;i++) cout<<" ";
        cout<<B<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值