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<iostream>
#include<cmath>
using namespace std; int myfunction( int A, int B, int n )
{
int counts = n-+;
int first=,second=,result;
for( int i=; i<counts; i++ )
{
result = (B*first+A*second)%; //大部分的时间都浪费在了这里
first = second;
second = result;
}
return result;
} int main(void)
{
int A,B,n; while()
{
cin>>A>>B>>n;
if( A== && B== && n== )
break;
if( n== || n== )
cout<<<<endl;
else
{
cout<<myfunction(A,B,n)<<endl;
}
}
return ;
}

其实这个数列是有规律的,只要算出它的一圈数列后,再算出n中有多少圈,之后就知道最终得出圈中哪个数。

例如输入1 2 10000后,数列的前几个数为1  1  3  5  4  0  1  1

它开始转圈了,因此我将程序改成如下所示:

 #include<iostream>
using namespace std;
int main()
{
int a,b,n,i,m,t[];
t[]=;
t[]=;
while(cin>>a>>b>>n)
{
if((a==)&&(b==)&&(n==))
break;
a=a%;
b=b%;
for(i=; i<; i++)
{
t[i]=(a*t[i-]+b*t[i-])%;
if((t[i]==)&&(t[i-]==))
{
break;
}
}
m=n%(i-);
if(m==)
cout<<t[i-]<<endl;
else
cout<<t[m]<<endl;
}
return ;
}

哈哈,AC了

这道题给我的提示是,遇到数列题目时要想想里面是不是存在“转圈”现象,发觉里面的规律,之后编程就容易了。

HDOJ1005 数列的更多相关文章

  1. C#求斐波那契数列第30项的值(递归和非递归)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. BZOJ1500[NOI2005]维修数列

    Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目.第2行包含N个数字,描述初始时的数列.以下M行,每行一 ...

  3. PAT 1049. 数列的片段和(20)

    给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段.例如,给定数列{0.1, 0.2, 0.3, 0.4},我们有(0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1 ...

  4. 斐波拉契数列加强版——时间复杂度O(1),空间复杂度O(1)

    对于斐波拉契经典问题,我们都非常熟悉,通过递推公式F(n) = F(n - ) + F(n - ),我们可以在线性时间内求出第n项F(n),现在考虑斐波拉契的加强版,我们要求的项数n的范围为int范围 ...

  5. fibonacci数列(五种)

    自己没动脑子,大部分内容转自:http://www.jb51.net/article/37286.htm 斐波拉契数列,看起来好像谁都会写,不过它写的方式却有好多种,不管用不用的上,先留下来再说. 1 ...

  6. js中的斐波那契数列法

    //斐波那契数列:1,2,3,5,8,13…… //从第3个起的第n个等于前两个之和 //解法1: var n1 = 1,n2 = 2; for(var i=3;i<101;i++){ var ...

  7. 洛谷 P1182 数列分段Section II Label:贪心

    题目描述 对于给定的一个长度为N的正整数数列A[i],现要将其分成M(M≤N)段,并要求每段连续,且每段和的最大值最小. 关于最大值最小: 例如一数列4 2 4 5 1要分成3段 将其如下分段: [4 ...

  8. 剑指Offer面试题:8.斐波那契数列

    一.题目:斐波那契数列 题目:写一个函数,输入n,求斐波那契(Fibonacci)数列的第n项.斐波那契数列的定义如下: 二.效率很低的解法 很多C/C++/C#/Java语言教科书在讲述递归函数的时 ...

  9. 代码的坏味道(4)——过长参数列(Long Parameter List)

    坏味道--过长参数列(Long Parameter List) 特征 一个函数有超过3.4个入参. 问题原因 过长参数列可能是将多个算法并到一个函数中时发生的.函数中的入参可以用来控制最终选用哪个算法 ...

随机推荐

  1. ABAP ALV 颜色设置(行,列,单元格)

    BCALV_EDIT_03 http://blog.sina.com.cn/s/blog_a87b19300102who3.html 关于ALV表格颜色,这种需求在项目中会经常用到. 列颜色 列的颜色 ...

  2. Icon specified in the Info.plist not found under the top level app wrapper: Icon.png

    For some reason the (possibly when adding multiple icons and changing the file?) the item gets moved ...

  3. node.js在windows下的学习笔记(10)---URL模块

    1.parse函数的作用是解析url,返回一个json格式的数组 url.parse('http://www.zjut.edu.cn'); { protocol: 'http:', slashes: ...

  4. [MODx] 10. Using Babel for Muti-languages support

    1. Go to 'Extras' -> download and install 'Babel'. 2. Set up '.htaccess' file, currently, we set ...

  5. Ajax防止重复提交

    转:http://www.cnblogs.com/jinguangguo/archive/2013/05/20/3086925.html 谈谈防止重复点击提交   首先说说防止重复点击提交是什么意思. ...

  6. 文件I/O(不带缓冲)之write函数

    调用write函数向打开的文件写数据. #include <unistd.h> ssize_t write( int filedes, const void *buf, size_t nb ...

  7. 可视化swing界面编辑--转载

    原文地址:http://279234058.iteye.com/blog/2200122 今天发现了一个WindowBuilder插件,功能好强大,啊哈哈,从此告别手动编辑swing界面代码,直接像V ...

  8. eclipse创建多模块maven工程小结

    创建maven工程步骤 1 新建一个maven工程,如下图所示: 2 选择项目名称(或项目目录),如下图所示: 3 填写maven工程相关信息,注意父maven工程的packing方式是pom,如下图 ...

  9. Hibernate中的"Repeated column in mapping for entity"异常

    转:http://lijiejava.iteye.com/blog/786535 一对多双向关联(类Item与类Bid): Item类: public class Item { private int ...

  10. h5拖放-上传图片预览功能

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...