hdu 1005:Number Sequence(水题)

时间:2021-07-10 08:56:18

Number Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 89984    Accepted Submission(s): 21437

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
 
Author
CHEN, Shunbao
 
Source
 
Recommend
JGShining   |   We have carefully selected several similar problems for you:  1004 1019 1009 1012 1071 
 
  这道题足足提交了18遍,虽然是水题,但做的时候也要小心。
  题目描述里提供有递归公式,但是万万不能用递归做,下面n的规模是10^8,用递归铁定超时。
  需要先找到变化周期,周期是从f1=1,f2=1开始,到再遇到两个连续的1的时候停止。 记录下这个周期,剩下的就好做了。
  不知道为什么,循环找周期的时候,for(i=3;i<10000;i++),i<10000改成i<=10000就提交WA,难道它还会跑到i=10000?? 
/*
这题不能直接按公式用递归来求,因为n最大可以达到100,000,000,会栈溢出
所以要找规律
前两个等于1,所以后面如果有两个连着的1出现,那就是出现周期了
*/
#include <iostream> using namespace std;
int t[];
int main()
{
int A,B,n;
t[]=t[]=;
while(cin>>A>>B>>n,A||B||n){
int i;
for(i=;i<;i++){
t[i]=(A*t[i-]+B*t[i-])%;
if(t[i]== && t[i-]==){
break;
}
}
n=n%(i-);
t[]=t[i-];
cout<<t[n]<<endl;
}
return ;
}

Freecode : www.cnblogs.com/yym2013

hdu 1005:Number Sequence(水题)的更多相关文章

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

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

  2. HDU 1005 Number Sequence(数列)

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

  3. HDU 1005 Number Sequence&lpar;数论&rpar;

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

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

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

  5. 51nod 1126 求递推序列的第N项 &amp&semi;&amp&semi; hdu - 1005 Number Sequence &lpar;求周期&rpar;

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1126 http://acm.hdu.edu.cn/showproblem ...

  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 (模拟)

    题目链接 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f( ...

  8. HDU 1005 Number Sequence【斐波那契数列&sol;循环节找规律&sol;矩阵快速幂&sol;求&lpar;A &ast; f&lpar;n - 1&rpar; &plus; B &ast; f&lpar;n - 2&rpar;&rpar; mod 7】

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

  9. HDU 1005 Number Sequence(矩阵)

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

随机推荐

  1. 20款 JavaScript 开发框架推荐给前端开发者

    下面,我们给大家提供了一个用于 HTML5 开发的各种用途的 JavaScript 库列表.这些框架能够给前端开发人员提供更好的功能实现的解决方案.如果你有收藏优秀的框架,也可以在后面的评论中分享给我 ...

  2. Java 报表之JFreeChart&lpar;第一讲&rpar;

    1.利用 JFreeChart 创建垂直柱状报表 package com.wcy.chart.bar; import javax.servlet.http.HttpSession; import or ...

  3. mariadb DML语句及用户授权

    DML(Data Manipulation Language):INSERT, DELETE, UPDATE, SELECT INSERT  [INTO]  tbl_name  [(col1,...) ...

  4. operation is executing and cannot be enqueued

    http://d2100.com/questions/29022 作为依赖关系的另一个 NSOperation 添加时不调用 NSOperation dealloc 使用文书我看到很多我自定义的 NS ...

  5. 《Web Scraping With Python》Chapter 1的学习笔记

    urllib urllib是python library自带的库,可以直接用. urlopen from urllib.request import urlopen html = urlopen(&q ...

  6. C&plus;&plus;,std&colon;&colon;shared&lowbar;future的使用

    今天给大家分享一个类似多线程任务的方法,具体如下: std::shared_future<int> tmp = std::async(p1,p2,p3); int tmpInt = tmp ...

  7. 【转载】汇编调试程序Debug使用

    https://blog.csdn.net/Notzuonotdied/article/details/70888205

  8. 十九、springcloud&lpar;五&rpar;配置中心本地示例和refresh

    1.创建spring-cloud-houge-config子项目,测试需要的项目入下 2.添加依赖 <dependency> <groupId>org.springframew ...

  9. JavaScript -- History

    -----042-History.html----- <!DOCTYPE html> <html> <head> <meta http-equiv=&quot ...

  10. ASP&period;net显示当前系统在线人数

    void Application_Start(object sender, EventArgs e) { // 在应用程序启动时运行的代码 Application.Lock(); if (Applic ...