hdoj.2256 Problem of Precision【矩阵快速幂】 2015/08/18
2015-08-18 11:15
274 查看
Problem of Precision
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1043 Accepted Submission(s): 612
[align=left]Problem Description[/align]
[align=left]Input[/align]
The first line of input gives the number of cases, T. T test cases follow, each on a separate line. Each test case contains one positive integer n. (1 <= n <= 10^9)
[align=left]Output[/align]
For each input case, you should output the answer in one line.
[align=left]Sample Input[/align]
3 1 2 5
[align=left]Sample Output[/align]
9 97 841
[align=left]Source[/align]
HDOJ 2008 Summer Exercise(4)- Buffet Dinner
注:(√2+√3)^2n
= ((√2+√3)^2)^n
= (5+2√6)^n
由于√6消除不掉,设(5+2√6)^n
= an+bn√6
则第n+1项
a(n+1) + b(n+1)√6 = (5+2√6)*(an
+ bn√6) = (5an+12bn) + (2an+5bn)√6
即a(n+1)
= 5an+12bn,,b(n+1) = 2an+5bn
可由此构建矩阵
|
5 12 | *| an | = | a(n+1) |
|
2 5 | | bn | | b(n+1) |
再
(5+2√6)^n 向下取整 首先double不可取余操作
故令
(5+2√6)^n + (5-2√6)^n
= (an+bn√6) + (an-bn√6)
= 2an
又(5-2√6) ≈
0.101020... (5-2√6)^n 无限趋近于 0
所以(5+2√6)^n
向下取整 即为 2an-1
#include<iostream> #include<cstdio> #include<cstring> using namespace std; struct node{ int m[2][2]; }res,per; int t,n; const int m = 1024; void init(){ per.m[0][0] = per.m[1][1] = 1; per.m[0][1] = per.m[1][0] = 0; res.m[0][0] = res.m[1][1] = 5; res.m[0][1] = 12; res.m[1][0] = 2; } node mutil(node a,node b){ int i,j,k; node c; for( i = 0 ; i< 2 ; ++i ) for( j = 0 ; j < 2 ; ++j ){ c.m[i][j] = 0; for( k = 0 ; k < 2 ; ++k ) c.m[i][j] += (a.m[i][k] * b.m[k][j])%m; c.m[i][j] %= m; } return c; } void modefy( int k ){ node ans = per,p = res; while( k ){ if( k&1 ) ans = mutil(ans,p); p = mutil(p,p); k>>=1; } // for( int i = 0 ; i < 2 ; ++i ) // printf("---- %d %d\n",ans.m[i][0],ans.m[i][1]); printf("%d\n",(10*ans.m[0][0] + 4*ans.m[0][1] - 1) % m); } int main(){ init(); scanf("%d",&t); while(t--){ scanf("%d",&n); if( n == 1 ) printf("9\n"); else modefy(n-1); } return 0; }
相关文章推荐
- hdoj 3342 Legal or Not 【拓朴排序】
- vijos - P1447开关灯泡 (大数模板 + 找规律 + 完全数 + python)
- 19.6.1 Partitioning Keys, Primary Keys, and Unique Keys 分区键,主键,和唯一健
- Hdu oj 2066 一个人的旅行
- #程序猿之路需要自己来铺垫#
- 第六章、输入输出系统
- JQuery实现数字滚动增加的效果
- Component Object Model
- Build your first Django site - QA
- Android Material Design ProgressBar 使用经验(导入,颜色自定义等)
- C++常见内存错误汇总
- Java中的super()研究
- PCB焊盘、过孔、走线、去耦技术
- java 类集框架总结
- 关于ota搭建的苹果官方文档
- 哈希表 之 接入与身份认证技术概述
- 输入框 提示剩余可输入文字
- linux 下 apache启动、停止、重启命令
- 【MINA】用mina做业务服之间的通信,实现业务负载均衡思路
- 问题:table 可否实现对角线;结果:HTML 斜线 表头