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

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
 
Recommend
 
JGShining   |   We have carefully selected several
similar problems for you:  1008 1004 1021 1019 1002 
 
 
这道题需要推一个类似于斐波那契矩阵的矩阵。
比较好推

 #include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int MAXN=;
inline void read(int &n){char c='+';bool flag=;n=;
while(c<''||c>'') c=='-'?flag=,c=getchar():c=getchar();
while(c>=''&&c<='') n=n*+c-,c=getchar();flag==?n=-n:n=n;}
struct matrix
{
int m[][];matrix(){memset(m,,sizeof(m));}
};
matrix ma;
int limit=;
const int mod=;
matrix mul(matrix a,matrix b)
{
matrix c;
for(int k=;k<limit;k++)
for(int i=;i<limit;i++)
for(int j=;j<limit;j++)
c.m[i][j]=(c.m[i][j]+(a.m[i][k]*b.m[k][j]))%mod;
return c;
}
matrix fast_martix_pow(matrix ma,int p)
{
matrix bg;
bg.m[][]=;bg.m[][]=;
bg.m[][]=;bg.m[][]=;
/*for(int i=0;i<limit;i++)
{
for(int j=0;j<limit;j++)
cout<<bg.m[i][j]<<" ";
cout<<endl;
}*/ while(p)
{
if(p&) bg=mul(bg,ma);
ma=mul(ma,ma);
p>>=;
}
return bg;
}
int main()
{
int a,b,n;
while(scanf("%d%d%d",&a,&b,&n)&&(a!=&&b!=&&n!=))
{
ma.m[][]=a;ma.m[][]=b;
ma.m[][]=;ma.m[][]=;
if(n<)
{
printf("1\n");
continue;
}
matrix ans=fast_martix_pow(ma,n-);
printf("%d\n",(ans.m[][]+ans.m[][])%mod);
}
return ;
}

HDU 1005 Number Sequence(矩阵)的更多相关文章

  1. HDU - 1005 Number Sequence 矩阵快速幂

    HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...

  2. HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)

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

  3. HDU - 1005 -Number Sequence(矩阵快速幂系数变式)

    A number sequence is defined as follows:  f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) m ...

  4. HDU 1005 Number Sequence(数列)

    HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

  5. HDU 1005 Number Sequence(数论)

    HDU 1005 Number Sequence(数论) Problem Description: A number sequence is defined as follows:f(1) = 1, ...

  6. HDU 1005 Number Sequence【斐波那契数列/循环节找规律/矩阵快速幂/求(A * f(n - 1) + B * f(n - 2)) mod 7】

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. HDU - 1005 Number Sequence (矩阵快速幂)

    A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mo ...

  8. HDU 1005 Number Sequence【多解,暴力打表,鸽巢原理】

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. HDU 1005 Number Sequence

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

随机推荐

  1. Linux学习之计算机基础理论

    一.描述计算机的组成及其功能. 计算机系统是由硬件系统(hardware)和软件系统(software system)两部分组成. 硬件系统: 从硬件基本结构上来讲,计算机是由运算器.控制器.存储器. ...

  2. Kneser猜想与相关推广

    本文本来是想放在Borsuk-Ulam定理的应用这篇文章当中.但是这个文章实在是太长,导致有喧宾夺主之嫌,从而独立出为一篇文章,仅供参考.$\newcommand{\di}{\mathrm{dist} ...

  3. ASP.NET-使用事件监视诊断程序异常

    用windows自带的事件监视程序来监视网站的异常 来自为知笔记(Wiz)

  4. 洛谷—— P1855 榨取kkksc03

    https://www.luogu.org/problem/show?pid=1855 题目描述 洛谷2的团队功能是其他任何oj和工具难以达到的.借助洛谷强大的服务器资源,任何学校都可以在洛谷上零成本 ...

  5. FZOJ--2214--Knapsack problem(背包)

    Problem 2214 Knapsack problem Accept: 5    Submit: 8 Time Limit: 3000 mSec    Memory Limit : 32768 K ...

  6. zzulioj--1778-- 和尚特烦恼4——有多少战斗力(gcd)

    1778: 和尚特烦恼4--有多少战斗力 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 259  Solved: 123 SubmitStatusWe ...

  7. android页面布局(listview填充中间)

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...

  8. Oozie框架基础

    * Oozie框架基础 官方文档地址:http://oozie.apache.org/docs/4.0.0/DG_QuickStart.html 除Oozie之外,类似的框架还有: ** Zeus:h ...

  9. 解析UML用例图中include与extend的区别

    UML用例图有很多值得学习的地方,这里向大家简单介绍一下UML用例图中include与extend的区别,希望本文的介绍对你有所帮助. 本文和大家重点讨论一下UML用例图中include与extend ...

  10. 6、json支持

    package main import ( "encoding/json" "fmt") // Json 支持 type Response1 struct{ P ...