Hdu 5439 Aggregated Counting (2015长春网络赛 ACM/ICPC Asia Regional Changchun Online 找规律)
题目链接:
题目描述:
刚开始给一个1,序列a是由a[i]个i组成,最后1就变成了1,2,2,3,3,4,4,4,5,5,5.......,最后问a[i]出现n次(i最大)时候,i最后一次出现的下标是多少?
解题思路:
问题可以转化为求a[i] == n (i最大),数列前i项的和为多少。
index: 1 2 3 4 5 6 7 8 9 10
a: 1 2 2 3 3 4 4 4 5 5
可以观察出:ans[1] = 1, ans[i] = ans[i-1] + a[i]*i;
但是n<=1e9,打不了这个庞大的表。进一步观察,可以发现a数组里面有很多的重复元素,辣么就可以对a数组里面的元素进行压缩存进b数组里面(出现次数为i的最后一个元素为b[i]):
index: 1 2 3 4 5 6 7 8 9 10
a: 1 2 2 3 3 4 4 4 5 5
b: 1 3 5 8 11 15 19 23 28 33
ans: 1 11 38 122 272 596 1086
1到出现次数为i的最后一个元素的区间和为ans[i],由a[]可以看出ans[i] = ans[i-1] + (b[i] + b[i-1] + 1) * (b[i] - b[i-1]) / 2 * i;
每次查询的时候如果n不在b[i]里面,可以找到一个最接近n并且小于n的b[i],然后再套用一次等差数列求和即可。
还有就是等差数列求和的时候,除以2要用到逆元处理一下。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef __int64 LL;
const int INF = 0x3f3f3f3f;
const int maxn = ;
const int mod = ;
LL a[maxn], b[maxn], ans[maxn], num; LL quick_mod(LL x, LL n)
{
LL res = ; while (n)
{
if (n % )
res = (res * x) % mod; x = (x * x) % mod;
n /= ;
}
return res % mod;
}
void init ()
{
LL res = , j = , nu; a[] = b[] = ;
a[] = b[] = ;
a[] = num = ;
b[] = ; while (b[num] <= 1e9)
{
if (res <= num)
{
j ++;
res += a[j];
} while (res > num)
{
num ++;
a[num] = j;
b[num] = b[num-] + a[num];
} } //printf ("%I64d %I64d\n", num, b[num]); ans[] = ;
ans[] = ; for (int i=; i<=num; i++)
ans [i] = (ans[i-] + (b[i] + b[i-] + ) % mod * a[i] % mod * i % mod * quick_mod(, mod-) % mod) % mod; } int main ()
{
LL t, n;
init ();
scanf ("%I64d", &t); while (t --)
{
scanf ("%I64d", &n);
LL pos = lower_bound (b, b+num, n) - b; if (b[pos] == n)
{
printf ("%I64d\n", ans[pos]);
continue;
} LL res = (ans[pos-] + (b[pos-] + n + ) % mod * (n - b[pos-]) % mod * pos % mod * quick_mod(, mod-) % mod) % mod; printf ("%I64d\n", res);
}
return ;
}
Hdu 5439 Aggregated Counting (2015长春网络赛 ACM/ICPC Asia Regional Changchun Online 找规律)的更多相关文章
- Hdu 5445 Food Problem (2015长春网络赛 ACM/ICPC Asia Regional Changchun Online)
题目链接: Hdu 5445 Food Problem 题目描述: 有n种甜点,每种都有三个属性(能量,空间,数目),有m辆卡车,每种都有是三个属性(空间,花费,数目).问至少运输p能量的甜点,花费 ...
- (并查集)Travel -- hdu -- 5441(2015 ACM/ICPC Asia Regional Changchun Online )
http://acm.hdu.edu.cn/showproblem.php?pid=5441 Travel Time Limit: 1500/1000 MS (Java/Others) Memo ...
- (二叉树)Elven Postman -- HDU -- 54444(2015 ACM/ICPC Asia Regional Changchun Online)
http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Time Limit: 1500/1000 MS (Java/Others) ...
- 2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】
Elven Postman Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- hdu 5444 Elven Postman(二叉树)——2015 ACM/ICPC Asia Regional Changchun Online
Problem Description Elves are very peculiar creatures. As we all know, they can live for a very long ...
- HDU 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)
Football Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- 2015 ACM/ICPC Asia Regional Changchun Online
1001 Alisha’s Party 比赛的时候学长stl吃T.手写堆过. 赛后我贴了那两份代码都过.相差.2s. 于是用stl写水果. # include <iostream> # i ...
- Aggregated Counting-----hdu5439(2015 长春网络赛 找规律)
#include<stdio.h> #include<string.h> #include<iostream> #include<math.h> #in ...
- hdu 5444 Elven Postman(根据先序遍历和中序遍历求后序遍历)2015 ACM/ICPC Asia Regional Changchun Online
很坑的一道题,读了半天才读懂题,手忙脚乱的写完(套上模板+修改模板),然后RE到死…… 题意: 题面上告诉了我们这是一棵二叉树,然后告诉了我们它的先序遍历,然后,没了……没了! 反复读题,终于在偶然间 ...
随机推荐
- TreeSet实现Comparator接口的排序算法的分析
为了方便,用lambda表达式代替comparator接口 例子如下: public static void main(String[] args) { TreeSet<Integer> ...
- group by where having 联合使用
having子句与where有相似之处但也有区别,都是设定条件的语句.在查询过程中聚合语句(sum,min,max,avg,count)要比having子句优先执行.而where子句在查询过程中执行优 ...
- springboot对传参的拦截统一处理
在学习某网<java秒杀系统方案优化>的课程中,学到了一种springboot对传参的拦截统一处理的方式,特记录一下. 如后台方法一般需要根据token从Session中获取User对象, ...
- DDD领域建模基本流程
整理一个精简的DDD领域建模基本流程,供大家在DDD领域建模实践中进行参考. 搜集用户故事(用户的原始需求) 整理用户故事,抽出用例(用例表达了用户对系统的需求,定义了系统的边界以及系统外部角色和系统 ...
- Fastreport生成WEB报表
开发WEB应用系统通常都会遇到报表打印问题.简单应用可利用IE的页面打印功能,利用HTML标签控制格式来实现.但复杂的业务型应用系统,报表不仅是组成应用的 重要部分,还常常是相当复杂的.现在很多应用系 ...
- Delphi通过Get获取来自PHP的返回值
Delphi代码 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Contro ...
- Silverlight中使用MVVM(1)
Silverlight中使用MVVM(1) Silverlight中使用MVVM(1)--基础 Silverlight中使用MVVM(2)—提高 Silverlight中使用MVVM(3)—进阶 ...
- LA-4726 (斜率优化+单调队列)
题意: 给定一个01序列,选一个长度至少为L 的连续子序列使其平均值最大;输出这个子序列的起点和终点;如果有多个答案,输出长度最小的,还有多个就输出第一个编号最小的; 思路: 用sum[i]表示[1, ...
- java类成员访问权限总结(private,default,protected,public)
- linux学习二(小随笔)
1apt-get 解包命令 tar zxvf ......... 打包命令 tar czvf ......... gz gunzip ........gz gzip ..........gz l ...