HDU 1005 Number Sequence (模拟)
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
分析:
首先考虑一下这道题的n的范围,n的范围是比较大的,我们虽然用数组可以存贮下来但是却会超内存,那么这个问题应该怎么解决的?
最后要求求得的答案对于7取余,那么所有的f(i)肯定都是0~6之内的数字,这样的话我们应该可以找到f(i)和f(i-1)使得两个值均为1,这样的话也就相当于又回到了最起始的状态,也就可以认为函数f()时一个以i-2位周期的周期函数。
知道周期函数后,将n的值对应成周期函数的下标。
代码:
#include<string.h>
#include<stdio.h>
#include<iostream>
using namespace std;
int c[200];
int a,b,n;
int main()
{
c[1]=1;
c[2]=1;
while(~scanf("%d%d%d",&a,&b,&n)&&a&&b&&n)
{
if(n>=3)
{
int i;
for(i=3; i<200; i++)
{
c[i]=(a*c[i-1]+b*c[i-2])%7;
if(c[i]==1&&c[i-1]==1)//又回归到最起始的状态,相当于在c这个数组里面i-2个数为一个周期
break;
}
i-=2;//周期长度
n=n%i;//需要求的是这个周期里面的第几个数
if(n==0)
printf("%d\n",c[i]);
else
printf("%d\n",c[n]);
}
else printf("1\n");
}
return 0;
}
HDU 1005 Number Sequence (模拟)的更多相关文章
- HDU 1005 Number Sequence(数列)
HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- HDU 1005 Number Sequence(数论)
HDU 1005 Number Sequence(数论) Problem Description: A number sequence is defined as follows:f(1) = 1, ...
- HDU - 1005 Number Sequence 矩阵快速幂
HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...
- HDU 1005 Number Sequence【多解,暴力打表,鸽巢原理】
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1005 Number Sequence
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 1005:Number Sequence(水题)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1005 Number Sequence【斐波那契数列/循环节找规律/矩阵快速幂/求(A * f(n - 1) + B * f(n - 2)) mod 7】
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1005 Number Sequence(矩阵)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- HDU - 1005 Number Sequence (矩阵快速幂)
A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mo ...
随机推荐
- PAT甲题题解-1021. Deepest Root (25)-dfs+并查集
dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...
- Final发布点评
1. 约跑App——nice!:为改进演示效果,本组使用摄像头实时采集投影的方式展示其作品,是一种演示的创新.本组重点放在了修改上次来着其他组发现的bug,不过新功能上好像没有加入多少,可能是保证软 ...
- PAT 甲级 1021 Deepest Root
https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 A graph which is conne ...
- [转帖]PG的简单备份恢复 找时间进行测试
转帖PG的简单使用 https://blog.csdn.net/lk_db/article/details/77971634 一: 纯文件格式的脚本: 示例:1. 只导出postgres数据库的数据, ...
- 常用的cpl 命令 运行直接打开控制台的简单方法
转载百度百科 工作中处理 windows机器 有时候 打开 网路修改ip地址特别繁琐,所以找了下 快速打开一些简单的控制台 能提高工作效率. (Control Panel extension) ...
- Jquery Cookie操作
// 写入 $.cookie('the_cookie','the_value');// 读取 $.cookie('the_cookie');// 删除 $.cookie('the_cookie',nu ...
- Enum 枚举值 (一) 获取描述信息
封装了方法: public static class EnumOperate { public class BaseDescriptionAttribute : DescriptionAttribut ...
- 【题解】Luogu P2047 社交网络总结 (Floyd算法,最短路计数)
题目描述 在社交网络(social network)的研究中,我们常常使用图论概念去解释一些社会现象.不妨看这样的一个问题.在一个社交圈子里有n个人,人与人之间有不同程度的关系.我 们将这个关系网络对 ...
- bzoj1038
这是一道非常有意思的题目 Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 我们可以用一 ...
- 【转】Example of using the --info linker option
5.3 Example of using the --info linker option This is an example of the output generated by the --in ...