Number Sequence  HDU-1005

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

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
 
 
发这道题主要是祭奠一下我第一次遇到的超内存MLE。。。。。第一想法这么简单,暴力递归。。然后MLE狠狠打脸。。

后来再认真读题,发现还是有规律的。

如果f(n - 1)和f(n)都为1的话,就是一个循环。

#include <iostream>
#include <cstdio>
using namespace std;
int f[];
int main()
{
int a, b;
long long n;
while (scanf("%d %d %lld", &a, &b, &n) == )
{
if (a == && b == && n == )
break;
f[] = f[] = ;
int i;
for (i = ; i < ; i++)
{
f[i] = (a * f[i - ] + b * f[i - ]) % ;
if (f[i] == && f[i - ] == ) //找到循环因子 i
{
break;
}
}
n = n % (i - );
if (n == ) //刚好经过一个循环
printf("%d\n", f[i - ]);
else
printf("%d\n", f[n]);
}
return ;
}

这题真的是好烦。。。。。。。

 

HDU1005的更多相关文章

  1. hdu1005 矩阵

    //Accepted hdu1005 0MS 248K #include <cstdio> #include <cstring> #include <iostream&g ...

  2. 矩阵快速幂(入门) 学习笔记hdu1005, hdu1575, hdu1757

    矩阵快速幂是基于普通的快速幂的一种扩展,如果不知道的快速幂的请参见http://www.cnblogs.com/Howe-Young/p/4097277.html.二进制这个东西太神奇了,好多优秀的算 ...

  3. HDU1005 找规律 or 循环点 or 矩阵快速幂

    http://acm.hdu.edu.cn/showproblem.php?pid=1005 1.一开始就注意到了n的数据范围 <=100 000 000,但是还是用普通的循环做的,自然TLE了 ...

  4. HDU1005(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 #include<cstdio> using namespace std; int ...

  5. HDU1005&&NEFU67 没有循环节

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

  6. hdu1005 Number Sequence(数论)

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

  7. HDU1005 数列找规律

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

  8. HDU1005(周期问题)

    Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * ...

  9. hdu1005 Number Sequence(寻找循环节)

    主题链接: pid=1005">huangjing 题意: 就是给了一个公式,然后求出第n项是多少... 思路: 题目中n的范围实在是太大,所以肯定直接递推肯定会超时,所以想到的是暴力 ...

随机推荐

  1. iOS_仿QQ表情键盘

    当UITextFiled和UITextView这种文本输入类控件成为第一响应者时,弹出的键盘由他们的一个UIView类的inputView属性来控制,当inputView为nil时会弹出系统的键盘,想 ...

  2. 10天学会phpWeChat——第五天:实现新闻投稿功能

    在前几讲里,我们逐渐实现了自己小模块的新闻列表展示.新闻详情展示功能,现在您已经初步有能力开发一个phpWeChat小模块了,本文将在已开发的hello world模块基础上,增加一个新的功能--新闻 ...

  3. DBA-mysql-用户控制

    创建: CREATE USER 'jeffrey'@'localhost'  IDENTIFIED BY 'new_password' PASSWORD EXPIRE; 授权: Grant all o ...

  4. Codis集群

    一.简介 Codis是一个分布式的Redis解决方案,对于上层的应用来说,连接Codis Proxy和连接原生的Redis Server没有明显的区别(不支持的命令列表),上层应用可以像使用单机的Re ...

  5. java编译正常javac出错不是内部或外部命令

    javac不是内部或外部命令 安装jdk版本jdk-8u111-windows-x64(jdk1.8.0_111) 配置环境: JAVA_HOME D:\xiazai\Java\jdk1.8.0_11 ...

  6. mybatisGenerator 代码自动生成报错 Result Maps collection already contains value for BaseResultMap--转

    转自:http://blog.csdn.net/tan3739/article/details/7555665 Exception in thread "main" Java.la ...

  7. 一个简易的反射类库NMSReflector

    转自:http://blog.csdn.net/lanx_fly/article/details/53914338 背景简介 以前看过一些代码,是简单的读取SqlReader然后赋值给Model,我不 ...

  8. iOS10 权限配置

    升到iOS10之后,需要设置权限的有: 麦克风权限:Privacy - Microphone Usage Description 是否允许此App使用你的麦克风? 相机权限: Privacy - Ca ...

  9. SPOJ bsubstr

    题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...

  10. Java的动态绑定机制

    Java的动态绑定又称为运行时绑定.意思就是说,程序会在运行的时候自动选择调用哪儿个方法. 一.动态绑定的过程: 例子: public class Son extends Father Son son ...