hdu 5439 Aggregated Counting

解题思路:找公式,预处理,二分
这里写图片描述
b[n]中存储i的最后的位置,n在 b[] 中的位置,代表其在 f(f(n)) 中的系数
eg: n=3 f(n)=5, f(f(n))=1+ (2+3)*2

  #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int maxn=500000;
ll b[maxn+10];
ll a[maxn+10];
ll ans[maxn+10];
/*
    序列             a=1 2 2 3 3 4 4 4 5 5 5 6 6 6 6 .....
    最后位置 f(n):pos=1   3   5     8    11         .....
               f(f(n))=1+ (2+3)*2 +(4+5)*3 +(6+7+8)*4 .....
               b[n]中存储i的最后的位置,n在b[]中的位置,代表其在f(f(n))中的系数
               eg: n=3 f(n)=5, f(f(n))=1+ (2+3)*2
*/
void init()
{
    a[0]=0,a[1]=1,a[2]=2,a[3]=2;    ///序列第i位上的数
    int cnt=0;
    int x=3;
    for(int i=3;;i++)
    {
        cnt=a[i];
        while(cnt)
        {
            a[++x]=i;
            cnt--;
            if(x>maxn)
                break;
        }
        if(x>maxn)break;
    }

    b[0]=0;
    b[1]=1,b[2]=3;
    for(int i=3;i<=maxn;i++)    ///i的最后的位置
    {
        b[i]=b[i-1]+a[i];
    }
    ans[0]=0;
    ans[1]=1;
    for(int i=2;i<maxn;i++)
    {
        ll l=b[i-1]+1,r=b[i];
        ans[i]=(ans[i-1]+((l+r)*(r-l+1)/2%mod*i)%mod)%mod;
    }
}
int main()
{
    init();
    int T;
    scanf("%d",&T);
    ll n;
    while(T--)
    {
        scanf("%I64d",&n);
        ll res;
        ll tmp=lower_bound(b+1,b+maxn,n)-b;
        ll l=b[tmp-1]+1,r=n;
        res=(ans[tmp-1]+((l+r)*(r-l+1)/2%mod*tmp)%mod)%mod;
        printf("%I64d\n",res);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值