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
 首先想到了暴力:
#include<stdio.h>
int main(void)
{
int a, b, n;
int f1, f2, f3;
while()
{
scanf("%d%d%d", &a, &b, &n);
if(a== && b== && n==)
break;
f1 = ;
f2 = ;
f3 = ;
for(int i = ; i <= n; i++)
{
f3 = (a*f2 + b*f1)%;
f1 = f2;
f2 = f3;
}
printf("%d\n", f3);
} return ;
}

可惜暴力的后果是WA,再仔细看一下数据范围,n的取值达到一亿。由于数据范围很大,因此这题不适合暴力。

那么这道类似斐波那契数列的题目该如何搞呢?很明显,f(n-1)和f(n-2)的取值只可能有0, 1, 2, 3, 4, 5, 6这七种情况。因此f(n-1)+f(n-2)的组合一共有7*7种情况,这说明什么呢?说明数列(以f3作为第一个数)出现循环最多在第50个数(最坏的情况,可由鸽巢原理得出)。

修改后的程序:

#include<stdio.h>
int main(void)
{
int f1, f2, a, b, n, i;
int arr[]; // 空间够用的话多开点没坏处
while()
{
scanf("%d%d%d", &a, &b, &n);
if(a == && b == && n == )
break;
f1 = f2 = ;
for(i = ; ; i++) // 一定会结束,因为最多在第50个数出现循环,其中i-1是循环周期
{
arr[i] = (a*f2 + b*f1)%;
f1 = f2;
f2 = arr[i];
if(i >= && arr[i-] == arr[] && arr[i] == arr[] )
break;
}
if(n == || n == )
printf("1\n");
else
printf("%d\n", arr[(n-)%(i-)]);
} return ;
}

HDU_1005:Number Sequence的更多相关文章

  1. HDU 1005 Number Sequence

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

  2. POJ 1019 Number Sequence

    找规律,先找属于第几个循环,再找属于第几个数的第几位...... Number Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submi ...

  3. HDOJ 1711 Number Sequence

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. Number Sequence

    Number Sequence   A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) ...

  5. [AX]AX2012 Number sequence framework :(三)再谈Number sequence

    AX2012的number sequence framework中引入了两个Scope和segment两个概念,它们的具体作用从下面序列的例子说起. 法国/中国的法律要求财务凭证的Journal nu ...

  6. KMP - HDU 1711 Number Sequence

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. hdu 1005:Number Sequence(水题)

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

  8. Number Sequence 分类: HDU 2015-06-19 20:54 10人阅读 评论(0) 收藏

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

  9. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

随机推荐

  1. linux下用eclipse开发mapreduce遇到的问题

    Unable to create the selected preference page.org/apache/hadoop/eclipse/preferences/MapReducePrefere ...

  2. linux 三剑客命令(grep,sed ,awk)

    grep 命令 :强大的文本’搜索’工具    1.grep   -n   'word'  file_name 在file_name文件中找到word所在的所有行并显示.-n 为显示行号.     2 ...

  3. BZOJ3339&&3585 Rmq Problem&&mex

    BZOJ3339&&3585:Rmq Problem&&mex Description 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一个区间内最 ...

  4. IOException parsing XML document from ServletContext resource

    错误是我们学习的机会,不要错过明白明这个错误原因的机会,那么我们就可以更加深刻得理解这个问题. 在启动springmvc的程序去访问的时候,报IO异常,一般情况下IO异常就是文件找不到. 详细错误如下 ...

  5. bzoj 1093 [ZJOI2007]最大半连通子图——缩点+拓扑

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1093 缩点+拓扑,更新长度的时候维护方案数. 结果没想到处理缩点后的重边,这样的话方案数会算 ...

  6. jquery ajax跨越

    JSONP是一个非官方的协议,它允许在服务器端集成Script tags返回至客户端,通过javascript callback的形式实现跨域访问 1.jsonp之$.ajax js $.ajax({ ...

  7. js的模块化写法

    记得前两天自己写一个动画首页,动画很复杂,我用的fullpage虽然相对比较简单,但是每个页面的animation各有差异,需要相对控制,估计有上千行的js代码,写的心情乱糟糟的. 如何让代码量巨大, ...

  8. ACMer之歌

    <<死了都要编>> 死了都要编 不动态规划不痛快 算法多深只有这样 才足够表白 死了都要编 不A星算法不痛快 宇宙毁灭星还在 把每天当成是比赛来编程 一分一秒都编到汗水掉下来 ...

  9. Java review-basic2

    1.Implement a thread-safe (blocking) queue: Class Producer implements Runable{ Private final Blockin ...

  10. mysql过多sleep连接 修改timeout配置节约连接数 配置连接数

    数据库连接数量我设置了16384,最大值 ; 对于mysql8在设置一下这个 SET GLOBAL mysqlx_max_connections = ; 可以使用 命令查看自己的设置 SHOW var ...