博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 5015 233 Matrix(网络赛1009) 矩阵快速幂
阅读量:6185 次
发布时间:2019-06-21

本文共 3335 字,大约阅读时间需要 11 分钟。

先贴四份矩阵快速幂的模板:

 

 

233 Matrix

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 257    Accepted Submission(s): 165

Problem Description
   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,a
0,2 = 2333,a
0,3 = 23333...) Besides, in 233 matrix, we got a
i,j = a
i-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,...,a
n,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
Hint
 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:            

 

 题解1:

题解2:

题目分析:矩阵快速幂,构建一个如下的矩阵即可:

 

[cpp]
  1. n+2行的矩阵  
  2. --                      --   --  --  
  3. | 1  1  1  1  1  1  1  0 |   | a1 |  
  4. | 0  1  1  1  1  1  1  0 |   | a2 |  
  5. | 0  0  1  1  1  1  1  0 |   | a3 |  
  6. | 0  0  0  1  1  1  1  0 |   | a4 |  
  7. | 0  0  0  0  1  1  1  0 | * | a5 |  
  8. | 0  0  0  0  0  1  1  0 |   | an |  
  9. |  - - - - - - - - - - - |   |    |  
  10. | 0  0  0  0  0  0 10  1 |   | 233|  
  11. | 0  0  0  0  0  0  0  1 |   | 3  |  
  12. --                      --   --  --  

 

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 11 #define N 15 12 #define M 15 13 #define mod 10000007 14 #define p 10000007 15 #define mod2 100000000 16 #define ll long long 17 #define LL long long 18 #define maxi(a,b) (a)>(b)? (a) : (b) 19 #define mini(a,b) (a)<(b)? (a) : (b) 20 21 using namespace std; 22 23 ll nn,m; 24 ll n; 25 ll x[15]; 26 //ll ans; 27 28 struct Mat 29 { 30 ll mat[N][N]; 31 }; 32 33 Mat e,f,g; 34 Mat operator * (Mat a,Mat b) 35 { 36 Mat c; 37 memset(c.mat,0,sizeof(c.mat)); 38 ll i,j,k; 39 for(k =0 ; k < n ; k++) 40 { 41 for(i = 0 ; i < n ;i++) 42 { 43 if(a.mat[i][k]==0) continue;//优化 44 for(j = 0 ;j < n ;j++) 45 { 46 if(b.mat[k][j]==0) continue;//优化 47 c.mat[i][j] = (c.mat[i][j]+(a.mat[i][k]*b.mat[k][j])%mod)%mod; 48 } 49 } 50 } 51 return c; 52 } 53 Mat operator ^(Mat a,ll k) 54 { 55 Mat c; 56 ll i,j; 57 for(i =0 ; i < n ;i++) 58 for(j = 0; j < n ;j++) 59 c.mat[i][j] = (i==j); 60 for(; k ;k >>= 1) 61 { 62 if(k&1) c = c*a; 63 a = a*a; 64 } 65 return c; 66 } 67 68 69 void ini() 70 { 71 ll i,j; 72 for(i=1;i<=nn;i++){ 73 scanf("%I64d\n",&x[i]); 74 } 75 memset(e.mat,0,sizeof(e.mat)); 76 memset(f.mat,0,sizeof(f.mat)); 77 e.mat[0][0]=233; 78 e.mat[0][1]=3; 79 e.mat[0][2]=233+x[1]; 80 for(i=2;i<=nn;i++){ 81 e.mat[0][i+1]=e.mat[0][i]+x[i]; 82 } 83 for(j=0;j
1){100 g= e* (f^(m-1) );101 }102 else{103 g.mat[0][nn+1]=e.mat[0][nn+1];104 }105 }106 107 void out()108 {109 printf("%I64d\n",g.mat[0][nn+1]);110 }111 112 int main()113 {114 // freopen("data.in","r",stdin);115 // freopen("data.out","w",stdout);116 //scanf("%d",&T);117 //for(int cnt=1;cnt<=T;cnt++)118 // while(T--)119 while(scanf("%I64d%I64d",&nn,&m)!=EOF)120 {121 ini();122 solve();123 out();124 }125 126 return 0;127 }

 

转载于:https://www.cnblogs.com/njczy2010/p/3972316.html

你可能感兴趣的文章
读《疯狂的程序员》
查看>>
perl和python的区别
查看>>
escarpins pas cher dominateur découverte.
查看>>
代码质量管理平台Sonar
查看>>
×××服务器配置详解(二)
查看>>
log4j:WARN Please initialize the log4j system properly.警告问题
查看>>
升级ESXi Host
查看>>
Windows8消费者预览版的安装
查看>>
window2003 svchost cpu占用100% 解决方法
查看>>
ipod无法使用无线网络问题分析
查看>>
mysql 表大小写
查看>>
我的友情链接
查看>>
Linux下安装并(单节点)配置启动Kafka
查看>>
Vert.x 提供web API 译<八>
查看>>
gcc 降低版本
查看>>
YII Framework学习教程-YII的Modules(模块化)
查看>>
iOS: 在iPhone和Apple Watch之间共享数据 App Groups
查看>>
Zabbix应用之Server/Agent部署
查看>>
添加PaloAlto 8.0到EVE-NG
查看>>
开源大数据处理工具汇总(上)
查看>>