HeHe
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 347 Accepted Submission(s): 147
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
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
思路:可以直接暴力,也可以通过找规律。 找规律的话要多列几项,比如4*4的矩阵就能看出来了,当前数等于左上角的数加一个数减一个数
代码1:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 1010
int a[3*N];
int main()
{
int n,m;
int s,t;
while(~scanf("%d",&n))
{
for(int i=1;i<=n*2-1;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
long long ans=0;
int cnt=n;
while(m--)
{
scanf("%d %d",&s,&t);
s=(s+cnt)%n;t=(t+cnt)%n;
int num=0;
for(int i=1;i<=n;i++)
num+=a[n-s+i-1]*a[n+t-i+1];
cnt=num;
ans+=num;
}
printf("%lld\n",ans);
}
return 0;
}
代码2:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 1010
int a[3*N];
int dp[N][N];
int main()
{
int n,m;
int s,t;
while(~scanf("%d",&n))
{
for(int i=1; i<=n*2-1; i++)
scanf("%d",&a[i]);
memset(dp,0,sizeof(dp));
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(i<2||j<2)
for(int k=1; k<=n; k++)
dp[i][j]+=a[n-i+k]*a[n+j-k];
else
{
dp[i][j]=dp[i-1][j-1];
dp[i][j]-=a[2*n-i+1]*a[j-1];
dp[i][j]+=a[n-i+1]*a[n+j-1];
}
}
}
scanf("%d",&m);
int cnt=n;
long long ans=0;
while(m--)
{
scanf("%d %d",&s,&t);
s=(s+cnt)%n+1;
t=(t+cnt)%n+1;
cnt=dp[s][t];
ans+=dp[s][t];
}
printf("%lld\n",ans);
}
return 0;
}