hdu1005 循环节||矩阵快速幂取模
2015-11-26 21:15
218 查看
Number Sequence
2000/1000 MS (Java/Others) 65536/32768 K (Java/Others)
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
Author
CHEN, Shunbao
1.找循环节
给出了运算公式的数学,凡是没有优化的话,超时超内存等等是避免不了的了。这题很显然是一个找规律的题目,也就是该题的求解中是存在循环节的。
对于公式 f
= A * f[n-1] + B * f[n-2]; 后者只有7 * 7 = 49 种可能,因为对于f[n-1] 或者 f[n-2] 的取值只有 0,1,2,3,4,5,6 这7个数,A,B又是固定的,所以就只有49种可能值了。由该关系式得知每一项只与前两项发生关系,所以当连续的两项在前面出现过循环节出现了,注意循环节并不一定会是开始的 1,1 。 又因为一组测试数据中f
只有49中可能的答案,最坏的情况是所有的情况都遇到了,那么那也会在50次运算中产生循环节。找到循环节后,就可以轻松解决了。
矩阵快速幂取模
2000/1000 MS (Java/Others) 65536/32768 K (Java/Others)
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
Author
CHEN, Shunbao
1.找循环节
给出了运算公式的数学,凡是没有优化的话,超时超内存等等是避免不了的了。这题很显然是一个找规律的题目,也就是该题的求解中是存在循环节的。
对于公式 f
= A * f[n-1] + B * f[n-2]; 后者只有7 * 7 = 49 种可能,因为对于f[n-1] 或者 f[n-2] 的取值只有 0,1,2,3,4,5,6 这7个数,A,B又是固定的,所以就只有49种可能值了。由该关系式得知每一项只与前两项发生关系,所以当连续的两项在前面出现过循环节出现了,注意循环节并不一定会是开始的 1,1 。 又因为一组测试数据中f
只有49中可能的答案,最坏的情况是所有的情况都遇到了,那么那也会在50次运算中产生循环节。找到循环节后,就可以轻松解决了。
#include<iostream> #include<cstring> using namespace std; int main() { int a,b,n; int f[55],flaga[10][10]; while(cin>>a>>b>>n&&(a||b||n)) { memset(flaga,0,sizeof(flaga)); f[1]=1;f[2]=1; int xhj=1; //循环节的终点 int af,bf; for(int i=3;i<=n;i++) { af=a*f[i-1]%7,bf=b*f[i-2]%7; f[i]=(af+bf)%7; if(flaga[af][bf]==1) { xhj=i; break; } flaga[af][bf]=1; } int xhjb=3; for(int i=3;i<=n;i++) { if(af==a*f[i-1]%7&&bf==b*f[i-2]%7) { xhjb=i; //循环的起始位置 break; } } if(n>=xhjb) cout<<f[(n-xhjb)%(xhj-xhjb)+xhjb]<<endl; else cout<<f <<endl; } }
矩阵快速幂取模
#include<iostream> using namespace std; //可以用二维数组名作为实参或者形参,在被调用函数中对形参数组定义时可以指定所有维数的大小 //也可以省略第一维的大小说明 //但是不能把第二维或者更高维的大小省略 。。。居然忘了,搞了会儿,要命啊!! void matrix_multi(int a[2][2],int b[2][2]) { int temp[2][2]; temp[0][0]=(a[0][0]*b[0][0]+a[0][1]*b[1][0])%7; temp[0][1]=(a[0][0]*b[0][1]+a[0][1]*b[1][1])%7; temp[1][0]=(a[1][0]*b[0][0]+a[1][1]*b[1][0])%7; temp[1][1]=(a[1][0]*b[0][1]+a[1][1]*b[1][1])%7; a[0][0]=temp[0][0],a[0][1]=temp[0][1],a[1][0]=temp[1][0],a[1][1]=temp[1][1]; } int main() { int a,b,n; while(cin>>a>>b>>n&&(a||b||n)) { int m=n; int e[2][2]={1,0, 0,1}; int mx[2][2]={a,b, 1,0}; n-=2; while(n>0) //快速幂 { if(n&1) matrix_multi(e,mx); matrix_multi(mx,mx); n/=2; } if(m<=2) cout<<1<<endl; else cout<<(e[0][0]+e[0][1])%7<<endl; } }
相关文章推荐
- [Unity框架]PureMVC基础
- Android基础--ConnectivityManager检查网络连接
- 软件测试W方法
- 关于在vs2013中的一个错误
- Android篇 ListView 优化笔记
- 4.5方法参数
- 使用SndPlaySound从内存中播放WAV
- 基于int的Linux的经典系统调用实现
- [连载]《C#通讯(串口和网络)框架的设计与实现》- 5.串口和网络统一IO设计
- 约瑟夫报数出列题
- 《leetCode》:Reverse Linked List II
- 图像的频率域高斯低通滤波
- XMPP协议实现原理介绍
- MIT算法导论-第二讲-渐进符号,递归及解法
- Saltstack的Grains和Pillar比较
- android canvas详解
- spring bean 的生存范围 单例 还是原型
- POJ3041Asteroids(最小点覆盖+有点小抽象)
- android学习ScrollView的使用
- SSRF漏洞的挖掘经验