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. hdu 4447 Yuanfang, What Do You Think?

    思路: 这题有个结论也可以自己归纳: 对于给定的n,其约数用pi表示 T(n)=T(p1)T(p2)……T(pn)T(n') 其中T(n')是这个式子所独有的也就是 T(n')=(x^n-1)/T(p ...

  2. hdu 1376 Octal Fractions

    刚开始做这题时,用的是0.75[8]=(7/8+5/64)[10]这个,但是总是WA…………无语了…… 后来看别人的解题报告,知道了另外一个就是0.75[8]=((5/8+7)/8)[10],从低位向 ...

  3. 解决Unable to load R3 module ...VBoxDD.dll (VBoxDD):GetLastError=1790

    解决Unable to load R3 module ...VBoxDD.dll (VBoxDD):GetLastError=1790 参考文章:http://blog.sina.com.cn/s/b ...

  4. hdu1875

    http://acm.hdu.edu.cn/showproblem.php?pid=1875 2 2 10 10 20 20 3 1 1 2 2 1000 1000 给定坐标 //最小生成树 #inc ...

  5. 【memcache缓存专题(2)】memcache安装与命令行使用

    进新公司一个多月了,一直没有时间来更新,后续还是要保持着每日更新的频率 安装 在windows上安装 略(都玩到缓存的程度了,就没必要在windows上捣弄了) 给个参考: http://blog.c ...

  6. python 解析 xml

    <taskList nextId="62292"> <task module="reliability" owner="vprovo ...

  7. Lua表的构造及遍历

    关于lua中的table,主要的困惑来自于table既可以当array用又可以当record用,有时候就会混淆不清. lua中的table貌似是用map来实现的,array是语法糖,一种特例.下面是l ...

  8. RMI

    Java RMI (Remote Method Invocation 远程方法调用)是用Java在JDK1.1中实现的,它大大增强了Java开发分布式应用的能力.Java作为一种风靡一时的网络开发语言 ...

  9. MyEclipse +Servlet 乱码

    用MyEclipse 新建了Web Project,然后建立了一个Servlet,在doGet方法中谢了简单的一句欢迎语句,本来以为平平常常的事情,但是却出错了,乱码问题,在网上找了一些帖子,说是设置 ...

  10. python小问题记录:

    numpy.chararray.flatten chararray.flatten(order='C') Return a copy of the array collapsed into one d ...