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

来源:互联网 发布:模拟弹钢琴的软件 编辑:程序博客网 时间:2024/05/17 03:03
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 112 20 03 723 47 16
Sample Output
234279972937          


题意就是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;    }}

原创粉丝点击