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. android studio里的build.gradle基本属性

    //声明是android 程序 apply plugin: 'com.android.application' android { //编译SDK版本 compileSdkVersion 23 // ...

  2. cocos2d-x 添加纹理自动回收机制

    转自:http://www.cnblogs.com/lancidie/archive/2013/04/13/3019375.html 1.不是一个完整的模块,所以不提供完整代码,只提供思路和核心代码. ...

  3. Android上实现仿IOS弹性ScrollView

    [转]http://www.tuicool.com/articles/ummIJb

  4. 有关字符串作为URL的 UTF8编码和解码的问题。

    当字符串要作为url访问的时候,我们对字符串中的中文非常头疼,这时候需就需要使用 UTF8来编码: //使用 stringByAddingPercentEscapesUsingEncoding 方法来 ...

  5. ios开发——实用技术篇Swift&Swift调用C、C++、Object

    Swift调用C.C++.Object 1.Swift调用C语言a,首先在项目中添加 CFile 文件命名为CHello,同时产生桥梁文件. b,创建之后的项目结构 b,在CHello.h文件中编写接 ...

  6. TIME-WAIT和CLOSE-WAIT

    系统调优,你所不知道的TIME_WAIT和CLOSE_WAIT 2016-03-11 运维帮 来源微信订阅号:大房说 作者:大房 你遇到过TIME_WAIT的问题吗?   我相信很多都遇到过这个问题. ...

  7. 配置SQL Server 2008管理器

    SQl Server 配置管理器(简称为配置管理器)包含了SQL Server 2008服务.SQL Server 2008网络配置和SQL Native Client配置3个工具,供数据库管理人员做 ...

  8. Simulate android behaviors on win32

    To make debugging android games on win32 more convenience, we added some simulate actions to win32 p ...

  9. 解决FLASH最高层的问题,让FLASH置于div之下

    三个步骤:1.设置div 的 z-index:9999 //在最上面显示 这一步就可以保证div在img之上2.<param name="wmode" value=" ...

  10. Android_listView_Listener

    layout.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...