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到死…… 题意: 题面上告诉了我们这是一棵二叉树,然后告诉了我们它的先序遍历,然后,没了……没了! 反复读题,终于在偶然间 ...
随机推荐
- uboot 对 FAT 分区的解析
uboot 对 FAT 分区的解析 改写 UBOOT 从 U 盘读入固件,然后刷机.发现有的 U 盘无法正确读到分区,跟踪了一下发现自己写的代码有漏洞,只尝试解析分区表里的第一个分区.跟踪的过程中重温 ...
- Hero In Maze
Hero In Maze 时间限制(普通/Java):1000MS/10000MS 执行内存限制:65536KByte 描写叙述 500年前,Jesse是我国最卓越的剑客. 他英俊潇 ...
- UIPageControll 的属性和用法
UIPageControll 是继承于UIControl的一个IOS系统UI控件,可以提供给开发者设计分页效果的功能. 初始化方法 UIPageControl * page = [[UIPageCon ...
- redis05----Redis 中的事务
Redis支持简单的事务 Redis与 mysql事务的对比 Mysql Redis 开启 start transaction multi 语句 普通sql 普通命令 失败 rollback 回滚 d ...
- hihocoder 1015 KMP(找多个位置的 【*模板】)
#1015 : KMP算法 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在 ...
- POJ2243 Knight Moves —— A*算法
题目链接:http://poj.org/problem?id=2243 Knight Moves Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- 序列流、对象操作流、打印流、标准输入输出流、随机访问流、数据输入输出流、Properties(二十二)
1.序列流 * 1.什么是序列流 * 序列流可以把多个字节输入流整合成一个, 从序列流中读取数据时, 将从被整合的第一个流开始读, 读完一个之后继续读第二个, 以此类推.* 2.使用方式 * 整合两个 ...
- Oracle:通过pl/sql developer工具导入excel数据
1.在pl/sql developer中选择工具-->ODBC导入器 2.选择需要导入的EXCEL文件(CVS也可以):用户名.口令不用管,直接点“连接”,找到要导入的xls文件 3. 选择“导 ...
- Linux系统CentOS下mysql的安装日志
今天自己捣鼓了一下,在linux系统CentOs6.5下使用源码方式安装和配置mysql,这里记录一下步骤. a) 下载mysql,source版本.Mysql-5.6.20.tar.gz b) 安装 ...
- codeforces 673B B. Problems for Round(模拟)
题目链接: B. Problems for Round time limit per test 2 seconds memory limit per test 256 megabytes input ...