Number Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 96275 Accepted Submission(s): 23088
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).
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
题意:
给出 A (1 ~ 1000),B (1 ~ 1000),N (1 ~ 10 ^ 8),F [ 1 ] = F [ 2 ] = 1,后 F [ N ] =(A * F [ N - 1 ] + B * F [ N - 2 ] )% 7。输出 F [ N ]。
思路:
数学。结果 % 7,说明 f 最多也只可能是 0 ~ 6 中的某一个数,模拟几组数据即可发现会有循环节。算出循环节后 F [ N % MOD ] 即是答案。循环节最多也只可能是 7 X 7 = 49 这么多,所以数组开 55 来保存循环节也就够了。
AC:
#include <cstdio> #include <algorithm> #include <cstring> using namespace std; int num[55]; int main () { int a, b, n; while (~scanf("%d%d%d", &a, &b, &n) && (a + b + n)) { int mod; num[1] = num[2] = 1; for (mod = 3; mod < 55; ++mod) { num[mod] = (a * num[mod - 1] + b * num[mod - 2]) % 7; if (num[mod] == 1 && num[mod - 1] == 1) { mod -= 2; break; } } if (!(n % mod)) printf("%d\n",num[mod]); else printf("%d\n", num[n % mod]); } return 0; }
相关推荐
Computer Analysis of Number Sequences - H. Ibstedt 一本关于数值序列分析的书,需要一定的数学基础。
let sequence: Vec < Number> = serde_json :: from_str ( "[true, 2, 3.5, -4, [1.0, -0.5]]" ). unwrap (); let actual = sequence. into_iter (). product (); assert_eq! (actual, Number :: from (num :: ...
18.oracle 序列(sequence) 39 19.oracle 索引 40 20.oracle管理权限和角色 42 21.PL/SQL 47 (1)存储过程简单版本 47 (2)存储过程升级版本 49 (3)函数 50 (4)包 50 (5)触发器 51 PL/SQL语法数据类型 57 (6)PL/SQL进阶...
5、--任何含有空值的数学表达式,最后的计算结果都是空值。 6、select ename||sal from emp; --(将sal的查询结果转化为字符串,与ename连接到一起,相当于Java中的字符串连接) 7、select ename||'afasjkj' from...
1410 Number Sequence 无聊题 1401 Hilbert Curve Intersections 无聊题 1331 Perfect Cubes 无聊题 1322 Random Number 无聊题 1535 Lucky Ticket 无聊题 1539 Lot 无聊题 1363 Chocolate 经典题…… ...
1410 Number Sequence 无聊题 1401 Hilbert Curve Intersections 无聊题 1331 Perfect Cubes 无聊题 1322 Random Number 无聊题 1535 Lucky Ticket 无聊题 1539 Lot 无聊题 1363 Chocolate 经典题…… ...
CHAPTER 1 About Python ............................................................................................1 What Is Python? ......................................................................
<3>.pctfree(index)=(maximum number of rows-initial number of rows)*100/maximum number of rows <4>.creating reverse key indexes sql> create unique index xay_id on xay(a) reverse pctfree 30 ...
oracle的框架主要由物理结构、逻辑结构、内存分配、后台进程、oracle例程、系统改变号 (System Change Number)组成 物理结构 物理结构包含三种数据文件: 1) 控制文件 2) 数据文件 3) 在线重做日志文件 ...
1、索引 ·什么是索引 ·索引的基本原理 ·索引的基本写法 ·索引的分类 ... 比如你判断一个班上的同学数学成绩怎么样,你就可能用游标,先把全部的成绩查询到游标中,之后再循环一条条进行判断处理。