前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HUSTM 1601 - Shepherd

HUSTM 1601 - Shepherd

作者头像
ShenduCC
发布2018-04-26 11:55:14
5760
发布2018-04-26 11:55:14
举报
文章被收录于专栏:算法修养

题目描述 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 提示

暴力也能过 醉了

代码语言:javascript
复制
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <stdio.h>

using namespace std;
int n;
int a[100005];
int s[100005];
int q;
int b,c;
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        s[0]=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            s[i]=s[i-1]+a[i];

        }
        scanf("%d",&q);
        for(int i=1;i<=q;i++)
        {
            long long int ans=0;
            scanf("%d%d",&b,&c);

            if(c==0)
            {
                ans=a[b];
                printf("%lld\n",ans);
                continue;
            }
            else
            {
                for(int j=b;j<=n;j+=c)
                    ans+=a[j];
            }
            printf("%lld\n",ans);

        }
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-03-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档