输出
The number of all situations of no-continuous up of the coin,moduled by 10000.
这个题也是一个寻找循环节的一个典例,当然在这里还是再说一下循环节的前置条件(也可见我的上一篇帖子“寻找循环节I”)---递推相邻的两项之间的值比较连续,换句话说,就是相邻两项不会相隔太远,而且还要满足的条件就是题目要求我们取模,这个MOD的值一定不能太大,太大的话就不能用循环节的方法了,,一定的用其他(比如说矩阵快速幂)的方法求解;这个是非常要注意的!
下面就先看一下代码吧:
#include <stdio.h>
int a[15001] = {0,2,3}; //15000是一个循环节,直接用15000作为数组的最大长度即可;
int main() {
? ? int t,n;
? ? scanf("%d",&t);
? ? for(int i=3; i<=15001; i++) {
? ? ? ? a[i] = (a[i-1] + a[i-2])%10000; //局部取模---分治思想
? ? ? ? /* ? ? ? ? ? ? ? ? ? ?
? ? ? ? if(a[i] == 2 && a[i-1] == 1) { ? ?//寻找循环节的过程;
? ? ? ? ? ? printf("%dn",i); ?//此时输出15001,i-1的值为15000,即为循环节;
? ? ? ? }
? ? ? ? */
? ? }
? ? while(t--) {
? ? ? ? scanf("%d",&n);
? ? ? ? printf("%dn",a[n%15000]); //考虑到n的范围很大,所以要先对n进行取模之后再输出a[n]的值;
? ? }
}
后续解法待续。。。