1036: Shepherd
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 477 Solved: 156
[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
5 1 2 3 4 5 3 1 1 2 2 3 3
Sample Output
15 6 3
解答
#include<stdio.h>
long long c[100006];
int main()
{
long long i,repeat,a,b,d,j,s,sum;
while(~scanf("%lld",&repeat)){
sum=0;
for(i=0;i<repeat;i++){
scanf("%lld",&c[i]);
sum=sum+c[i];
}
scanf("%lld",&d);
for(i=1;i<=d;i++){
scanf("%lld%lld",&a,&b);
j=a-1;
s=0;
if(a==1&&b==1){
printf("%lld\n",sum);
}
else{
while(j<repeat){
s=s+c[j];
j=j+b;
}
printf("%lld\n",s);
}
}
}
return 0;
}
本文介绍了一道关于牧羊人训练羊群的算法题目。题目要求计算特定条件下被选中进行训练的羊群总重量。文章提供了完整的C语言实现代码,并通过多个案例展示了如何根据输入参数计算总重量。
176

被折叠的 条评论
为什么被折叠?



