您的位置:首页 > 其它

HDU 4291 A Short problem

2015-09-17 14:10 471 查看

A Short problem

Time Limit: 1000ms
Memory 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: