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
 #include<iostream>
using namespace std;
int main()
{
int f[];
int a, b, n;
while (cin >> a >> b >> n, a != && b != && n != )
{
int i;
f[] = , f[] = ;
for (i = ; i < ; i++)
{
f[i] = (a*f[i - ] + b * f[i - ]) % ;
if (f[i] == && f[i - ] == ) break;
}
i -= ;
if (i > n)
{
cout << f[n] << endl;
continue;
}
n = n % i;
if (n == ) n = i;
cout << f[n] << endl;
}
return ;
}
大佬代码:http://www.cnblogs.com/kuangbin/archive/2011/07/26/2117381.html

#include<stdio.h>
int main()
{
//freopen("test.in","r",stdin);
//freopen("test.out","w",stdout);
int A,B,i;
long n;
int f[];
f[]=f[]=;
while(scanf("%d %d %ld",&A,&B,&n))
{
if(A==&&B==&&n==) break;
int cnt=;
for(i=;i<=;i++)//打表找到周期
{
f[i]=(A*f[i-]+B*f[i-])%;
if(f[i]==&&f[i-]==)break;
if(f[i]==&&f[i-]==){cnt=;break;}//这里有个小陷阱,如果A=7,B=7则后面都为0了
}
if(cnt){printf("0\n");continue;}
if(i>n){printf("%d\n",f[n]);continue;}
i-=;//i为周期
n%=i;
if(n==)n=i;
printf("%d\n",f[n]);
}
return ; }

这题完全参考大佬的代码改良;

凉凉;;;;;;;;;

数据量小,一些不合适的没在测试数据不在里面

最新文章

  1. JAVA SE 803 考试前突击
  2. PHP 设计模式 笔记与总结(5)PHP 魔术方法的使用
  3. 【MySql】赶集网mysql开发36条军规
  4. Entity Framework 学习初级篇7--基本操作:增加、更新、删除、事务
  5. git基本用法
  6. how tomcat works 5 servlet容器 下
  7. 微信小程序自动化测试实践
  8. 常用H5
  9. 为什么wait()和notify()属于Object类
  10. Django之模板2
  11. GNU构建系统和AutoTools
  12. 大杂烩 -- Java内存布局【图】以及java各种存储区【详解】
  13. Gym - 101806R :Recipe(分治+斜率优化)
  14. 20155306 白皎 0day漏洞——漏洞利用原理之GS
  15. Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match
  16. MYSQL 什么时候用单列索引?什么使用用联合索引?(收集)
  17. Minecraft Forge编程入门三 “初始化项目结构和逻辑”
  18. C++中char*与wchar_t*之间的转换
  19. 简单的js表单验证框架
  20. 使用superobject 新建Json数据(数组)

热门文章

  1. Java基础_0307:String类的基本概念
  2. VS 中NuGet 尝试还原程序包时出错&quot;*&quot;已拥有为&quot;**&quot;定义的依赖项
  3. Python 基础算法
  4. 列出下面几项的URL并解释每部分代表的含义
  5. 前端-----html(1)
  6. v2v-VMware/VSphere中虚机离线迁移至openstack平台
  7. Python内建函数-callable
  8. Python-eval()函数
  9. Tensorflow的Queue读取数据机制
  10. python 字典不区分大小写工具类