#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[] = b[] = ;
a[] = ;
b[] = ;
for(i=; i<N; i++)
{
a[i] = lower_bound(b+, b+i, i) - b;
b[i] = b[i-] + a[i];
}
ans[] = ;
for(i=; i<N; i++)
{
ans[i] = ans[i-] + (b[i]-b[i-])*(b[i-]+ + b[i]) / % mod * i % mod;
}
} int main()
{
int T, n, i;
Init();
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
int pos = lower_bound(b+, b+N+, n) - b;
__int64 Ans = ans[pos-];
for(i=b[pos-]+; i<=n; i++)
Ans = (Ans + (__int64)i*pos) % mod;
printf("%I64d\n", Ans);
}
return ;
}

题目链接: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\n", 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\n", Ans);
}
return 0;
}

  

  

Aggregated Counting-----hdu5439(2015 长春网络赛 找规律)的更多相关文章

  1. Hdu 5439 Aggregated Counting (2015长春网络赛 ACM/ICPC Asia Regional Changchun Online 找规律)

    题目链接: Hdu 5439 Aggregated Counting 题目描述: 刚开始给一个1,序列a是由a[i]个i组成,最后1就变成了1,2,2,3,3,4,4,4,5,5,5.......,最 ...

  2. hdu 4731 2013成都赛区网络赛 找规律

    题意:找字串中最长回文串的最小值的串 m=2的时候暴力打表找规律,打表可以用二进制枚举

  3. 2015长春网络赛1001 求连通快数量的问题dfs

    Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Sub ...

  4. hdu 5441 (2015长春网络赛E题 带权并查集 )

    n个结点,m条边,权值是 从u到v所花的时间 ,每次询问会给一个时间,权值比 询问值小的边就可以走 从u到v 和从v到u算不同的两次 输出有多少种不同的走法(大概是这个意思吧)先把边的权值 从小到大排 ...

  5. hdu 5446(2015长春网络赛J题 Lucas定理+中国剩余定理)

    题意:M=p1*p2*...pk:求C(n,m)%M,pi小于10^5,n,m,M都是小于10^18. pi为质数 M不一定是质数 所以只能用Lucas定理求k次 C(n,m)%Pi最后会得到一个同余 ...

  6. Hdu 5445 Food Problem (2015长春网络赛 ACM/ICPC Asia Regional Changchun Online)

    题目链接: Hdu  5445 Food Problem 题目描述: 有n种甜点,每种都有三个属性(能量,空间,数目),有m辆卡车,每种都有是三个属性(空间,花费,数目).问至少运输p能量的甜点,花费 ...

  7. Supreme Number 2018沈阳icpc网络赛 找规律

    A prime number (or a prime) is a natural number greater than 11 that cannot be formed by multiplying ...

  8. HDU 4763 Theme Section (2013长春网络赛1005,KMP)

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  9. HDU 4764 Stone (2013长春网络赛,水博弈)

    Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

随机推荐

  1. 基于jQuery实现文字倾斜显示代码

    这是一款基于jQuery实现文字倾斜显示,这是一款基于jQuery实现的超酷动态文字显示效果.适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗. ...

  2. java web hello world(二)基于Servlet理解监听

    java web最开始实现是通过Servlet实现,这里就来实现下,最原始的监听是如何实现的. 第一步,创建一个基本的web项目 ,参见(java web hello world(一)) 第二步,we ...

  3. IDEA 中 右键新建时,没有新建class的解决方案

    今天在用IDEA新建包后,因为我新建的包名是userprivman-data这样的包,然后导致右键后没法新建类,并且在新建包名的时候就提示:不合法的包名,可能不会新建类 然后右键单击包名,发现没有新建 ...

  4. 详解 Go 语言中的 time.Duration 类型

    swardsman详解 Go 语言中的 time.Duration 类型swardsman · 2018-03-17 23:10:54 · 5448 次点击 · 预计阅读时间 5 分钟 · 31分钟之 ...

  5. 【转】crontab命令 脚本定时运行

    一.crond简介 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动cro ...

  6. 用 free 或 delete 释放了内存之后,立即将指针设置为 NULL,防止产 生“野指针”

    用 free 或 delete 释放了内存之后,立即将指针设置为 NULL,防止产 生“野指针”. #include <iostream> using namespace std; /* ...

  7. Effective C++ 条款26

    尽可能延后变量定义式的出现时间 我们知道定义一个对象的时候有一个不争的事实,那就是分配内存.假设是我们自己定义的对象.程序运行过程中会调用类的构造函数和析构函数. 我们打个例如,假设天下雨了,你带把雨 ...

  8. XML基础知识-->Spring配置

    XML的特殊字符 XML中共有5个特殊的字符,分别是:&<>“’.如果配置文件中的注入值包括这些特殊字符,就需要进行特别处理.有两种解决方法:其一,采用本例中的<![CDAT ...

  9. 常用快捷键—Webstorm入门指南

    提高代码编写效率,离不开快捷键的使用,Webstorm拥有丰富的代码快速编辑功能,你可以自由配置功能快捷键. 快捷键配置 点击“File”-> “settings” Webstorm预置了其他编 ...

  10. open() 函数以 w+ 模式打开文件

    这种模式打开文件时,会先清空文件,然后才执行读写操作,当我们要执行读操作时,需要通过 seek() 方法将读取指针移到前面,才能读取内容 [root@localhost ~]$ cat 1.txt # ...