Problem E. Matrix from Arrays

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1384    Accepted Submission(s): 630

Problem Description
Kazari has an array A length of L, she plans to generate an infinite matrix M using A.
The procedure is given below in C/C++:

int cursor = 0;

for (int i = 0; ; ++i) {
for (int j = 0; j <= i; ++j) {
M[j][i - j] = A[cursor];
cursor = (cursor + 1) % L;
}
}

Her friends don't believe that she has the ability to generate such a huge matrix, so they come up with a lot of queries about M, each of which focus the sum over some sub matrix. Kazari hates to spend time on these boring queries. She asks you, an excellent coder, to help her solve these queries.

 
Input
The first line of the input contains an integer T (1≤T≤100) denoting the number of test cases.
Each test case starts with an integer L (1≤L≤10) denoting the length of A.
The second line contains L integers A0,A1,...,AL−1 (1≤Ai≤100).
The third line contains an integer Q (1≤Q≤100) denoting the number of queries.
Each of next Q lines consists of four integers x0,y0,x1,y1 (0≤x0≤x1≤108,0≤y0≤y1≤108) querying the sum over the sub matrix whose upper-leftmost cell is (x0,y0) and lower-rightest cell is (x1,y1).
 
Output
For each test case, print an integer representing the sum over the specific sub matrix for each query.
 
Sample Input
1
3
1 10 100
5
3 3 3 3
2 3 3 3
2 3 5 8
5 1 10 10
9 99 999 1000
 
Sample Output
1
101
1068
2238
33076541
 
Source
 
Recommend
chendu   |   We have carefully selected several similar problems for you:  6373 6372 6371 6370 6369 
 
题意:给你一串数字按顺序填充矩阵,询问q次,问(x0,y0)到(x1,y1)的矩阵和
如:
3
1 10 100
是这样填充:
    1    10    1      1  10  ...
100    10  10  100  ...
100  100    1     ...
    1    10   ...
100     ...
分析:通过打表我们可以找出规律(这个得看数学直觉和平常的做题范围了):如果是奇数大小为L*L,如果为偶数大小为2L*2L的矩阵是重复出现,所以循环节我们可以设成2L*2L
  找到规律后我们可以先预处理求出2L*2L的矩阵前缀和,然后把要求的范围分割成多少个2L*2L的矩阵来求
AC代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e3+10;
const ll mod = 1e9+7;
const double pi = acos(-1.0);
const double eps = 1e-8;
//FILE* fout = fopen("0001.out", "w");
ll n, T;
ll mapn[maxn][maxn], a[maxn];
ll dp[maxn][maxn]; //(i,j)区域的前缀和
ll get( ll s, ll t ) { //这里的s,t由x,y减一得到,有可能产生负数
if( s == -1 || t == -1 ) { //如果s,t为负数,dp的值为0
return 0;
}
ll x = s%n, cnt_x = s/n; //判断s,t范围内由几个2*n的区域组成
ll y = t%n, cnt_y = t/n;
//debug(x), debug(cnt_x), debug(y), debug(cnt_y);
//debug(dp[x][n-1]), debug(dp[n-1][y]), debug(dp[x][y]); return dp[x][n-1]*cnt_y+dp[n-1][y]*cnt_x+dp[n-1][n-1]*cnt_x*cnt_y+dp[x][y];
}
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
scanf("%lld",&T);
while( T -- ) {
memset(dp,0,sizeof(dp));
memset(mapn,0,sizeof(mapn));
scanf("%lld",&n);
for( ll i = 0; i < n; i ++ ) {
scanf("%lld",&a[i]);
}
ll cur = 0;
for( ll i = 0; i <= 100; i ++ ) {
for( ll j = 0; j <= i; j ++ ) {
mapn[j][i-j] = a[cur];
cur = (cur+1)%n;
}
}
dp[0][0] = mapn[0][0];
for( ll i = 1; i < 2*n; i ++ ) {
dp[0][i] = dp[0][i-1] + mapn[0][i];
}
for( ll i = 1; i < 2*n; i ++ ) {
dp[i][0] = dp[i-1][0] + mapn[i][0];
}
for( ll i = 1; i < 2*n; i ++ ) {
for( ll j = 1; j < 2*n; j ++ ) { //求前缀和
dp[i][j] = mapn[i][j] + dp[i][j-1] + dp[i-1][j] - dp[i-1][j-1];
}
}
n = 2*n; //将n变成2*n 因为如果是奇数大小为L*L,如果为偶数大小为2L*2L的矩阵是重复出现
ll q, x0, y0, x1, y1;
scanf("%lld",&q);
while( q -- ) {
scanf("%lld%lld%lld%lld",&x0,&y0,&x1,&y1);
//fprintf( fout, "%lld\n", get(x1,y1)-get(x1,y0-1)-get(x0-1,y1)+get(x0-1,y0-1) );
printf("%lld\n",get(x1,y1)-get(x1,y0-1)-get(x0-1,y1)+get(x0-1,y0-1));
}
}
return 0;
}

  

杭电第四场 hdu6336 Problem E. Matrix from Arrays 打表找规律 矩阵前缀和(模板)的更多相关文章

  1. 2018 Multi-University Training Contest 4 Problem E. Matrix from Arrays 【打表+二维前缀和】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6336 Problem E. Matrix from Arrays Time Limit: 4000/20 ...

  2. 数学--数论--HDU 1792 A New Change Problem (GCD+打表找规律)

    Problem Description Now given two kinds of coins A and B,which satisfy that GCD(A,B)=1.Here you can ...

  3. A Simple Problem with Integers 循环节 修改 平方 找规律 线段树

    A Simple Problem with Integers 这个题目首先要打表找规律,这个对2018取模最后都会进入一个循环节,这个循环节的打表要用到龟兔赛跑. 龟兔赛跑算法 floyed判环算法 ...

  4. 杭电多校第十场 hdu6432 Cyclic 打表找规律

    Cyclic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Su ...

  5. Problem E. Matrix from Arrays(杭电2018年多校第四场+思维+打表找循环节)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6336 题目: 题意:给你一个l个元素的数组a,用题目中的程序构造一个新的矩阵,询问q次,问以(x1,y ...

  6. 杭电oj 1016 Prime Ring Problem

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. hdu6373 Pinball 杭电第六场 物理知识

    Pinball Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total S ...

  8. 杭电第六场 hdu6362 oval-and-rectangle 积分求期望

    oval-and-rectangle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. hdu6354 杭电第五场 Everything Has Changed 计算几何

    Everything Has Changed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java ...

随机推荐

  1. Task CancellationTokenSource和Task.WhenAll的应用

    Task是.net4.0推出的异步编程类,与ThreadPool.QueneUserWorkItem方法类似的是,Task也是使用线程池来工作的.但Task比起这个QueneUserWorkItem的 ...

  2. S2:java集合框架

    Java集合就是一个容器.面向对象语言对事物的体现都是以对象的形式存在,所以为了方便对多个对象的操作,就对对象进行存储,集合就是存储对象最常用的一种方式.集合只用于存储对象,集合长度是可变的,集合可以 ...

  3. SpringMVC项目案例之---数据的获取与显示

    数据的获取与显示 (一)功能 1.对用户输入的数据进行获取 2.将获取的数据显示到页面 3.使用了SpringMVC技术的注解方式 4.使用了过滤器,处理中文乱码问题 5.在web.xml中设置了访问 ...

  4. npm包开发与发布

    把通用的功能开发成npm包,便用使用和维护,更重要的是可以分享给广大的开发者,是不是很激动人心! 那么,步骤如下: 1.创建项目 创建项目目录,npm init ,根据需要输入配置信息(建完后也可以在 ...

  5. 统计学习方法—SVM推导

    目录 SVM 1. 定义 1.1 函数间隔和几何间隔 1.2 间隔最大化 2. 线性可分SVM 2.1 对偶问题 2.2 序列最小最优算法(SMO) 3. 线性不可分SVM 3.1 松弛变量 3.2 ...

  6. ccf 201809-4 再卖菜

    这题一开始不知道剪枝这种操作,只会傻傻地dfs. 然后dfs递归写80分超时,非递归写70分超时(纳尼?我一直以为非递归算法在时间上会更优秀一些,为什么会这样?!!) 剪一下枝就都能过了 #inclu ...

  7. 精准测试与开源工具Jacoco的覆盖率能力大PK

    导读:本文根据实际使用情况,简要分析了精准测试和类Jacoco等传统白盒工具在设计理念.功能和应用场景的异同点,并阐述了覆盖率技术如何在新型企业开发体系中,发挥应有的重要作用. 覆盖率技术可以说是测试 ...

  8. 【win】【qt5打包】【qt程序打包成一个可执行文件(带图标任何win都可以运行哦)】

    [前言] 业务需求将qt程序打包成win可执行文件.咱是做linux的,奈何用的麒麟系统,程序运行在win,好嘛,重新在win qtcreator编译后打包呗. [目标] 1.给qt程序添加一个图标. ...

  9. 康托(Cantor)展开

    直接进入正题. 康托展开 Description 现在有"ABCDEFGHIJ”10个字符,将其所有的排列中按字典序排列,给出任意一种排列,说出这个排列在所有的排列中是第几小的? Input ...

  10. Oracle中的通用函数

    1.nvl(列,默认值)函数处理null select nvl(null,3),nvl(4,3) from dual    结果显示为3,4.因为nvl中的第一个为null时,返回结果为第二个值,第一 ...