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


上面是题目的简介。

程序思路:

f(n)是一个递推式,A、B为两个输入的参数,n表示迭代次数。以平常接数学题目时的思维来看,很简单,直接套公式,迭代n次肯定可以得到结果,下面是c语言实现代码:

#include "stdio.h"

#pragma warning(disable:4996)

int main()
{
int a, b;
long n;
long i;
int f[100]; f[0] = 1;
f[1] = 1; while (scanf("%d %d %d", &a, &b, &n))
{
if (a < 1 || b > 1000 || n < 1 || n > 100000000)
return -1; for (i = 2;i < n;i++)
{
f[i] = a * f[i - 1] + b * f[i - 2];
f[i] = f[i] % 7;
} printf("%d\n", f[n-1]);
} return 0;
}

然而,考虑到n的次数很大时,迭代次数也增加到相当大,其运算很明显会占用大量的时间。所以需要考虑响应的方法减小计算量,加快程序运行速度。

我们可以观察到公式所有的值都是经过mod7 运算的,通过对7取余,我们可以知道递推结果的数列中每个值的大小都是0~6。每位有7种可能情况,递推公式中有两个数列的值输入,所以有7*7=49 种可能的组合。在所有的组合都出现之后,下一个情况必定是前面出现过的组合中的某一种,一次类推,重复出现,由此可以求到循环的周期。

利用循环周期,就可以很轻松地计算得到第n个对应哪一种情况了。

废话不多说,直接上代码吧。

#include "stdio.h"

#pragma warning(disable:4996)

int main()
{
int a, b, n;
int f[54] = { 0,1,1 };
int pos; while (scanf("%d %d %d", &a, &b, &n) != EOF)
{
if (a < 1 || b > 1000 || n < 1 || n > 100000000)
return -1; for (int i = 3;i < 54;i++)
{
f[i] = (a * f[i - 1] + b * f[i - 2]) % 7; //printf("%d ", f[i]); if (i > 5)
{
if (f[i - 1] == f[3] && f[i] == f[4])
{
pos = i - 4;
break;
}
}
}
//printf("\n"); if (n > 2)
printf("%d\n", f[(n-3) % pos+3]);
else
printf("1\n"); } return 0;
}

补充:

  1. 为计算方便,从数组下标为1的元素对应数列第一个数,舍弃下标为0的那个元素;
  2. 关于循环,网上很多程序都是检测到前后两个分别为1、1的情况时就认为开始下一轮循环了;然而这种方法是错误的,因为如果取特例a=7、b=7时,输出序列为:1、1、0、0、0。。。。后面全部是0,这样程序会持续运行下去,因为不可能再检测到1、1了;所以检测循环时从第3个和第4个开始,检测是否重复:
if (f[i - 1] == f[3] && f[i] == f[4])
{
pos = i - 4;
break;
}

运行结果:

杭电ACM刷题(2):1005,Number Sequence 标签: 杭电acmC语言 2017-05-11 22:43 116人阅读的更多相关文章

  1. 杭电ACM刷题(1):1002,A + B Problem II 标签: acmc语言 2017-05-07 15:35 139人阅读 评

    最近忙于考试复习,没有多少可供自己安排的时间,所以我利用复习之余的空闲时间去刷刷杭电acm的题目,也当对自己编程能力的锻炼吧. Problem Description I have a very si ...

  2. 杭电acm刷题顺序

    最近兴趣来了,闲暇之余,回顾大学期间刷过的杭电acm那些入门级别的题,以此巩固基础知识! 以下参考刷题顺序,避免入坑 原文传送门:https://blog.csdn.net/liuqiyao_01/a ...

  3. 杭电acm刷题(3):1062,Text Reverse 标签: 杭电acm 2017-05-15 08:26 126人阅读 评论(0)

    Problem Description Ignatius likes to write words in reverse way. Given a single line of text which ...

  4. 杭电acm 1076题

    水题,一个求闰年的题目,复习一下闰年的求法.... 1,如果能被4整除但不能被100整除的是闰年 2,能被400整除的是闰年 题目大意是:给定一个开始年份T以及一个正数N,要求求出从T开始,到了哪一年 ...

  5. 杭电acm 1037题

    本题应该是迄今为止最为简单的一道题,只有一组输入,输出也简单.... /****************************************** 杭电acm 1037题 已AC ***** ...

  6. 杭电acm 1038题

    本题比较简单,但是需要掌握几个小技巧,先上代码 /************************************* 杭电ACM 1038题,已AC ********************* ...

  7. 杭电acm 1049题

    一道水题..... 大意是一条1inch的虫子在一个n inch的盒子的底部,有足够的能够每一分钟往上爬u inch,但是需要休息一分钟,这期间会往下掉d inch,虫子爬到盒子口即认为结束.要求计算 ...

  8. 杭电acm 1033题

    Problem Description For products that are wrapped in small packings it is necessary that the sheet o ...

  9. 1005 Number Sequence(HDU)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 Number Sequence Time Limit: 2000/1000 MS (Java/O ...

随机推荐

  1. 条款35:考虑virtual函数以外的其他选择

    有一部分人总是主张virtual函数几乎总应该是private:例如下面这个例子,例子时候游戏,游戏里面的任务都拥有健康值这一属性: class GameCharacter{ public: int ...

  2. Struts2(2)

    一.分模块开发:单独写模块的配置文件,把配置文件引入到核心配置文件中 Aaction的单独配置文件如下 <?xml version="1.0" encoding=" ...

  3. winform常用方法

    1.对象的初始化器: Class a = new Class() { id = , name = "张三" } 2.窗体间传值    ①构造函数    ②单例函数 //单例模式:确 ...

  4. python list的extend和append方法

    append: Appends object at the end. x = [1, 2, 3] x.append([4, 5]) print (x) gives you: [1, 2, 3, [4, ...

  5. HiHo 1032 最长回文子串 (Manacher算法求解)

    /** * 求解最长回文字串,Manacher算法o(n)求解最长回文子串问题 **/ #include<cstdio> #include<cstdlib> #include& ...

  6. $_validate 自动验证 字段是否重复

    1.在控制器里面实例化 数据表的时候用D方法,不要用M方法 2.在当前Admin下的Model里面建立一个自定义的Model AdminsController.class.php ========== ...

  7. FPGA中竞争冒险问题的研究

    什么是竞争冒险? 1 引言     现场可编程门阵列(FPGA)在结构上由逻辑功能块排列为阵列,并由可编程的内部连线连接这些功能块,来实现一定的逻辑功能. FPGA可以替代其他PLD或者各种中小规模数 ...

  8. RSA公钥,私钥和数字签名通用理解

    一.公钥加密 假设一下,我找了两个数字,一个是1,一个是2.我喜欢2这个数字,就保留起来,不告诉你们(私钥),然后我告诉大家,1是我的公钥. 我有一个文件,不能让别人看,我就用1加密了.别人找到了这个 ...

  9. 一行删除所有docker container的命令

    ns=`docker | awk '//{print $1}'`;for n in $ns;do docker rm -f $n;done docker | awk '{print $1}'|xarg ...

  10. Maven Build错误。

    错误: No goals have been specified for this build. You must specify a valid lifecycle phase or a goal ...