1036: Shepherd
Time Limit: 1 Sec Memory Limit: 128 MB[Submit][Status][Web Board]
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
Sample Output
【分析】
题意:第一行输入一个数字n,第二行输入n个数字,第三行输入数字q,表示接下来又q个询问,接下来的q行,每行有两个数字a,b,要求计算根据每组a,b,在n个数字中从第a个开始,相隔b,2*b,3*b ……个位置的数字的和,输出这个和。
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int a[100000];
int n;
while(~scanf("%d",&n))
{
long long s1=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
s1+=a[i];
}
int pp;scanf("%d",&pp);
while(pp--)
{
long long sum=0;
int b,c;
scanf("%d%d",&c,&b);
if(c==1&&b==1){printf("%lld\n",s1);continue;}
for(int i=c-1;i<n;i+=b)sum+=a[i];
printf("%lld\n",sum);
}
}
return 0;
}