zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

      

  • 相关阅读:
    [转]在自己的项目中调用别人的库的方法(static lib库,dynamic lib库以及dll动态库)
    前端、后台、客户端以及服务器
    C# 使用j
    C# Linq技术中SelectMany() 获取list对象的属性 汇总成为一个新的集合
    C# 集合的扩展方法-查询表达式GroupBy()的使用 转
    C# ASP.NET MVC中Filter过滤器的使用 通过注册到Global.asax注册为全局 过滤所有action
    C# ASP.NET MVC中有四种Filter 过滤器类型 通过标签 Attribute加到action上面
    C# 《Asp.Net Web Api 》-----路由机制 转
    C# ASP.NET MVC5路由系统机制详细讲解(转)
    MVC 自定义数据校验规则 Validation
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/4831440.html
Copyright © 2011-2022 走看看