标签:
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 399 Accepted Submission(s): 151
1 //2016.9.11 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #define N 100005 6 7 using namespace std; 8 9 int a[N], nex[N];//nex数组,表示跳到下一个要取余的位置,比a[i]大的数不用取余,此处优化降低时间 10 11 int main() 12 { 13 int T, n, q, ans; 14 scanf("%d", &T); 15 while(T--) 16 { 17 scanf("%d", &n); 18 for(int i = 1; i <= n; i++) 19 { 20 scanf("%d", &a[i]); 21 } 22 scanf("%d", &q); 23 int l, r; 24 for(int i = 1; i <= n; i++) 25 { 26 nex[i] = -1; 27 for(int j = i+1; j <= n; j++) 28 if(a[i]>=a[j]) 29 { 30 nex[i] = j; 31 break; 32 } 33 } 34 while(q--) 35 { 36 scanf("%d%d", &l, &r); 37 ans = a[l]; 38 for(int i = nex[l]; i <= r; i = nex[i]) 39 { 40 if(i == -1)break; 41 ans %= a[i]; 42 } 43 printf("%d\n", ans); 44 } 45 } 46 47 return 0; 48 }
标签:
原文地址:http://www.cnblogs.com/Penn000/p/5861361.html