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=tn1tn2tn3t1t0tntn1tn2t2t1tn+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 MM.
 

 

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,,t2n4,t2n3,t2n2 separated by exact one space.
The third line contains an integer m, indicates the number of query.
Next m lines will give queries
r0r1r2rm1c0c1c2cm1
For r0,c0 the program will query the element of MM which locates in the rth0 row, cth0 column. For ri,ci(0<i<m), assume that the answer of i1th query is ANS, the program will query the element of MM which locates in ((ri+ANS)%n)th row, ((ci+ANS)%n)th column.
Please process to the end of file.
[Technical Specification]
1n1000
0ti100
0ri,cin1
1m100000
 

 

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
 
这题很卡时间,所以我们不能赋值给二维矩阵再去操作,应该直接在 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;
}

 

posted @ 2016-07-10 15:31  樱花庄的龙之介大人  阅读(256)  评论(0编辑  收藏  举报