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

The shorter, the simpler. With this problem, you should be convinced of this truth.
  
  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

There are multiple test cases.
  
  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

For each query(l,r), output F(l,r) on one line.
 

Sample Input

1
3
2 3 3
1
1 3
 

Sample Output

2
 

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的更多相关文章

  1. HDU5875:Function

    题目链接: Function 分析: icpccamp里的方法不会,我用了一个nex[]数组存储当前点ai需要取模的下一个点aj的编号j,如果aj>ai,就不用遍历. 时间为920ms 代码: ...

  2. 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] ...

  3. HDU5875 Function

    题意:给定序列,有m个区间的询问,求每个询问a[l]%a[l+1]...%a[r]后的值.(N<=10^5) 思路:这题如果使用线段树,可能会由于姿势和卡常数原因TLE,由于数据好像比较奇怪(? ...

  4. 2016 ACM/ICPC Asia Regional Dalian Online

    1009 Sparse Graph(hdu5876) 由于每条边的权值都为1,所以最短路bfs就够了,只是要求转置图的最短路,所以得用两个set来维护,一个用来存储上次扩散还没访问的点,一个用来存储这 ...

随机推荐

  1. Delphi xe7并行编程快速入门(转)

    源:http://blog.csdn.net/henreash/article/details/41315183 现在多数设备.计算机都有多个CPU单元,即使是手机也是多核的.但要在开发中使用多核的优 ...

  2. hibernate--student_course_score

    学生, 课程,分数的设计. a)使用联合主键@EmbeddedId 使用Serializable接口 b)不适用联合主键 联合主键: 3张表, student: id, name course: id ...

  3. 微信小程序tips集合:无法输入文字/随时查看页面/元素审查/点击事件/数据绑定

    1:编辑文档无法输入文字 出现这种情况一般是因为之前编辑的文档未保存,所有在其他文档输入的时候会自动输入到未保存的文档中,在文档暂时编辑完毕后要ctrl+s随手保存,不然会出现无法打字情况 2: 随时 ...

  4. OC版二分查找

    二分查找(也称折半查找)是很常见的一种在数组中查找数据的算法,作为一名程序员是应该必须会的.它的基础思想:获取数组的中间值,将数组分割成两份,利用中间值跟指定的值进行比较,如果中间值大于指定的值,就在 ...

  5. java开发之提高java和mysql代码性能和质量

    0.if嵌套的层数最好不要超过3层 点击(此处)折叠或打开 import java.util.HashMap; import java.util.Map; public class Qiantao { ...

  6. Spark 的combineByKey函数

    在Spark中有许多聚类操作是基于combineByKey的,例如group那个家族的操作等.所以combineByKey这个函数也是比较重要,所以下午花了点时间看来下这个函数.也参考了http:// ...

  7. iOS 键盘类型UIKeyboardType

    UIKeyboardTypeDefault: UIKeyboardTypeASCIICapable: UIKeyboardTypeNumbersAndPunctuation: UIKeyboardTy ...

  8. Android studio中添加外部的jar包

    1.下载相关的jar包, 2,将jar包复制到当前module的lib中 3.选择新添加的jar包.右键.add as library 就好啦

  9. jqGrid添加自定义按钮

    用法: <script> ... jQuery("#grid_id").navGrid("#pager",...).navButtonAdd(&qu ...

  10. MapReduce 表连接

    题目描述: 根据给定的关系 child parent Tom Lucy Tom Jack Jone Lucy Jone Jack Lucy Mary Lucy Ben Jack Alice Jack ...