HDU5875-Function

来源:互联网 发布:深入理解java反射机制 编辑:程序博客网 时间:2024/05/20 08:25

Function

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

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) (1lrN) is defined as:
F(l,r)={AlF(l,r1) 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(1N100000).
  The second line contains N space-separated positive integers: A1,,AN (0Ai109).
  The third line contains an integer M denoting the number of queries. 
  The following M lines each contain two integers l,r (1lrN), representing a query.
 
Output
For each query(l,r), output F(l,r) on one line.
 
Sample Input
132 3 311 3
 
Sample Output
2
 
Source
2016 ACM/ICPC Asia Regional Dalian Online

#include <iostream>#include <cstring>#include <cstdio>using namespace std;int main(){    int t,n,m,l,r,a[100009],next[100009];    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        for(int i=1; i<=n; i++)            scanf("%d",&a[i]);        memset(next,-1,sizeof next);        for(int i=1; i<=n; i++)        {            for(int j=i+1; j<=n; j++)            {                if(a[j]<=a[i])                {                    next[i]=j;                    break;                }            }        }        scanf("%d",&m);        while(m--)        {            scanf("%d %d",&l,&r);            int x=a[l];            for(int i=next[l]; i<=r; i=next[i])            {                if(i==-1) break;                x%=a[i];            }            printf("%d\n",x);        }    }    return 0;}

0 0