HDU 5875 Function (思路题目)
来源:互联网 发布:淘宝免费申请试用在哪 编辑:程序博客网 时间:2024/05/17 03:07
大体题意:
告诉你一个自定义的F函数求解方法,给你q个自变量(区间),求解答案!
思路:
分析自定义的函数可以知道,我们可以变换一下这个函数,变换之后也就是F(l,r) = a[l] % a[l+1] %..... % a[r];
如果这样纯粹的求解的话,肯定会超时的,不说有多少个操作,就是这个取模就慢的不行!
我们知道,一个数对比他大的数取模的话,那么这个数取模后是不变的,我们可以利用这一个性质进行优化!
我们定义nest[i] 表示在i位置以后 第一个不比a[i]大的数,这样我们不断的跳位置即可!因为之间的没必要进行枚举,因为取模了值也不变!这样就可以通过了!
坑:
注意,输入的n 个数会有0存在,因此不能直接取模,如果发现是0的话,就没意义了, 直接break 即可!(continue就超时)反正有0就很恶心了,不该有0 的数据的 = = !
详细见代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 100000 + 10;int n;int nest[maxn];int a[maxn];void get_next(){for (int i = 1; i <= n; ++i){for (int j = i+1; j <= n; ++j){nest[i] = n+1;if (a[j] <= a[i]){nest[i] = j;break;}}}}int main(){int T;scanf("%d",&T);while(T--){scanf("%d",&n);for (int i = 1; i <= n; ++i){scanf("%d",&a[i]);}get_next();int q;scanf("%d",&q);while(q--){int l,r;scanf("%d %d",&l,&r);int ans = a[l];for (int i = nest[l]; i <= r; i = nest[i]){if (a[i] == 0)break;ans %= a[i];if (ans == 0)break;}printf("%d\n",ans);}}return 0;}
Function
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1548 Accepted Submission(s): 567
Problem Description
The shorter, the simpler. With this problem, you should be convinced of this truth.
You are given an arrayA 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 calculateF(l,r) , for each query (l,r) .
You are given an array
You job is to calculate
Input
There are multiple test cases.
The first line of input contains a integerT , indicating number of test cases, and T test cases follow.
For each test case, the first line contains an integerN(1≤N≤100000) .
The second line containsN space-separated positive integers: A1,…,AN (0≤Ai≤109) .
The third line contains an integerM denoting the number of queries.
The followingM lines each contain two integers l,r (1≤l≤r≤N) , representing a query.
The first line of input contains a integer
For each test case, the first line contains an integer
The second line contains
The third line contains an integer
The following
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
0 0
- HDU 5875 Function (思路题目)
- HDU 5416(思路题目)
- HDU -- 5242(思路题目)
- HDU 5831(思路题目)
- HDU 4913 (思路题目)
- HDU 5482(思路题目)
- HDU 5875 Function(预处理)
- hdu 5875 Function(高效)
- B. Wrath (思路题目)
- C. Pride (思路题目)
- hdu 6038 function 思路 找环+组合数
- HDU 动态规划(46道题目)倾情奉献~ 【只提供思路与状态转移方程】
- HDU 动态规划(46道题目)倾情奉献~ 【只提供思路与状态转移方程】
- HDU 动态规划(46道题目)倾情奉献~ 【只提供思路与状态转移方程】
- HDU 动态规划(46道题目)倾情奉献~ 【只提供思路与状态转移方程】
- HDU 动态规划(46道题目)倾情奉献~ 【只提供思路与状态转移方程】
- HDU 动态规划(46道题目)倾情奉献~ 【只提供思路与状态转移方程】
- HDU 动态规划(46道题目)倾情奉献~ 【只提供思路与状态转移方程】
- 优化Hybrid app的原生支持
- android使用startActivityForResult时,requestCode和resultCode的作用
- linux VS Windows
- hdu 5869 Different GCD Subarray Query
- 使用Jquery miniui插件写的一个管理员对员工的操作(二)
- HDU 5875 Function (思路题目)
- [矩阵快速幂加速DP] Codeforces 717D Bubble Cup 9 - Finals D. Dexterina’s Lab
- C++引用的基本概念与应用
- [随机化 构造] Codeforces 717H Bubble Cup 9 - Finals H. Pokermon League challenge
- 数据库系统概论之第二章关系数据库
- [构造] Codeforces 717E Bubble Cup 9 - Finals E. Paint it really, really dark gray
- 51nod 1212 无向图最小生成树
- 2016.09.10【初中部 NOIP普及组 】模拟赛题解
- 51nod 1714 B君的游戏