Function

Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1095    Accepted Submission(s): 420

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
 
 
 
解析:题意为对于每个询问区间[l, r],求a[l]%a[l+1]%a[l+2]%...%a[r]。因为当后面的数比前面的数大时,取模得到的结果没有变化。当后面的数比前面的数小或相等时,取模得到的结果才有变化,并且结果越来越小。因此可以进行预处理得到每个数第一个不大于这个数的位置,每次取模时直接跳转到此位置进行。
 
 
 
#include <cstdio>
#include <cstring> const int MAXN = 1e5+5;
int a[MAXN];
int next[MAXN]; //第一个小于等于这个数的位置
int n; void solve()
{
memset(next, -1, sizeof(next)); //初始化为-1
for(int i = 1; i <= n; ++i){
for(int j = i+1; j <= n; ++j){
if(a[j] <= a[i]){
next[i] = j;
break;
}
}
}
int q, l, r;
scanf("%d", &q);
while(q--){
scanf("%d%d", &l, &r);
int res = a[l];
for(int i = next[l]; i <= r; i = next[i]){
if(i == -1) //表明后面的数都比它大
break;
if(res == 0) //0取模后结果还是0
break;
res %= a[i];
}
printf("%d\n", res);
}
} int main()
{
int t;
scanf("%d", &t);
while(t--){
scanf("%d", &n);
for(int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
solve();
}
return 0;
}

  

HDU 5875 Function的更多相关文章

  1. HDU 5875 Function 优先队列+离线

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5875 Function Time Limit: 7000/3500 MS (Java/Others) ...

  2. HDU 5875 Function(RMQ-ST+二分)

    Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total ...

  3. HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  4. HDU 5875 Function(ST表+二分)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5875 [题目大意] 给出一个数列,同时给出多个询问,每个询问给出一个区间,要求算出区间从左边开始不 ...

  5. HDU 5875 Function st + 二分

    Function Problem Description   The shorter, the simpler. With this problem, you should be convinced ...

  6. HDU 5875 Function 大连网络赛 线段树

    Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total ...

  7. HDU - 5875 Function(预处理)

    Function The shorter, the simpler. With this problem, you should be convinced of this truth.      Yo ...

  8. HDU 5875 Function -2016 ICPC 大连赛区网络赛

    题目链接 网络赛的水实在太深,这场居然没出线zzz,差了一点点,看到这道题的的时候就剩半个小时了.上面是官方的题意题解,打完了才知道暴力就可以过,暴力我们当时是想出来了的,如果稍稍再优化一下估计就过了 ...

  9. HDU 5875 Function (2016年大连网络赛 H 线段树+gcd)

    很简单的一个题的,结果后台数据有误,自己又太傻卡了3个小时... 题意:给你一串数a再给你一些区间(lef,rig),求出a[lef]%a[lef+1]...%a[rig] 题解:我们可以发现数字a对 ...

随机推荐

  1. Open multiple Eclipse workspaces on the Mac

    This seems to be the supported native method in OS X: cd /Applications/eclipse/ open -n Eclipse.app ...

  2. POJ3126Prime Path

    http://poj.org/problem?id=3126 题意 : 给你两个四位数,都是素数,一个是初始素数x,一个是目标素数y,让你从x变成y,x每次只能改变1位数,来变成另外一个素数k,再改变 ...

  3. Linux内核的同步机制

    本文详细的介绍了Linux内核中的同步机制:原子操作.信号量.读写信号量和自旋锁的API,使用要求以及一些典型示例 一.引言 在现代操作系统里,同一时间可能有多个内核执行流在执行,因此内核其实象多进程 ...

  4. Hibernate逍遥游记-第9章 Hibernate的映射类型

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  5. Java对象的序列化(Object Serialization)

    先定义两个简单的类: package comm; import java.io.Serializable; import java.util.Date; import java.util.Gregor ...

  6. PCL—低层次视觉—点云分割(基于凹凸性)

    1.图像分割的两条思路 场景分割时机器视觉中的重要任务,尤其对家庭机器人而言,优秀的场景分割算法是实现复杂功能的基础.但是大家搞了几十年也还没搞定——不是我说的,是接下来要介绍的这篇论文说的.图像分割 ...

  7. netty 解决TCP粘包与拆包问题(三)

    今天使用netty的固定长度进行解码 固定长度解码的原理就是按照指定消息的长度对消息自动解码. 在netty实现中,只需要采用FiexedLengthFrameDecoder解码器即可... 以下是服 ...

  8. 8天学通MongoDB——第四天 索引操作

    这些天项目改版,时间比较紧,博客也就没跟得上,还望大家见谅. 好,今天分享下mongodb中关于索引的基本操作,我们日常做开发都避免不了要对程序进行性能优化,而程序的操作无非就是CURD,通常我们 又 ...

  9. LA 4127 - The Sky is the Limit (离散化 扫描线 几何模板)

    题目链接 非原创 原创地址:http://blog.csdn.net/jingqi814/article/details/26117241 题意:输入n座山的信息(山的横坐标,高度,山底宽度),计算他 ...

  10. tornado中使用torndb,连接数过高的问题

    问题背景 最近新的产品开发中,使用了到了Tornado和mysql数据库.但在基本框架完成之后,我在开发时候发现了一个很奇怪的现象,我在测试时,发现数据库返回不了结果,于是我在mysql中输入show ...