HDU5875
Function
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 399 Accepted Submission(s): 151
Problem Description
You are given an array A of N postive integers, and M queries in the form (l,r). A function F(l,r) (1≤l≤r≤N) is defined as:
F(l,r)={AlF(l,r−1) modArl=r;l<r.
You job is to calculate F(l,r), for each query (l,r).
Input
The first line of input contains a integer T, indicating number of test cases, and T test cases follow.
For each test case, the first line contains an integer N(1≤N≤100000).
The second line contains N space-separated positive integers: A1,…,AN (0≤Ai≤109).
The third line contains an integer M denoting the number of queries.
The following M lines each contain two integers l,r (1≤l≤r≤N), representing a query.
Output
Sample Input
Sample Output
Source
//2016.9.11
#include <iostream>
#include <cstdio>
#include <cstring>
#define N 100005 using namespace std; int a[N], nex[N];//nex数组,表示跳到下一个要取余的位置,比a[i]大的数不用取余,此处优化降低时间 int main()
{
int T, n, q, ans;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = ; i <= n; i++)
{
scanf("%d", &a[i]);
}
scanf("%d", &q);
int l, r;
for(int i = ; i <= n; i++)
{
nex[i] = -;
for(int j = i+; j <= n; j++)
if(a[i]>=a[j])
{
nex[i] = j;
break;
}
}
while(q--)
{
scanf("%d%d", &l, &r);
ans = a[l];
for(int i = nex[l]; i <= r; i = nex[i])
{
if(i == -)break;
ans %= a[i];
}
printf("%d\n", ans);
}
} return ;
}
HDU5875的更多相关文章
- HDU5875:Function
题目链接: Function 分析: icpccamp里的方法不会,我用了一个nex[]数组存储当前点ai需要取模的下一个点aj的编号j,如果aj>ai,就不用遍历. 时间为920ms 代码: ...
- hdu-5875
题目大意: f(l,r)=a[l] l==r f(l,r)=f(l,r-1)%a[r] l<r 思路: 由此可以推出f(l,r)=a[l]%a[l+1]%a[l+2]%....%a[r] ...
- HDU5875 Function
题意:给定序列,有m个区间的询问,求每个询问a[l]%a[l+1]...%a[r]后的值.(N<=10^5) 思路:这题如果使用线段树,可能会由于姿势和卡常数原因TLE,由于数据好像比较奇怪(? ...
- 2016 ACM/ICPC Asia Regional Dalian Online
1009 Sparse Graph(hdu5876) 由于每条边的权值都为1,所以最短路bfs就够了,只是要求转置图的最短路,所以得用两个set来维护,一个用来存储上次扩散还没访问的点,一个用来存储这 ...
随机推荐
- IFields Interface 定义一个字段集合对象
Description The Fields object represents a collection of columns in a table. The term field is synon ...
- VS+VA 开发NDK
工欲善其事,必先利其器. Android NDK开发环境,可选择VIM+插件.Eclipse+CDT等,这里介绍另一种选择:VS+VA 软件准备:Visual studio 2008 // 其他版本也 ...
- 2016"百度之星" - 资格赛(Astar Round1) Problem A
保存前缀乘,询问的时候输出c[ri]/c[li-1]即可,因为是除法,所以计算一下c[li-1]的逆元. #include <stdio.h> #include <math.h> ...
- 微信小程序实例教程(四)
第八章:微信小程序分组开发与左滑功能实现 先来看看今天的整体思路: 进入分组管理页面 --> 点击新建分组新建 进入到未分组页面基本操作 进入到已建分组里面底部菜单栏操作 --> 从名 ...
- mousewheel,DOMMouseScroll判断滚轮滚动方向
firefox使用DOMMouseScroll,其他浏览器使用mousewheel 首先绑定一个滚动事件 //firefox使用DOMMouseScroll,其他浏览器使用mousewheel$(do ...
- VS2010与SVN
http://blog.sina.com.cn/s/blog_4fe44775010182yl.html 在VS2010中使用SVN,必须先安装SVN的客户端,再安装VisualSVN(SVN的插件) ...
- 一次性能优化,tps从400+到4k+
项目介绍 路由网关项目watchman ,接收前端http请求转发到后端业务系统,功能安全验证,限流,转发. 使用技术:spring boot+ nreflix zuul,最开始日志使用slf4j+l ...
- UVa 124 - Following Orders
题目大意:给一个变量列表和变量的大小关系,输出所有的满足约束的序列. 构建为有向图,然后就是拓扑排序,使用回溯输出所有的结果. #include <cstdio> #include < ...
- Backbone视图渲染React组件
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...
- discuz开发学习
2014年3月24日 10:36:10 遇到一个问题,discuz 缓存的样式,没有自动生成.后来去后台 进行操作才有效. 解决了之前的遇到的 首页没有套用样式的问题. 现在的问题是 模版的扩展图片 ...