HUST-1601 - Shepherd 暴力

1601 - Shepherd

时间限制:2秒 内存限制:64兆
511 次提交 84 次通过
题目描述
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.
输入
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.
输出
For each plan (the same order in the input), print the total weight of sheep selected.
样例输入
5
1 2 3 4 5
3
1 1
2 2
3 3
样例输出
15
6
3
提示

来源
Problem Setter : Zhou Zhou

题意:第一行输入一个数字n,第二行输入n个数字,第三行输入数字q,表示接下来又q个询问,接下来的q行,每行有两个数字a,b,要求计算根据每组a,b,在n个数字中从第a个开始,相隔b,2*b,3*b ……个位置的数字的和,输出这个和。

解析:没什么特别的方法,就是直接的计算,题目有个坑点,题中b>=1,而实际上存在b=0的情况,因此需要一个特判,b=0时,直接输出第a个位置上的数字,至于从第1个位置开始间隔为1的和的特判并不是必要的,因为题目给的时间比较长,没有任何的优化也不会TLE,如果要优化的话可以对b<=500的情况打表。

根据以上的思路,然后写成代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;

int main()
{
    int n,m,c,b,i,q,j,k;
    int a[100000];
    long long int sum,s1;
    while(~scanf("%d",&n))
    {
        s1=0;
        for(i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
            s1+=a[i];
        }
        scanf("%d",&q);
        while(q--)
        {
            sum=0;
            scanf("%d %d",&c,&b);
            if(c==1&&b==1){printf("%lld\n",s1);continue;}
            if(b==0) {sum+=a[c-1];printf("%lld\n",sum);continue;}
            for(i=c-1; i<n; i+=b)
            {
                sum+=a[i];
            }
            printf("%lld\n",sum);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值