题目链接

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的范围,n的范围是比较大的,我们虽然用数组可以存贮下来但是却会超内存,那么这个问题应该怎么解决的?

最后要求求得的答案对于7取余,那么所有的f(i)肯定都是0~6之内的数字,这样的话我们应该可以找到f(i)和f(i-1)使得两个值均为1,这样的话也就相当于又回到了最起始的状态,也就可以认为函数f()时一个以i-2位周期的周期函数。

知道周期函数后,将n的值对应成周期函数的下标。

代码:

#include<string.h>
#include<stdio.h>
#include<iostream>
using namespace std;
int c[200];
int a,b,n; int main()
{
c[1]=1;
c[2]=1;
while(~scanf("%d%d%d",&a,&b,&n)&&a&&b&&n)
{
if(n>=3)
{
int i;
for(i=3; i<200; i++)
{
c[i]=(a*c[i-1]+b*c[i-2])%7;
if(c[i]==1&&c[i-1]==1)//又回归到最起始的状态,相当于在c这个数组里面i-2个数为一个周期
break;
}
i-=2;//周期长度
n=n%i;//需要求的是这个周期里面的第几个数
if(n==0)
printf("%d\n",c[i]);
else
printf("%d\n",c[n]);
}
else printf("1\n");
}
return 0;
}

HDU 1005 Number Sequence (模拟)的更多相关文章

  1. HDU 1005 Number Sequence(数列)

    HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

  2. HDU 1005 Number Sequence(数论)

    HDU 1005 Number Sequence(数论) Problem Description: A number sequence is defined as follows:f(1) = 1, ...

  3. HDU - 1005 Number Sequence 矩阵快速幂

    HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...

  4. HDU 1005 Number Sequence【多解,暴力打表,鸽巢原理】

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. HDU 1005 Number Sequence

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  6. hdu 1005:Number Sequence(水题)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. HDU 1005 Number Sequence【斐波那契数列/循环节找规律/矩阵快速幂/求(A * f(n - 1) + B * f(n - 2)) mod 7】

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  8. HDU 1005 Number Sequence(矩阵)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  9. HDU - 1005 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)) mo ...

随机推荐

  1. [2017BUAA软工助教]团队beta得分总表

    一.累计得分 项目 α例会 α发布 α测试 α展示 α事后 合计 满分 50 10 10 150 10 230 hotcode5 50 10 9 150 9 228 弗朗明哥舞步 50 10 8 13 ...

  2. 我们的团队-IT梦想队

    IT梦想队 队长:李遇塘 队员:王长.周兴荣.朱岭杰.马婧婧 团队宣言:  一匹狼战斗力低,但一群狼的我们无所畏惧!李遇塘http://www.cnblogs.com/Liyutang/ 王 长htt ...

  3. Jmeter put 方法总结

    1.百度到很多关于jmeter put 方法的使用 ,但觉得都完全 下面我大致总结下 : >1.放入 url 中 如:www.*****.com?a=1&b=2 ; >2.放入到p ...

  4. [转帖]Windows 使用netsh 命令行方式处理 windows防火墙的方法

    Windows防火墙命令行手册 https://blog.csdn.net/mystudyblog0507/article/details/79617629 简介 netsh advfirewall ...

  5. Kafka数据可靠性深度解读

    原文链接:http://www.infoq.com/cn/articles/depth-interpretation-of-kafka-data-reliability Kafka起初是由Linked ...

  6. SVN Update Error: Please execute the 'Cleanup' command

    尝试用下面两种方法 svn clean up 中有一个选项break lock勾选上 把对应的文件来里的.svn里面的lock文件删除. svn local delete, incoming dele ...

  7. js new关键字

    实现new 关键字只需4步 1. 声明一个对象: 2. 把这个对象的__proto__ 指向构造函数的 prototype; 3. 以构造函数为上下文执行这个对象: 4. 返回这个对象. 简洁的代码示 ...

  8. pgm9

    这部分介绍 sampling 方法,书上也称为 particle-based method,这是因为每一个从分布中采集到的样本可以看成是一个 particle(instantiation of r.v ...

  9. String args[] 和 String[] args 有什么区别

    String args[] 和 String[] args 有什么区别 public static void main(String args[]) 或 public static void main ...

  10. Lua 调试库

    Lua 调试库 http://blog.csdn.net/vermilliontear/article/details/50851045 http://blog.csdn.net/vermillion ...