233矩阵(上三角矩阵求累加量//代码和思路不一样

本文介绍了一种特殊的矩阵——233矩阵,并提供了一种算法来计算该矩阵中特定位置的值。233矩阵的第一行由递增的233序列构成,后续行的每个元素等于其左侧和上方元素之和。文章通过示例详细解释了如何计算矩阵中任意位置的值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 2333, 23333... (it means a 0,1 = 233,a0,2 = 2333,a 0,3 = 23333...) Besides, in 233 matrix, we got a i,j = ai-1,j +a i,j-1( i,j ≠ 0). Now you have known a 1,0,a 2,0,...,a n,0, could you tell me a n,m in the 233 matrix?
Input
There are multiple test cases. Please process till EOF. 

For each case, the first line contains two postive integers n,m(n ≤ 10,m ≤ 10 9). The second line contains n integers, a 1,0,a 2,0,...,an,0(0 ≤ a i,0 < 2 31).
Output
For each case, output a n,m mod 10000007.
Sample Input
1 1
1
2 2
0 0
3 7
23 47 16
Sample Output
234
2799
72937


        
  


题意就是i,j = ai-1,j +a i,j-1;

下面是例子。

#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <cstdio>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
using namespace std;

#define pi acos(-1)
#define endl '\n'
#define rand() srand(time(0));
#define me(x) memset(x,0,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0);
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
//const int dx[]={-1,0,1,0,-1,-1,1,1};
//const int dy[]={0,1,0,-1,1,-1,1,-1};
const int maxn=1e3+5;
const int maxx=1e5+100;
const double EPS=1e-7;
const int MOD=10000007;
#define mod(x) ((x)%MOD);
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;
/*lch[root] = build(L1,p-1,L2+1,L2+cnt);
    rch[root] = build(p+1,R1,L2+cnt+1,R2);中前*/
/*lch[root] = build(L1,p-1,L2,L2+cnt-1);
    rch[root] = build(p+1,R1,L2+cnt,R2-1);中后*/
long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}

struct node
{
    LL t[13][13];
    void mex()
    {
        me(t);
    }
}a,b,c,d,e;

int n,m;
node operator*(node a,node b)//重载运算符
{
    node ret;
    LL x;
    ret.mex();
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
    {
        x=0;
        for(int k=0;k<n;k++)
            x+=mod((LL)a.t[i][k]*b.t[k][j]);
        ret.t[i][j]=mod(x);
    }
    return ret;
}
void init()
{
    a.mex();b.mex();c.mex();d.mex();e.mex();
}

node pow_mat(node a,node b,int x)//矩阵快速幂
{
    node ret=b;
    while(x)
    {
        if(x%2)  ret=ret*a;
        a=a*a;
        for(int i=0;i<n;i++)
        {
            /*for(int j=0;j<n;j++)
            {
                cout<<ret.t[i][j]<<" ";
            }
            cout<<endl;*/
        }
        x>>=1;
    }
    return ret;
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        for(int i=n-1; i>=0; i--)
            cin>>b.t[0][i];
        //cout<<"输入完成"<<endl;
        for(int i=0; i<n; i++)
            for(int j=0; j<n; j++)
                if(i>=j)
                    c.t[i][j]=1;//上三角矩阵行列式求累加量
        b=pow_mat(c,b,m);
       // cout<<"-----------------"<<endl;
        n=n+2;
        d.t[0][n-1]=3;
        d.t[0][n-2]=23;//构造右边233的矩阵
        for(int i=0; i<n; i++)
            for(int j=0; j<n; j++)
                if(i>=j)
                {
                    if(i!=n-2)
                        e.t[i][j]=1;
                    else
                        e.t[i][j]=10;
                }
        /*cout<<"-------------"<<endl;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                cout<<d.t[i][j]<<" ";
            }
            cout<<endl;
        }
        cout<<"-----------------"<<endl;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                cout<<e.t[i][j]<<" ";
            }
            cout<<endl;
        }*/
        d=pow_mat(e,d,m);
        cout<<(d.t[0][0]+b.t[0][0])%MOD<<endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值