HDU 5875 Function
2016-09-12 20:37
405 查看
Function
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1095 Accepted Submission(s): 420
[align=left]Problem Description[/align]
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).
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each query(l,r), output F(l,r) on one line.
[align=left]Sample Input[/align]
1
3
2 3 3
1
1 3
[align=left]Sample Output[/align]
2
[align=left]Source[/align]
2016 ACM/ICPC Asia Regional Dalian Online
解析:题意为对于每个询问区间[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 5876 Sparse Graph
- request is not finfished yet!
- svn 切换用户
- 对称加密与非对称加密2_SSL
- String to Integer (atoi)
- 【一】React.JS & BackBone.JS 前后端分离初体验---React组件
- HDU1224-Free DIY Tour
- cuda reduce学习
- JAVA001对象导论10
- 【T电商 3】Nginx的Http(图片)服务器配置+ftp上传使用说明
- 树状数组区间修改加区间查询
- 高楼抛玻璃球(鸡蛋)问题
- PHP设计模式之:单例模式
- 关于如何添加手机照片到ImageView上
- 筛法求素数
- 看重云上创业项目,与AWS合作的光环新网“另有所图”
- 我对Spring的aop的理解
- jsp重定向与请求转发
- MAC为Apache2服务器配置多个虚拟主机
- 7个少根筋的笑话