1036:shepherd

Posted banyouxia

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1036:shepherd相关的知识,希望对你有一定的参考价值。

1036: Shepherd

Description

Hehe keeps a flock of sheep, numbered from 1 to n and each with a weight wi. To keep the sheep healthy, he prepared some training for his sheep. Everytime he selects a pair of numbers (a,b), and chooses the sheep with number a, a+b, a+2b, … to get trained. For the distance between the sheepfold and the training site is too far, he needs to arrange a truck with appropriate loading capability to transport those sheep. So he wants to know the total weight of the sheep he selected each time, and he finds you to help him.

Input

There’re several test cases. For each case:
The first line contains a positive integer n (1≤n≤10^5)---the number of sheep Hehe keeps.
The second line contains n positive integer wi(1≤n≤10^9), separated by spaces, where the i-th number describes the weight of the i-th sheep.
The third line contains a positive integer q (1≤q≤10^5)---the number of training plans Hehe prepared.
Each following line contains integer parameters a and b (1≤a,b≤n)of the corresponding plan.

Output

For each plan (the same order in the input), print the total weight of sheep selected.

Sample Input

5
1 2 3 4 5
3
1 1
2 2
3 3

Sample Output

15
6
3
思路:将总和算出来,当a==1&&b==1时直接输出总和,其他情况进行查找相加。
代码如下:
#include<bits/stdc++.h>
using namespace std;
#define maxn 100000 + 10
int w[maxn];
int main(){
    int n1,n2,a,b;
    while(~scanf("%d",&n1)){
        long long sum = 0;
        for(int i = 1; i <= n1; i++){
            scanf("%d",&w[i]);
            sum += w[i];
        }
        scanf("%d",&n2);
        while(n2--){
            scanf("%d %d",&a,&b);
            if(a==1 && b==1){
                printf("%lld ",sum);
            }else{
                int sum1 = a;
                long long sum2 = 0;
                int j = 0;
                while(sum1 <= n1){
                    sum2 += w[sum1];
                    j++;
                    sum1 = a + b*j;
                }
                printf("%lld ",sum2);
            }
        }
    }
    return 0;
}






































以上是关于1036:shepherd的主要内容,如果未能解决你的问题,请参考以下文章

剧透,美团自研API网关服务Shepherd架构解读 | ArchSummit

百亿规模API网关服务Shepherd的设计与实现

百亿规模API网关服务Shepherd的设计与实现

美团技术:百亿规模API网关服务Shepherd的设计与实现

美团:百亿规模API网关服务Shepherd的设计与实现

汇Shell SHEPHERD v3 FRED v5+TNO.Riskcurves.v9.0.18.8069+DNV Phast 6.7风险评估