首页 技术 正文
技术 2022年11月21日
0 收藏 624 点赞 3,410 浏览 868 个字

Number Sequence

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

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 ZJCPC2004

当时别人问我这题,我最开始一看感觉是矩阵快速幂,回来后想想%7,因为7太小了,可能有规律,周期最大为48,突然想起来原来以前做过这个题

#include <stdio.h>
int main()
{
int i,j,A,B,f1,f2,f3,n;
while(scanf("%d%d%d",&A,&B,&n)!=EOF&&(A||B||n))
{
f1=1;
f2=1;
A=A%7;
B=B%7;
n=(n-3)%48+2;
for(i=2;i<=n;i++)
{
f3=(A*f2+B*f1)%7;
f1=f2;
f2=f3;
}
printf("%d\n",f2);
}
return 0;
}

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,028
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,365
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,780
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,858