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. ASProgressPopUpView

    ASProgressPopUpView https://github.com/alskipp/ASProgressPopUpView 效果: -使用- 将源码拖入工程当中: // // RootVie ...

  2. Linux入门-3 Linux磁盘及文件系统管理

    1. 磁盘基本概念 1.1 磁盘结构:盘片(单碟vs多碟).磁头(读写数据) 1.2 磁盘在Linux中的表示 1.3 分区概念 2 使用fdisk进行磁盘管理 3 Linux文件系统 mke2fs ...

  3. Python实例---基于页面的后台管理[简单版]

    后台管理菜单 + 母板[css/content/js] 向后台提交数据[2种]:       1.  模态对话框(数据少操作,且Js复杂):        form表单 :优点:简单,前端提交后后台处 ...

  4. curl命令学习之一--基本用法

  5. LNMP-day1-安装并配置

    Nginx安装 #Nginx [root@localhost downloads]# pwd /root/downloads #安装依赖pcre [root@localhost downloads]# ...

  6. MapReduce Design Patterns(chapter 1)(一)

    Chapter 1.Design Patterns and MapReduce MapReduce 是一种运行于成百上千台机器上的处理数据的框架,目前被google,Hadoop等多家公司或社区广泛使 ...

  7. August 12th 2017 Week 32nd Saturday

    That which does not kill us makes us stronger. 但凡不能杀死你的,最终都会使你更强大. Seemingly I have heard this from ...

  8. Intellij IDEA常用快捷键和一些配置——Mac版

    常用的快捷键 代码补全Ctrl + space 删除行Command + D 注释Command + / 导入包Command + shift + O 格式化代码Command + shift + F ...

  9. 【bbs】login.php

    require的路径 整体结构的复用 渐变效果的加入 按钮:实现背景透明,文字不透明:在background-color中使用rgba,标准浏览器中,背景透明,文字不透明background-colo ...

  10. c#中的结构

    1.在c#中,结构是值类型的数据结构,它可以使用一个单一的变量存储各种数据类型的相关数据,使用Struct关键字进行声明. 2.C#中结构的特点: (1)结构中可以有字段,属性,方法,运算表达式,事件 ...