HUST 1601 Shepherd

间隔小的时候dp预处理,大的时候暴力。。正确做法不会。。。

dp[i][j]表示以i为开头,间隔为j的和,递推:dp[i][j] = dp[i + j][j] + a[i]

测试数据中间隔可能是0......

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

const int maxn = 100000 + 200;
long long dp[maxn][70 + 10];
long long a[maxn];
int n, q, x, y;

void init()
{
    memset(dp, 0, sizeof dp);
    for (int i = n; i >= 1; i--)
        for (int j = 1; j <= 70; j++)
            dp[i][j] = dp[i + j][j] + a[i];
}

int main()
{
    while (~scanf("%d", &n)){
        for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
        init();
        scanf("%d", &q);
        while (q--)
        {
            scanf("%d%d", &x, &y);
            if (y == 0) printf("%lld\n", a[x]);
            else
            {
                if (y <= 70) printf("%lld\n", dp[x][y]);
                else
                {
                    long long ans = 0;
                    for (int i = x; i <= n; i = i + y) ans = ans + a[i];
                    printf("%lld\n", ans);
                }
            }
        }
    }
    return 0;
}

 

posted @ 2016-03-07 09:51  Fighting_Heart  阅读(114)  评论(0编辑  收藏  举报