HDU - 1005 Number Sequence 周期是48?不,是336!
2016-09-30 02:11
441 查看
题目:
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
Sample Output
显然,这是一个找周期的问题。
我们很容易推出,一定有周期,而且最小周期不超过49.
(下面的论述中,都假设a和b小于7,即已经mod7)
而且,如果a、b不全为0,那么一定是从(1,1)(即第1项)开始循环的
当a=b=0时,不是从第1项开始,而是从第3项开始循环。
我最开始的方法是,对于每组给的a、b,求出最小正周期
代码:
这个是AC了的,没有任何问题。
其中,当b=0的时候,6一定是周期,这是费马小定理。
当a=0的时候,恰好奇数列和偶数列是一样的,而且6一定是它们的周期,所以12是数列的周期。
例如,当a=0,b=3时,前14项为1,1,3,3,2,2,6,6,4,4,5,5,1,1,周期为12
然后我发现vj里面有个AC的代码很短,就看了一下,发现他是直接以48为周期。
网上搜了一下,很多人说48一定是周期,甚至还有人说49是周期。。。
但是我枚举了a和b(一共也就49种情况)
代码:
结果是:
很明显,有14、21、42的存在,48不是周期!
所以说,应该是VJ给的测试数据很水,所以有很多代码都浑水摸鱼了。
当然,336一定是周期,有了这个信息,也可以写出很简洁的代码了。
代码:
这个也很快,也是0ms AC
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
显然,这是一个找周期的问题。
我们很容易推出,一定有周期,而且最小周期不超过49.
(下面的论述中,都假设a和b小于7,即已经mod7)
而且,如果a、b不全为0,那么一定是从(1,1)(即第1项)开始循环的
当a=b=0时,不是从第1项开始,而是从第3项开始循环。
我最开始的方法是,对于每组给的a、b,求出最小正周期
代码:
#include<iostream> using namespace std; int getT(int a, int b) { if (a == 0 || b == 0)return 12; if ((a + b) % 7 == 1)return 1; int T = 0, x1 = 1, x2 = 1; while (true) { int temp = (b*x1 + a*x2) % 7; x1 = x2; x2 = temp; T++; if (x1 == 1 && x2 == 1)break; } return T; } int main() { int a, b, n; while (cin >> a >> b >> n) { if (n == 0)break; if (a % 7 == 0 && b % 7 == 0) { cout << (n<3) << endl; continue; } int x1 = 1, x2 = 1; if (n>20)n = (n - 20) % getT(a % 7, b % 7) + 20; if (n > 2)n -= 2; else n = 0; while (n--) { int temp = (b*x1 + a*x2) % 7; x1 = x2; x2 = temp; } cout << x2 << endl; } return 0; }
这个是AC了的,没有任何问题。
其中,当b=0的时候,6一定是周期,这是费马小定理。
当a=0的时候,恰好奇数列和偶数列是一样的,而且6一定是它们的周期,所以12是数列的周期。
例如,当a=0,b=3时,前14项为1,1,3,3,2,2,6,6,4,4,5,5,1,1,周期为12
然后我发现vj里面有个AC的代码很短,就看了一下,发现他是直接以48为周期。
网上搜了一下,很多人说48一定是周期,甚至还有人说49是周期。。。
但是我枚举了a和b(一共也就49种情况)
代码:
int main() { for (int i = 0; i < 7; i++) { for (int j = 0; j < 7; j++)cout << getT(i, j) << " "; cout << endl; } return 0; }
结果是:
很明显,有14、21、42的存在,48不是周期!
所以说,应该是VJ给的测试数据很水,所以有很多代码都浑水摸鱼了。
当然,336一定是周期,有了这个信息,也可以写出很简洁的代码了。
代码:
#include<iostream> using namespace std;
int main() { int a, b, n; while (cin >> a >> b >> n) { if (n == 0)break; if (a % 7 == 0 && b % 7 == 0) { cout << (n<3) << endl; continue; } int x1 = 1, x2 = 1; n = (n + 333) % 336 + 1; while (n--) { int temp = (b*x1 + a*x2) % 7; x1 = x2; x2 = temp; } cout << x2 << endl; } return 0; }
这个也很快,也是0ms AC
相关文章推荐
- hdu 1005 Number Sequence(找周期)
- 51nod 1126 求递推序列的第N项 && hdu - 1005 Number Sequence (求周期)
- 【HDU 1005】Number Sequence —— 找周期
- HDU 1005 Number Sequence(循环周期是关键)
- HDU 1005 Number Sequence 寻找周期
- HDU-1005-Number Sequence(Java版+考虑周期+恶心水题!)
- HDU 1005 Number Sequence 矩阵乘法 Fib数列
- HDU 1005 Number Sequence
- HDU 1005 Number Sequence
- hdu-1005-Number Sequence
- HDU 1005 Number Sequence(矩阵快速幂、循环节)
- hdu 1005 Number Sequence
- HDU 1005 Number Sequence
- HDU-1005-Number Sequence
- HDU-1005 Number Sequence【矩阵二分幂】
- HDU 1005 Number Sequence
- HDU_1005 Number Sequence
- HDU 1005 Number Sequence
- HDU 1005 Number Sequence(矩阵快速幂)
- 【HDU】-1005-Number Sequence(数学,规律)