Aggregated Counting-----hdu5439(2015 长春网络赛 找规律)

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define N 500000
#define mod 1000000007

__int64 a[N], b[N], ans[N];

void Init()
{
    int i;
    a[1] = b[1] = 1;
    a[2] = 2;
    b[2] = 3;
    for(i=1; i<N; i++)
    {
        a[i] = lower_bound(b+1, b+i, i) - b;
        b[i] = b[i-1] + a[i];
    }
    ans[1] = 1;
    for(i=2; i<N; i++)
    {
        ans[i] = ans[i-1] + (b[i]-b[i-1])*(b[i-1]+1 + b[i]) / 2 % mod * i % mod;
    }
}

int main()
{
    int T, n, i;
    Init();
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);
        int pos = lower_bound(b+1, b+N+1, n) - b;
        __int64 Ans = ans[pos-1];
        for(i=b[pos-1]+1; i<=n; i++)
            Ans = (Ans + (__int64)i*pos) % mod;
        printf("%I64d
", Ans);
    }
    return 0;
}
View Code

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5439

函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val第一个元素位置。如果所有元素都小于val,则返回last的位置

pos = lower_bound(a, a+n, k) - a;  是在a中>=k的第一个数的位置;

题意:刚开始给一个1,序列a是由a[i]个i组成,最后就变成了1,2,2,3,3,4,4,4,5,5,5.......

就是求Ans: 即n在a中最后出现的位置m,m在a中的最后出现的位置Ans;

我们用b【i】表示i在a中出现的最后位置;

用ans【i】表示b【i】在a中出现的最后位置;

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define N 500000
#define mod 1000000007

__int64 a[N], b[N], ans[N];

void Init()
{
    int i;
    a[1] = b[1] = 1;
    a[2] = 2;
    b[2] = 3;
    for(i=1; i<N; i++)
    {
        a[i] = lower_bound(b+1, b+i, i) - b;
        b[i] = b[i-1] + a[i];
    }
    ans[1] = 1;
    for(i=2; i<N; i++)
    {
        ans[i] = ans[i-1] + (b[i]-b[i-1])*(b[i-1]+1 + b[i]) / 2 % mod * i % mod;
    }
}

int main()
{
    int T, n, i;
    Init();
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);
        int pos = lower_bound(b+1, b+N+1, n) - b;
        __int64 Ans = ans[pos-1];
        for(i=b[pos-1]+1; i<=n; i++)
            Ans = (Ans + (__int64)i*pos) % mod;
        printf("%I64d
", Ans);
    }
    return 0;
}
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define N 500000
#define mod 1000000007

__int64 a[N], b[N], ans[N];

void Init()
{
    int i;
    a[1] = b[1] = 1;
    a[2] = 2;
    b[2] = 3;
    for(i=1; i<N; i++)
    {
        a[i] = lower_bound(b+1, b+i, i) - b;
        b[i] = b[i-1] + a[i];
    }
    ans[1] = 1;
    for(i=2; i<N; i++)
    {
        ans[i] = ans[i-1] + (b[i]-b[i-1])*(b[i-1]+1 + b[i]) / 2 % mod * i % mod;
    }
}

int main()
{
    int T, n;
    Init();
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);
        int pos = upper_bound(b+1, b+N+1, n) - b - 1;
        __int64 Ans = (ans[pos] + (b[pos] + 1 + n)*(n - b[pos])/2 % mod * (pos+1)) % mod;
        printf("%I64d
", Ans);
    }
    return 0;
}

  

  

原文地址:https://www.cnblogs.com/zhengguiping--9876/p/4831440.html