1. 程式人生 > >zcmu-1036: Shepherd(尋找特殊情況走出TLE的困境)

zcmu-1036: Shepherd(尋找特殊情況走出TLE的困境)

1036: Shepherd

Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 274  Solved: 72 [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

一次超時了,一次沒用long long,貢獻兩次submit...

註釋在程式碼裡~ 

#include<stdio.h>
#include<string.h>
const int maxn = 1e5 + 5;
int c[maxn];
int main()
{
    int n,m,a,b,i;
    long long sum;//資料很大用LONGLONG
    while(scanf("%d",&n) != EOF)//多組輸入
    {
        memset(c,0,sizeof(c));//清空陣列
        long long total = 0;
        for(i = 1 ; i <= n;i++)
        {
            scanf("%d",&c[i]);
            total += c[i];
        }
        scanf("%d",&m);
        for(int i = 0 ;i < m;i++)
        {
            sum = 0;
            scanf("%d%d",&a,&b);
            if(a == 1 && b == 1)//把最耗時的一個情況單獨考慮
                sum = total;
            else
            {
                for(int j = a;j <= n;j += b)
                    sum += c[j];
            }
            printf("%lld\n",sum);
        }
    }
    return 0;
}