HDU - 5084 HeHe

BestCoder #15的1003...

当时不想写模拟就跳过1002看了1003,写了一发暴力,TLE。。。这是第一个见到的在矩阵上找规律的题。


这是BC首页的题解。那个前缀和还是辉哥给讲明白的。。。


此题的数据规模为1000,普通的矩阵乘法无法通过。要优化,考虑到矩阵的特殊性,使得优化成为了可能。
ans[x][y]就是 a[y]*a[2*n-x] + .... + a[y+n-1]*a[n-x+1]
乘的这部分
a[i]*a[j]
i+j是定值
所以 对于i+j等于某个数的,求个前缀和
最后每个值就O(1)得到了

于是最后总复杂度为o(n^2)


开一个二维的矩阵,第一维是i+j的值,第二维是i的值, 值为t [ i ]*t [ j ]。

对于ans[ x ][ y ] = t[ n+1-x ]*t[ n-1+y ] + t[ n+2-x]*t[ n-2+y]+...+t[ 2*n-x]*t[ y ],即二维数组中第2*n-x+y行,从n+1-x个元素开始的n个元素的连续和。


这样就理解了题解中说的对于i+j等于某个数的,求个前缀和。

因为要求前缀和,矩阵的元素的标记是从( 1, 1 )到( n, n )的。


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;
typedef long long LL;
typedef unsigned long long LLU;

const int maxn=1010;
LL t[2*maxn], M[3*maxn][2*maxn];
int n;

void solve()
{
    for(int i=n+1; i<=3*n-1; i++)
    {
        M[i][0]=0;
        for(int j=1; j<2*n; j++)
            if(i-j>0 && (i-j)<=(2*n-1))
                M[i][j]=t[j]*t[i-j]+M[i][j-1];
    }
}

int main()
{
    while(~scanf("%d", &n))
    {
        for(int i=1; i<=2*n-1; i++)
            scanf("%d", &t[i]);
        solve();

        LL ANS=0, ans=0;
        int r, c, m;
        scanf("%d", &m);
        while(m--)
        {
            scanf("%d%d", &r, &c);
            r=(r+ANS)%n+1;
            c=(c+ANS)%n+1;
            ANS=M[2*n-r+c][2*n-r]-M[2*n-r+c][n+1-r-1];
            ans+=ANS;
        }
        printf("%I64d\n", ans);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值