hdu 1005 Number Sequence
2015-09-08 18:32
441 查看
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005
矩阵快速幂。。
矩阵快速幂。。
[code]#include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<vector> #include<map> using std::map; using std::min; using std::find; using std::pair; using std::vector; using std::multimap; #define pb(e) push_back(e) #define sz(c) (int)(c).size() #define mp(a, b) make_pair(a, b) #define all(c) (c).begin(), (c).end() #define iter(c) __typeof((c).begin()) #define cls(arr, val) memset(arr, val, sizeof(arr)) #define cpresent(c, e) (find(all(c), (e)) != (c).end()) #define rep(i, n) for(int i = 0; i < (int)n; i++) #define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i) const int M = 7; const int N = 100001; const int INF = 0x3f3f3f3f; struct Matrix { typedef vector<int> vec; typedef vector<vec> mat; inline mat mul(mat &A, mat &B) { mat C(sz(A), vec(sz(B[0]))); rep(i, sz(A)) { rep(k, sz(B)) { rep(j, sz(B[0])) { C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % M; } } } return C; } inline mat pow(mat &A, int n) { mat ret(sz(A), vec(sz(A[0]))); rep(i, sz(A)) ret[i][i] = 1; while(n) { if(n & 1) ret = mul(ret, A); A = mul(A, A); n >>= 1; } return ret; } inline void solve(int a, int b, int n) { mat ans(2, vec(2)); ans[0][0] = a, ans[0][1] = b; ans[1][0] = 1, ans[1][1] = 0; ans = pow(ans, n - 2); printf("%d\n", (ans[0][0] + ans[0][1]) % M); } }go; int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w+", stdout); #endif int a, b, n; while(~scanf("%d %d %d", &a, &b, &n), a + b + n) { if(1 == n || 2 == n) { puts("1"); continue; } go.solve(a, b, n); } return 0; }
相关文章推荐
- Android Bluetooth Stack: Bluedroid(五岁以下儿童):The analysis of A2DP Source
- 实时获取UITextFiled和UIDataPicker等的显示数值
- Netlink Protocol Library Suite (libnl)
- uitableview 刷新一行
- IOS 自动布局指南1 介绍(auto layout guide)
- UVA 11235 Frequent values (RMQ )
- Core Data Programming Guid
- iOS UITabBarController按钮凸起效果实现
- 开源项目分析之UIL
- easyui 模糊匹配解决办法 Firefox不会自动匹配
- guibin
- values语言支持文件命名格式
- Algorithms—95.Unique Binary Search Trees II
- virtualbox增强功能-VBoxGuestAdditions安装
- quick 3.3加载Spine问题,quick3.3加载spine
- Android view中的requestLayout和invalidate方法
- easyUI合并行的单元格
- java.lang.IllegalArgumentException: addChild: Child name '/SSHE' is not unique
- Caused by: java.lang.IllegalArgumentException: addChild: Child name 'localhost' is not unique
- confluence 配置