Function
The shorter, the simpler. With this problem, you should be convinced of this truth.
You are given an array (l,r).
You are given an array (l,r).
InputThere are multiple test cases.
The first line of input contains a integer F(l,r) on one line.Sample Input
1 3 2 3 3 1 1 3
Sample Output
2
预处理出每个数下一个比他小(或等于)的数的位置。然后跳着取模即可。
数据是有多水。。n^2预处理都能过。。
#include <bits/stdc++.h> #define MAXN 100005 using namespace std; int a[MAXN],nxt[MAXN]; int main(void) { int T,n,m,l,r,ans; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i = 1; i <= n; i++) { scanf("%d",&a[i]); nxt[i]=n+1; } for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){ if(a[i]>=a[j]){ nxt[i]=j; break; } } } scanf("%d",&m); while(m--) { scanf("%d %d",&l,&r); int ans=a[l]; for(int i=nxt[l];i<=r;i=nxt[i]){ if(i==n+1) break; ans%=a[i]; } printf("%d\n",ans); } } return 0; }