HDU 4291 A Short problem
2015-09-17 14:10
471 查看
A Short problem
Time Limit: 1000msMemory Limit: 32768KB
This problem will be judged on HDU. Original ID: 4291
64-bit integer IO format: %I64d Java class name: Main
According to a research, VIM users tend to have shorter fingers, compared with Emacs users.
Hence they prefer problems short, too. Here is a short one:
Given $n (1 \leq n \leq 10^{18})$, You should solve for
\[g(g(g(n))) mod 10^9 + 7\]
where
\[g(n) = 3g(n - 1) + g(n - 2)\]
\[g(1) = 1\]
\[g(0) = 0\]
Input
There are several test cases. For each test case there is an integer n in a single line.Please process until EOF (End Of File).
Output
For each test case, please print a single line with a integer, the corresponding answer to this case.Sample Input
0 1 2
Sample Output
0 1 42837
Source
2012 ACM/ICPC Asia Regional Chengdu Online解题:矩阵快速幂,关键在于如何找到内部循环的的模对象
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int maxn = 3; LL mod = 1000000007; const int n = 2; struct Matrix { LL m[maxn][maxn]; void init() { memset(m,0,sizeof m); } Matrix() { init(); } Matrix operator*(const Matrix &rhs) const { Matrix ret; for(int k = 0; k < n; ++k) { for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) ret.m[i][j] = (ret.m[i][j] + m[i][k]*rhs.m[k][j]%mod)%mod; } return ret; } void set_a() { init(); m[0][0] = 0; m[0][1] = 1; } void set_b() { init(); m[0][0] = 0; m[0][1] = m[1][0] = 1; m[1][1] = 3; } void print() { for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) cout<<m[i][j]<<" "; cout<<endl; } } }; Matrix a,b; LL quickPow(LL index,LL md) { a.set_a(); b.set_b(); mod = md; while(index) { if(index&1) { a = a*b; } index >>= 1; b = b*b; } return a.m[0][0]; } int main() { LL m; while(~scanf("%I64d",&m)) printf("%I64d\n",quickPow(quickPow(quickPow(m,183120),222222224),1000000007)); return 0; }
View Code
相关文章推荐
- 我的Android进阶之旅------>Android使用百度地图时,关于android.permission.BAIDU_LOCATION_SERVICE的声明警告。
- Spring AOP + AspectJ annotation example
- VB截取中英文混合字符串中若干半角单位长字串的函数
- json格式化工具
- QT中监控全局键盘鼠标事件
- Java中的StringUtils类
- 浅谈数据结构-插入排序(直接插入、希尔排序)
- 如何检测 JavaScript 中的自定义全局变量
- 有效减少虚拟机镜像文件的大小
- Win10提示“计算机内存不足”的怎么办 解决办法介绍
- test3.10
- 11.2.04 Oracle RAC 目录中 crfclust.bdb文件过大,Bug 20186278
- android代码获取网站图标
- 移除父控件的所有子控件
- activiti数据库表结构剖析
- Android 三次贝塞尔曲线练习之弹性的圆
- 支付宝添加scheme的方法
- 如何解析Plist文件?
- 使用 WebStorm IDE 调试 Pomelo 应用程序
- iOS开发学习资源