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
 
 

以下是快速幂的模板, 求a^n, 为防止溢出,中间最好mod一哈

int main(){
int n;
int a;
cin >> a>>n;
int ans = 1;
while(n){//a^n
if(n&1){
ans *= a;
}
a*=a;
n>>=1;
ans %= 10000007;
}
cout << ans<<endl;
return 0;
}

本题的递推式:

代码(含矩阵快速幂):

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<stdlib.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int maxn = 1e5 + 100;
double eps = 1e-8;
int a[4][4], b[4][4];
int ans[4][4]; struct mtx{
int a[4][4];
};
mtx mtpl(mtx x, mtx y){//将矩阵a乘b的结果直接放在tmp里
mtx tmp;
memset(tmp.a, 0, sizeof(tmp.a));
for(int i = 1; i <= 2; i++){
for(int j = 1; j <= 2; j++){
int sum = 0;
for(int k = 1; k <= 2; k++){
sum += x.a[i][k] * y.a[k][j];
}
sum%=7;
tmp.a[i][j] += sum;
}
}
return tmp;
}
int main(){
int A, B;
ll n;
while(scanf("%d %d %lld", &A, &B, &n) ){
if(A==B && B==n && n==0)break;
mtx a, ans;
ans.a[1][1] = ans.a[2][2] = 1;
ans.a[1][2] = ans.a[2][1] = 0;
a.a[1][1] = A;
a.a[1][2] = B;
a.a[2][1] = 1;
a.a[2][2] = 0;
n-=2; //注意这里求的是a^(n-2)而不是a^n
if(n==-1) {
cout << 1<<endl;
continue;
}
while(n){
if(n&1)ans=mtpl(ans, a);
a=mtpl(a, a);
n>>=1;
}
cout << (ans.a[1][1]+ans.a[1][2]) %7<< endl;//人家说最后结果mod 7,WA的时候没想到这里要%7。。不是第一次了
}
return 0;
}

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(矩阵快速幂系数变式)

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

  3. HDU 1005 Number Sequence(数列)

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

  4. HDU 1005 Number Sequence(数论)

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

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

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

  7. HDU 1005 Number Sequence(矩阵)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  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. how to render html tag

    使用autoescaping If autoescaping is turned on in the environment, all output will automatically be esc ...

  2. Java 循环队列

    传统数组实现的队列有缺陷,当多次入队出队后,队头指针会后移,当队尾指针达到数组末尾时,会提示队列已满,导致数组前部分空间被浪费.如果当队尾和队头指针到达数组末尾时能从数组[0]继续添加数据,可以提升数 ...

  3. .NET绘制旋转太极图

    .NET绘制旋转太极图 我之前发了一篇<用.NET写"算命"程序>的文章,但有人纷纷提出了质疑,认为没有"科学"(mi xin)依据

  4. crawler碎碎念4 关于python requests、Beautiful Soup库、SQLlite的基本操作

    Requests import requests from PIL import Image from io improt BytesTO import jason url = "..... ...

  5. AI——第四次工业革命

    历史上发生了三次工业革命:第一次是以蒸汽机的发明为代表,改变了长久以来的棉纺织业,人类进入"蒸汽时代":第二次是以汽车的发明和电学的发展为代表,人类的活动范围和时长都极大地扩展,人 ...

  6. Django自定义状态码

    class BaseResponse: def __init__(self): self.code = 1000 self.data = None self.error = None @propert ...

  7. [组合数学][多项式][拉格朗日插值]count

    源自 ditoly 大爷的 FJ 省队集训课件 Statement 有 \(m\) 个正整数变量,求有多少种取值方案 使得所有变量的和不超过 \(S\) 并且前 \(n\) 个变量的值都不超过 \(t ...

  8. Quartz cron 表达式(linux 定时器,java 定时任务,spring task定时任务)

    原文地址:https://blog.csdn.net/feng27156/article/details/39293403 Quartz cron 表达式的格式十分类似于 UNIX cron 格式,但 ...

  9. python条件与循环-条件

    1.条件和循环 主要讨论:if.while.for以及相关的搭配else.elif.break.continue和pass语句. 1.1 if语句 if语句由三部分组成:关键字本身.用于判断结果真假的 ...

  10. 使用Azure CLI创建Linux虚拟机

    Azure提供了三种方式创建虚拟机,分别是Azure CLI.Azure PowerShell和Azure门户.本文介绍使用Azure CLI来创建Linux虚拟机. 使用Azure CLI创建Lin ...