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. Java实例---flappy-bird实例解析

    第一天: 实现背景图片和小鸟的动态飞行效果 package com.ftl.flappybird.day2; import java.awt.Color;//颜色 Color.class import ...

  2. 简单转java-web项目

  3. 转贴:如何学好C++语言.docx

    不知道哪里转的.呵呵 抱歉 C++是最难的语言.这个世界上最难的编程语言可能非C++莫属了.你千万不要以为几天就可以学好C++,C++的学习曲线是相当BT的,你可以看看这篇文章.C++是一门很自由的语 ...

  4. Python中根据提供的日期,返回是一年中的第几天

    # coding = utf-8 import datetime dd = raw_input("Please enter date , format is YYYYMMDD:") ...

  5. Innodb_io_capacity 对于IO稳定性的一些研究

    背景:最近在做一台线上服务器IO负载情况的时候发现了以下现象: 24小时的IO_UTIL 的曲线看似风平浪静,毛刺较少 但当图片放大到半小时级别的时候发现IO_UTIL即磁盘使用率出现了规律性的波动, ...

  6. BZOJ 1036 树的统计Count 树链剖分模板题

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1036 题目大意: 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将 ...

  7. 关于Struts2通配符无效的说明

    在struts2.3之前的版本,正常的配置就可以了,但在struts2.3版本之后,使用通配符调用方法时,内部会验证是否允许访问该方法. 1.struts2.5 为了增加安全性,在 struts.xm ...

  8. [19/04/25-星期四] GOF23_结构型模式(适配器模式、代理模式)

    一.引言 结构模式:核心作用就是从程序的结构上实现松耦合,从而扩大整体的类结构,用来解决更大的问题. 二.适配器模式(adapter) 生活中假设笔记本是标准的USB接口但是外置键盘是圆形接口,这时候 ...

  9. stylus的用法

    参考链接:预处器的对比——Sass.LESS和Stylus   http://www.w3cplus.com/css/sass-vs-less-vs-stylus-a-preprocessor-sho ...

  10. 6.7 块管理器BlockManager

    /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreem ...