加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

NYLG 698 a coin problem 寻找循环节II

发布时间:2020-12-14 03:54:50 所属栏目:大数据 来源:网络整理
导读:链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=698 描述: One day,Jiameier is tidying up the room,and find some coins. Then she throws the coin to play.Suddenly,she thinks of a problem,that if throw n times coin,how many situations

链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=698

描述:

One day,Jiameier is tidying up the room,and find some coins. Then she throws the coin to play.Suddenly,she thinks of a problem,that if throw n times coin,how many situations of no-continuous up of the coin. Hey,Let's solve the problem.
输入
The first of input is an integer T which stands for the number of test cases. For each test case,there is one integer n (1<=n<=1000000000) in a line,indicate the times of throwing coins.
输出

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]的值;

? ? }

}


后续解法待续。。。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读