zoukankan      html  css  js  c++  java
  • hdu 5084 前缀和预处理

    http://acm.hdu.edu.cn/showproblem.php?pid=5084

    给出矩阵M,求M*M矩阵的r行c列的数,每个查询跟前一个查询的结果有关。

    观察该矩阵得知,令ans = M*M,则 ans[x][y] = (n-1-x行的每个值)*(n-1+y列的每个值),即:

    ans[x][y] = t[y] * t[2*n - 2 - x] +....+ t[y + n - 1]*t[n - 1 - x]

    每一对的和为定值2*n-2-x-y,然后就是求每对i+j的前缀和(所有i+j值相同按顺序加起来)

    #pragma comment(linker, "/STACK:36777216")
    #pragma GCC optimize ("O2")
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    #include <string>
    #include <queue>
    #include <map>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define RD(x) scanf("%d",&x)
    #define RD2(x,y) scanf("%d%d",&x,&y)
    #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
    #define clr0(x) memset(x,0,sizeof(x))
    #define eps 1e-9
    const double pi = acos(-1.0);
    typedef long long LL;
    const int modo = 1e9 + 7;
    const int INF = 0x3f3f3f3f;
    const int maxn = 1005,maxm = 1e4 + 5;
    int t[maxn<<1],ans[maxn<<1][maxn<<1],n,m;
    
    int main(){
        int x,y,_n;
        while(~RD(n)){
            _n = n;
            n = -2 + (n<<1);
            clr0(ans);
            for(int i = 0;i <= n;++i)
                RD(t[i]);
            for(int i = 0;i <= n;++i)
                for(int j = 0;j <= n;++j)
                ans[i][j] = t[i]*t[j];
    
            for(int i = 1;i <= n;++i)
                for(int j = 0;j < n;++j){
                    ans[i][j] += ans[i-1][j+1];
                }
            RD(m);
            LL sum = 0;int res = 0;
            while(m--){
                RD2(x,y);
                x = (x+res)%_n,y = (y+res)%_n;
                int sx = _n - 1 + y,sy = _n - 1 - x;
                res = ans[sx][sy];
                if(sx - _n >= 0 && sy + _n <= n)
                    res -= ans[sx-_n][sy+_n];
                sum += res;
    //            cout<<x<<','<<y<<endl;
    //            cout<<res<<endl;
            }
            printf("%I64d
    ",sum);
        }
        return 0;
    }
    


  • 相关阅读:
    select/poll/epoll 对比
    I/O Mutiplexing poll 和 epoll
    Socket 编程IO Multiplexing
    ubuntu12.04 lts 安装gcc 4.8
    time since epoch
    ceph-RGW Jewel版新概念
    支持向量机(svm)
    MachineLearning之Logistic回归
    ML之回归
    ML之监督学习算法之分类算法一 ——— 决策树算法
  • 原文地址:https://www.cnblogs.com/zibaohun/p/4074385.html
Copyright © 2011-2022 走看看