【矩阵快速幂-求平方根表达式的值】HDU Problem of Precision 2256
2015-09-20 21:23
267 查看
Problem of PrecisionTime Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1055 Accepted Submission(s): 622 Problem Description Input 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) Output For each input case, you should output the answer in one line. Sample Input 3 1 2 5 Sample Output 9 97 841 Source HDOJ 2008 Summer Exercise(4)- Buffet Dinner |
简单明了。不再解释。
解题思路:
盗图。。这图讲的足够了。
矩阵:
AC代码:
#include <stdio.h> #include <string.h> #include <algorithm> #define MOD 1024 using namespace std; typedef int LL; LL Mat[2][2]; LL res[2][2]; void init() { for(int i=0;i<2;i++){ for(int j=0;j<2;j++){ res[i][j]=(i==j); } } Mat[0][0]=5;Mat[0][1]=2; Mat[1][0]=12;Mat[1][1]=5; } void Matmul(LL a[2][2],LL b[2][2]) { LL t[2][2]={0}; for(int i=0;i<2;i++){ for(int k=0;k<2;k++){ if(a[i][k]){ for(int j=0;j<2;j++){ t[i][j]=(t[i][j]+a[i][k]*b[k][j]%MOD)%MOD; } } } } for(int i=0;i<2;i++) for(int j=0;j<2;j++) a[i][j]=t[i][j]; } void Matrix(LL x[2][2],LL n) { while(n){ if(n&1) Matmul(res,x); Matmul(x,x); n>>=1; } } int main() { int t; scanf("%d",&t); while(t--){ LL n; scanf("%d",&n); init(); Matrix(Mat,n-1); LL ans[2]; LL cnt=0; ans[0]=5;ans[1]=2; for(int i=0;i<2;i++){ cnt=(cnt+ans[i]*res[i][0]%MOD)%MOD; } printf("%d\n",(2*cnt-1)%MOD); } return 0; }
Problem of PrecisionTime Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1055 Accepted Submission(s): 622 Problem Description Input 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) Output For each input case, you should output the answer in one line. Sample Input 3 1 2 5 Sample Output 9 97 841 Source HDOJ 2008 Summer Exercise(4)- Buffet Dinner |
相关文章推荐
- mallet环境配置
- 区间专题
- leetcode - N-Queens
- 杭电2539点球大战
- 可能导致Java内存泄漏的几项原因
- 自己掌控自己,精而少
- unresolved external symbol错误及其解决方法
- C++ 11 学习3:显示虚函数重载(override)
- C++ 11 学习2:空指针(nullptr) 和 基于范围的for循环(Range-based for loops)
- C++ 11 学习1:类型自动推导 auto和decltype
- 提交Sublime Text 插件到Package Control
- 黑马程序员——集合框架(Collection)
- 百度地图获取地址信息由纬度和经度
- mysql命令大全
- 第二节 基本概念及操作
- 求长度
- 今目标——让我们离成功更近……
- dockerfile 使用方法
- 指针寻找最大小值
- 写lua时需要注意的地方