hdu 5084 HeHe(暴力/找规律)

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=tn1tn2tn3t1t0tntn1tn2t2t1tn+1tntn1t3t2t2n3t2n4t2n5tn1tn2t2n2t2n3t2n4tntn1

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
题意:给一个矩阵形如题目给的那样,问M*M对应的(x,y)是多少,(0,0)是左上角

思路:可以直接暴力,也可以通过找规律。 找规律的话要多列几项,比如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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值