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. 多机并行计算框架 和CoolHash数据库(可用于多机机器学习)

    http://my.oschina.net/fourinone/blog/289122 http://www.oschina.net/p/fourinone

  2. [转] WebService开发笔记 1 -- 利用cxf开发WebService竟然如此简单

    以下文章来自   http://www.blogjava.net/jacally/articles/186655.html 现在的项目中需要用到SOA概念的地方越来越多,最近我接手的一个项目中就提出了 ...

  3. 建立一个属于自己的AVR的RTOS

    建立一个属于自己的AVR的RTOS(序) 建立一个属于自己的AVR的RTOS(第一篇:函数的运行) 建立一个属于自己的AVR的RTOS(第二篇:人工堆栈) 建立一个属于自己的AVR的RTOS(第三篇: ...

  4. (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  5. 【亲测】Python:解决方案:Python Version 2.7 required, which was not found in the registry

    好久不更新随笔了,今天因为数据可视化作业,想抓取一些人人网好友关系数据,于是开始尝试python,用到numpy模块,安装的时候提示: 'Python Version 2.7 required, wh ...

  6. net stop 出现1060错误

    net stop时出现1060错误,答案: 原来,net stop后面不能想当然地加上服务,而要在‘服务’里看看,那个服务究竟叫什么名字. 比如,我的mysql安装时不叫mysql,而是mysql5 ...

  7. matlab find函数

    find函数用来获取向量或矩阵中非0元素的索引 假设X是行向量,则find(X)返回的是一个行向量:X'为列向量,find(X')返回的是一个列向量. 看一个例子:A是一个行向量,B是一个列向量,将B ...

  8. iOS开发——沙箱

    iphone沙箱模型的有三个文件夹,documents,tmp,Library.有时开发时要求我们保存一些数据在本地,这就用到了. 1.Documents 目录:您应该将所有de应用程序数据文件写入到 ...

  9. R Student Companion(R语言初学指南)的源代码_数据_插图

    下载内容见附件:http://files.cnblogs.com/files/ml-cv/data_And_R_script.zip.

  10. 使用React Native来撰写跨平台的App

    React Native 是一个 JavaScript 的框架,用来撰写实时的.可原生呈现 iOS 和 Android 的应用.其是基于 React的,而 React 是 Facebook 的用于构建 ...