Hdu 1005 解题报告
2015-04-18 11:27
387 查看
Problem Description
A number sequence is defined as follows:
f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and n, you are to calculate the value of f(n).
Input
The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.
Output
For each test case, print the value of f(n) on a single line.
Sample Input
1 1 3
1 2 10
0 0 0
Sample Output
2
5
话说看到这道题,我看到他的表达式和N的范围,第一个想到的是矩阵乘法加快速幂。。时间复杂度O(logn)。。然后我写了一天(其实没有一天,就半小时),然后调试了好久(因为最近事多,时间太零碎了),然后做出来,高高兴兴的提交了。。然后悲剧的TLE。。话说hdu的题目就是这么恶心人。格式控制,特殊情况判断。。以下是我第一次提交的代码。。
悲剧了之后,我就今讨论区看了看,发现几乎没人用矩阵(是不是太低端了= =#),几乎都是用的数论找周期然后。。。神奇的变成了%49。。。
看了看大神的证明。。然后懂了。hdu1005 Number Sequence 这里证明。下面贴我的代码。。。
A number sequence is defined as follows:
f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and n, you are to calculate the value of f(n).
Input
The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.
Output
For each test case, print the value of f(n) on a single line.
Sample Input
1 1 3
1 2 10
0 0 0
Sample Output
2
5
话说看到这道题,我看到他的表达式和N的范围,第一个想到的是矩阵乘法加快速幂。。时间复杂度O(logn)。。然后我写了一天(其实没有一天,就半小时),然后调试了好久(因为最近事多,时间太零碎了),然后做出来,高高兴兴的提交了。。然后悲剧的TLE。。话说hdu的题目就是这么恶心人。格式控制,特殊情况判断。。以下是我第一次提交的代码。。
#include <stdio.h>#define MOD 7void calc(int a[][2], int b[][2]){ int c[2][2], i, j; for(i = 0; i < 2; i++) for(j = 0; j < 2; j++) c[i][j] = a[i][0]*b[0][j] + a[i][1]*b[1][j]; for(i = 0; i < 2; i++) for(j = 0; j < 2; j++) a[i][j] = c[i][j] % MOD;}int main(){ int a, b, n; while(scanf("%d%d%d", &a, &b, &n), (a||b||n)) { a %= MOD; b %= MOD; int mat[2][2] = {0, b, 1, a}; int ans[2][2] = {1, 1, 0, 0}; for(--n; n; n >>= 1){ if(n&1) calc(ans, mat); calc(mat, mat); } printf("%d\n",ans[0][0]); } return 0;}为什么会悲剧呢? 因为数据中很多n==1 和 n==2 的情况,然后用矩阵乘法得不偿失。。所以加上特判就过了(这是我后来看别人的博客发现的)
悲剧了之后,我就今讨论区看了看,发现几乎没人用矩阵(是不是太低端了= =#),几乎都是用的数论找周期然后。。。神奇的变成了%49。。。
看了看大神的证明。。然后懂了。hdu1005 Number Sequence 这里证明。下面贴我的代码。。。
#include <stdio.h>#define MOD 7int main(){ int a, b, n, i, F, f[60]; while(scanf("%d%d%d", &a, &b, &n), (a||b||n)) { f[1] = f[2] = 1; for(i = 3; i < 50; i++) f[i] = (a*f[i-1] + b*f[i-2]) % MOD; printf("%d\n",f[n%49]); } return 0;}在此之前,我先提交的我的矩阵乘法版本的AC代码(就加了个特判 笑cry):
#include <stdio.h> #define MOD 7 void calc(int a[][2], int b[][2]) { int c[2][2], i, j; for(i = 0; i < 2; i++) for(j = 0; j < 2; j++) c[i][j] = a[i][0]*b[0][j] + a[i][1]*b[1][j]; for(i = 0; i < 2; i++) for(j = 0; j < 2; j++) a[i][j] = c[i][j] % MOD; } int main() { int a, b, n; while(scanf("%d%d%d", &a, &b, &n), (a||b||n)) { if(n == 1 || n == 2){ printf("%d\n", 1); continue; } a %= MOD; b %= MOD; int mat[2][2] = {0, b, 1, a}; int ans[2][2] = {1, 1, 0, 0}; for(--n; n; n >>= 1){ if(n&1) calc(ans, mat); calc(mat, mat); } printf("%d\n",ans[0][0]); } return 0; }然后是我发现要用特判的那个博客:hdu 1005_尉传庆_新浪博客
相关文章推荐
- HDU 4876 ZCC loves cards (2014多校联合训练第二场1005) 解题报告(暴力+剪枝)
- hdu 1005解题报告
- hdu 1005解题报告
- HDU 1005 解题报告
- HDU解题报告--1005
- HDU 1075 Trie树 解题报告
- HDU 1018 Big Number数论(解题报告)
- hdu 2058 解题报告 - The sum problem
- HDU 1465 解题报告
- 杭电acm1056 hdu-acm-1056解题报告
- Hdu 2036 之解题报告
- Hdu 2042(水题)之解题报告
- HDU 2544 SPFA(SLF) 解题报告
- Hdu 2103解题报告
- HDU 1796 How many integers can you find 解题报告(数论)
- BestCoder20 1002.lines (hdu 5124) 解题报告
- hdu 2022 海选女主角 解题报告
- HDU - 1532 Drainage Ditches解题报告(网络流 最大流魔板题)
- hdu 1556 Color the ball 区间修改查询 解题报告
- hdu 1087 解题报告 Super Jumping! Jumping! Jumping!