hdu 5084(矩阵操作)

HeHe

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 350    Accepted Submission(s): 148

Problem Description

M=???????????tn−1tn−2tn−3?t1t0tntn−1tn−2?t2t1tn+1tntn−1?t3t2??????t2∗n−3t2∗n−4t2∗n−5?tn−1tn−2t2∗n−2t2∗n−3t2∗n−4?tntn−1???????????

You are expected to write a program to point out some elements of M∗M.

Input

Multi test cases (about 100), every case occupies two lines.
The first line contains an integer n.
Then second line contain 2*n-1 integers t0,t1,t2,t3,…,t2∗n−4,t2∗n−3,t2∗n−2 separated by exact one space.
The third line contains an integer m, indicates the number of query.
Next m lines will give queries
r0r1r2?rm−1c0c1c2?cm−1
For r0,c0 the program will query the element of M∗M which locates in the rth0 row, cth0 column. For ri,ci(0<i<m), assume that the answer of i−1th query is ANS, the program will query the element of M∗M which locates in ((ri+ANS)%n)th row, ((ci+ANS)%n)th column.
Please process to the end of file.
[Technical Specification]
1≤n≤1000
0≤ti≤100
0≤ri,ci≤n−1
1≤m≤100000

Output

For each case,output the sum of the answer of each query.

Sample Input

3
1 2 3 1 2
2
0 0
1 2
4
10 5 7 2 10 5 7
3
1 2
3 0
2 1
2
1 2 3
4
0 0
0 1
1 0
1 1

Sample Output

23
348
22

Hint

$\quad\ \text{For the first case }M = \begin{pmatrix}
3 & 1 & 2\\
2 & 3 & 1\\
1 & 2 & 3
\end{pmatrix}$

$\text{For the second case }M = \begin{pmatrix}
2 & 10 & 5 & 7\\
7 & 2 & 10 & 5\\
5 & 7 & 2 & 10\\
10 & 5 & 7 & 2
\end{pmatrix}$

Source

BestCoder Round #15

这题很卡时间,所以我们不能赋值给二维矩阵再去操作,应该直接在 t 数组上面操作,a[i][j] 与 t[n-1+j-i] 一一对应,所以我们求解矩阵上某个值时直接用 t 数组操作好了。还有就是答案会超出整形表示范围,开_int64.

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int N = 2005;
int v[N];
int n;
int main()
{
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<=2*n-2;i++){
            scanf("%d",&v[i]);
        }
        int q;
        int temp=0;
        long long ans = 0;
        scanf("%d",&q);
        while(q--){
            int r,c;
            scanf("%d%d",&r,&c);
            r = (r+temp)%n,c = (c+temp)%n;
            temp = 0;
            for(int i=0;i<n;i++){
                temp+= v[n-1+i-r]*v[n-1+c-i];
            }
            ans += temp;
        }
        printf("%lld\n",ans);
    }
    return 0;
}
时间: 2024-08-08 07:49:53

hdu 5084(矩阵操作)的相关文章

hdu 4291 矩阵幂 循环节

http://acm.hdu.edu.cn/showproblem.php?pid=4291 凡是取模的都有循环节-----常数有,矩阵也有,而且矩阵的更神奇: g(g(g(n))) mod 109 + 7  最外层MOD=1e9+7  可以算出g(g(n))的循环节222222224,进而算出g(n)的循环节183120LL,然后由内而外计算即可 注释掉的是求循环节的代码 //#pragma comment(linker, "/STACK:102400000,102400000")

OpenCV &mdash;&mdash; 矩阵操作

多通道的矩阵 -- 通道是连续的!! 要将指向该数据类型的指针移动到下一通道,我们只需要将其增加1.如果想访问下一个"像素"或者元素集,则需要一定的偏移量 矩阵的step元素是矩阵中行的长度,单位为字节.   #include "cv.h" #include "highgui.h" #include <IOSTREAM.H> int main(int argc,char** argv) { float vals[]={0.85,-0.

iOS开发UI篇—Quartz2D使用(矩阵操作)

iOS开发UI篇-Quartz2D使用(矩阵操作) 一.关于矩阵操作 1.画一个四边形 通过设置两个端点(长和宽)来完成一个四边形的绘制. 代码: 1 - (void)drawRect:(CGRect)rect 2 { 3 //画四边形 4 //获取图形上下文 5 CGContextRef ctx=UIGraphicsGetCurrentContext(); 6 //绘图 7 CGContextAddRect(ctx, CGRectMake(20, 50, 100, 100)); 8 //渲染

HDU 4965 矩阵快速幂

顺手写了下矩阵类模板 利用到矩阵乘法的交换律 (A*B)^n == A * (B*A)^n-1 *B #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <utility> #include <stack> #includ

poj3735—Training little cats(特殊操作转化为矩阵操作)

题目链接:http://poj.org/problem?id=3735 题目意思: 调教猫咪:有n只饥渴的猫咪,现有一组羞耻连续操作,由k个操作组成,全部选自: 1. g i 给第i只猫咪一颗花生 2. e i 让第i只猫咪吃光它的花生 3. s i j 交换猫咪i与猫咪j的花生 现将上述一组连续操作做m次后,求每只猫咪有多少颗花生? 思路:这道题难点在如何把这种奇怪的操作转化为矩阵操作,网络上看到一个画的很好的图,这里直接偷过来. 现在,对于每一个操作我们都可以得到一个转置矩阵,把k个操作的矩

Opencv矩阵操作

分配矩阵空间: CvMat* cvCreateMat(int rows, int cols, int type); 释放矩阵空间: cvReleaseMat(&M); 数据类型宏定义: CV_<bit_depth>(S|U|F)C<number_of_channels> 复制矩阵: CvMat* M2;    M2=cvCloneMat(M1); 初始化矩阵: double a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; 要

matlab矩阵操作

1:求行数  nrow=size(matrix,1); 2:求列数  ncol=size(matrix,2); 3:矩阵合并 c1=[m1,m2]横向合并  c2=[m1;m2]纵向合并 m1= [5     2     3] ; m2=[1     2     3]; c1 =[5     2     3     1     2     3]; c2 =[ 5 2 3 1 2 3 ] matlab的下标从1开始,取matlab的元素直接用m[index]即可 取子矩阵 m[r1:rn,:]  

避免使用循环的几个高效matlab矩阵操作

matlab拥有强大的晕针运算能力,下面的几个例子展示了很多在其他语言中需要用循环遍历才能完成的操作在matlab里可以用矩阵操作来完成 1)将矩阵中小于零的元素置零: a = [1,2,3;-1,-2,-3;1,0,-1] a(a<0) = 0 打印结果: a = 1 2 3 -1 -2 -3 1 0 -1 a = 1 2 3 0 0 0 1 0 0 2)将矩阵a中小于0的元素乘以2,其他元素不改变值: a = [1,2,3;-1,-2,-3;1,0,-1] a(find(a<0)) = a

hdu 4965 矩阵快速幂 矩阵相乘性质

Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 170    Accepted Submission(s): 99 Problem Description One day, Alice and Bob felt bored again, Bob knows Alice is a gir