ZCMU 1036: Shepherd

这道题目描述了主人Hehe为他的羊群安排训练,每次选择编号为a、a+b、a+2b...的羊进行训练,并需要知道每次选取羊的总重量。输入包含羊的数量、每只羊的重量以及训练计划,输出每个计划中羊的总重量。题目需要注意数据类型溢出和特定情况的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

【分析】题意是n只羊,从第c只开始,每隔b-1只羊,就计算重量和

题目简单,但有两个坑,如果把sum定义为int型会wa,不单独考虑a==1&&b==1时会Time Limit Exceed,我觉得是题目设置问题

#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;  
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值