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. 在windows平臺下使用cygwin獲取root用戶權限

    最近在使用cygwin時發現一個問題,當我要使用root用戶權限時,竟然創建不了root賬戶.最後在網上找了下後,暫時衹找到了通過更改當前用戶權限獲得root權限的方法,具體如下: 实际环境:win1 ...

  2. kickstart+ftp+tftp+dhcp+PXE

    ##########yum less install.log #看安装log yum install system-config-kickstart* -y yum install tftp* -y ...

  3. C语言字符串处理函数

    函数名: strcpy  功  能: 拷贝一个字符串到另一个  用  法: char *stpcpy(char *destin, char *source);  程序例:  #include < ...

  4. cps变换

    网上看了很多内容,很少有给出一个准确的概念,它的英文全称是continuous passing style, 直译为连续传递样式,那么cps transform就是将一些原本不是continuous ...

  5. 利用web工具splinter模拟登陆做自动签到

    首先,我需要的工具和组件有: Chrome浏览器 浏览器驱动ChromeDriver Python 3.5 Web应用测试工具Splinter 代码部分: from splinter import B ...

  6. 20160113 js中选择多个check一块删除

    js中<script type="text/javascript"> $(document).ready(function (e) { $("#Button2 ...

  7. Python内存数据库/引擎

    1 初探 在平时的开发工作中,我们可能会有这样的需求:我们希望有一个内存数据库或者数据引擎,用比较Pythonic的方式进行数据库的操作(比如说插入和查询). 举个具体的例子,分别向数据库db中插入两 ...

  8. 要用于尝试,广东移动间接实现“流量不清零”[bubuko.com]

    拥有1亿用户的广东移动在推出流量共享后,推出4G套餐外流量的“自动升档”服务,每月根据客户消费情况动态自动匹配当月最恰当的一档流量资费.未来,还将推出“流量转赠”服务,用不完的流量可转赠给其他用户. ...

  9. xcode8让真机测试支持ios8.0以下版本

    xcode8支持ios8以下真机测试方法: 1.应用程序-xcode 显示包内容-Contents-Developer-Platforms-iPhoneOS.platform-DeviceSuppor ...

  10. 承接Unity3D外包公司 — 技术分享

    Cardboard SDK for Unity的使用 上一篇文章作为系列的开篇,主要是讲了一些虚拟现实的技术和原理,本篇就会带领大家去看一看谷歌的Cardboard SDK for Unity,虽然目 ...