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
", a[x]);
            else
            {
                if (y <= 70) printf("%lld
", dp[x][y]);
                else
                {
                    long long ans = 0;
                    for (int i = x; i <= n; i = i + y) ans = ans + a[i];
                    printf("%lld
", ans);
                }
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/5249507.html