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
------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<string.h>
#define SIZE 2
long long int ** multiple(long long int a[][SIZE], long long int b[][SIZE]);
int main()
{
int A, B;
long long int n;
long long int factor[SIZE][SIZE];
long long int ans[SIZE][SIZE];
while (scanf("%d%d%lld", &A, &B, &n) && (A||B||n))
{
memset(factor, 0, 2 * 2 * sizeof(int));
memset(ans, 0, 2 * 2 * sizeof(int));
factor[0][0] = A;
factor[0][1] = B;
factor[1][0] = 1;
factor[1][1] = 0;
//矩阵快速幂
for (int i = 0; i < SIZE; i++)
ans[i][i] = 1;
n = n - 2;
while (n > 0)
{
if (n & 1)
memcpy(ans, multiple(ans, factor), 2 * 2 * sizeof(long long int));
memcpy(factor, multiple(factor, factor), 2 * 2 * sizeof(long long int));
n >>= 1;
}
printf("%lld\n", (ans[0][0] + ans[0][1]) % 7);
}
return 0;
} long long int ** multiple(long long int a[][SIZE], long long int b[][SIZE])
{
int i, j, k;
long long int c[SIZE][SIZE];
memset(c, 0, 2 * 2 * sizeof(long long int));
for(i = 0; i < SIZE; i++)
for(j = 0; j < SIZE; j++)
for (k = 0; k < SIZE; k++)
{
c[i][j] += a[i][k] * b[k][j];
c[i][j] = c[i][j] % 7;
}
return c;
}

参考文章:

https://blog.csdn.net/codeswarrior/article/details/81258928
https://www.cnblogs.com/cmmdc/p/6936196.html

ACM1005:Number Sequence的更多相关文章

  1. 1005:Number Sequence(hdu,数学规律题)

    Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...

  2. 递推:Number Sequence(mod找规律)

    解题心得: 1.对于数据很大,很可怕,不可能用常规手段算出最后的值在进行mod的时候,可以思考找规律. 2.找规律时不必用手算(我傻,用手算了好久).直接先找前100项进行mod打一个表出来,直接看就 ...

  3. HDU 1711:Number Sequence(KMP)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. POJ 1019:Number Sequence 二分查找

    Number Sequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36013   Accepted: 10409 ...

  5. HDU 1711:Number Sequence(KMP模板,求位置)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. hdu5014:number sequence对称思想

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5014 题目大意:给定数组 a[]={0,1,2......n} 求一个数组b[] 元素也为0.... ...

  7. HDU1711:Number Sequence

    Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...

  8. hdu1005 Number Sequence(寻找循环节)

    主题链接: pid=1005">huangjing 题意: 就是给了一个公式,然后求出第n项是多少... 思路: 题目中n的范围实在是太大,所以肯定直接递推肯定会超时,所以想到的是暴力 ...

  9. [AX]AX2012 Number sequence framework :(三)再谈Number sequence

    AX2012的number sequence framework中引入了两个Scope和segment两个概念,它们的具体作用从下面序列的例子说起. 法国/中国的法律要求财务凭证的Journal nu ...

随机推荐

  1. 多个 Word 文档合并为一个

    如果您工作中经常要跟 Word 文档打交道,时不时的您可能需要将多个 Word 文档合并为一个.信息量少的时候,我们可以直接使用复制粘贴.除此之外,还有没有其它办法呢? 借助word2010/2007 ...

  2. 内置模块之sys

    一.模块sys sys模块主要对解释器相关的操作 1.常用方法和属性 sys.argv    命令行参数List,第一个元素是程序本身路径 sys.exit(n)  退出程序,正常退出时exit(0) ...

  3. python安装 numpy&安装matplotlib& scipy

    numpy安装 下载地址:https://pypi.python.org/pypi/numpy(各取所需) copy安装目录.eg:鄙人的D:\python3.6.1\Scripts pip inst ...

  4. TCP/UDP调试器 SocketToolV4.1

    TCP/UDP Socket调试工具提供了TCP Server,TCP Client,UDP Server,UDP Client,UDP Group 五种Socket调试方案.SocketTool V ...

  5. 深入了解Node模块原理

    深入了解Node模块原理 当我们编写JavaScript代码时,我们可以申明全局变量: var s = 'global'; 在浏览器中,大量使用全局变量可不好.如果你在a.js中使用了全局变量s,那么 ...

  6. storm集群安装

    1.下载storm安装文件并解压 [root@hadoop01 soft]# wget http://mirrors.hust.edu.cn/apache/storm/apache-storm-1.1 ...

  7. 解析Java对象的equals()和hashCode()的使用

    解析Java对象的equals()和hashCode()的使用 前言 在Java语言中,equals()和hashCode()两个函数的使用是紧密配合的,你要是自己设计其中一个,就要设计另外一个.在多 ...

  8. [TJOI2013]攻击装置

    题目 癌我竟然会做 发现我们要求的是一个最大独立集问题 发现一个格子和能攻击到的格子的奇偶性和它都不同,于是我们就可以按照\(i+j\)的奇偶性把整张图分成两个部分 两个部分之间没有连边 于是二分图最 ...

  9. pyenv 配置python虚拟环境

    安装pyenv环境 yum -y install git yum install gcc make patch gdbm-devel openssl-devel sqlite-devel readli ...

  10. Ubuntu安装docker笔记

    前言   根据参考文档简单记录Ubuntu系统安装docker的步骤 系统版本 panzi@ubuntu:~$ cat /etc/issue Ubuntu 16.04.5 LTS \n \l 移除旧版 ...