• A Simple Math Problem(矩阵快速幂)----------------------蓝桥备战系列


    Lele now is thinking about a simple function f(x). 

    If x < 10 f(x) = x. 
    If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10); 
    And ai(0<=i<=9) can only be 0 or 1 . 

    Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m. 

    Input

    The problem contains mutiple test cases.Please process to the end of file. 
    In each case, there will be two lines. 
    In the first line , there are two positive integers k and m. ( k<2*10^9 , m < 10^5 )
    In the second line , there are ten integers represent a0 ~ a9. 

    Output

    For each case, output f(k) % m in one line.

    Sample Input

    10 9999
    1 1 1 1 1 1 1 1 1 1
    20 500
    1 0 1 0 1 0 1 0 1 0

    Sample Output

    45
    104

    代码:

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<set>
    #include<vector>
    #include<map>
    #include<cmath>
    const int maxn=1e5+5;
    typedef long long ll;
    using namespace std;
    
    ll mod;
    ll op[15];
    struct mat
    {
    	ll a[15][15];
    };
    mat Mul(mat a,mat b)
    {
    	mat ans;
    	memset(ans.a,0,sizeof(ans.a));
    	for(int t=0;t<10;t++)
    	{
    		for(int j=0;j<10;j++)
    		{
    			for(int k=0;k<10;k++)
    			{
    				ans.a[t][j]=(ans.a[t][j]+a.a[t][k]*b.a[k][j])%mod;
    			}
    		}
    	}
    	return ans;
    }
    mat ans;
    
    ll quick(ll n)
    {
    	mat res;
    	memset(res.a,0,sizeof(res.a));
    	for(int t=0;t<10;t++)
    	{
    		res.a[0][t]=op[t];
    	}
    	res.a[1][0]=1;
    	res.a[2][1]=1;
    	res.a[3][2]=1;
    	res.a[4][3]=1;
    	res.a[5][4]=1;
    	res.a[6][5]=1;
    	res.a[7][6]=1;
    	res.a[8][7]=1;
    	res.a[9][8]=1;
    	while(n)
    	{
    		if(n&1)
    		{
    			ans=Mul(res,ans);
    		}
    		res=Mul(res,res);
    		n>>=1;
    	}
    	return ans.a[0][0];
    }
    int main()
    {
    	ll n;
    	while(cin>>n)
    	{
    		cin>>mod;
    		for(int t=0;t<10;t++)
    		{
    			scanf("%lld",&op[t]);
    		}
    		if(n<10)
    		{
    			printf("%lld
    ",n%mod);
    		}
    		else{
    		
    		for(int t=9;t>=0;t--)
    		{
    			ans.a[9-t][0]=t;
    		}
    		printf("%lld
    ",quick(n-9)%mod);
    	    }
    	}
    
       return 0;
    }
    
  • 相关阅读:
    PC-BSD 9.2 发布,基于 FreeBSD 9.2
    Attic 0.8.1 发布,备份程序
    Apache Lucene 4.5 发布,Java 搜索引擎
    Linux Kernel 3.11.4/3.10.15/3.4.65/3.0.99
    Lucene 查询工具 LQT
    Rubinius 2.0 发布,Ruby 虚拟机
    Golang通过Thrift框架完美实现跨语言调用
    微软再次要求Google审查官方链接 称将进行调查
    TCPDF 6.0.036 发布,PHP 的 PDF 操作包
    libnode 0.4.0 发布,C++ 语言版的 Node.js
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10781752.html
Copyright © 2020-2023  润新知