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. docker-代理服务器

    配置Docker以使用代理服务器 如果容器需要使用HTTP,HTTPS或FTP代理服务器,则可以通过不同方式对其进行配置: 在Docker 17.07及更高版本中,可以 将Docker客户端配置为自动 ...

  2. Spring boot项目搭建及简单实例

    Spring boot项目搭建 Spring Boot 概述 Build Anything with Spring Boot:Spring Boot is the starting point for ...

  3. 2D地图擦除算法

    . 关于2D地图擦除算法,去年我写过一个实现,勉强实现了地形擦除,但跟最终效果还相差甚远,这次我写了一个完整的实现,在此记录,留个印象. . 去年的版本<<算法 & 数据结构--裁 ...

  4. dp - 逆序数序列

    对于一个数列{ai},如果有i<j且ai>aj,那么我们称ai与aj为一对逆序对数.若对于任意一个由1~n自然数组成的 数列,可以很容易求出有多少个逆序对数.那么逆序对数为k的这样自然数数 ...

  5. 定时器之Quart.net(1)

    第一步:Install-Package Quartz namespace ProjectEdb { class Program { static void Main(string[] args) { ...

  6. django restful 序列化

    https://www.cnblogs.com/wt7018/p/11456440.html https://www.cnblogs.com/wt7018/p/11530962.html

  7. Scrapy信号量

    1.类 from scrapy import signals class MySingle(object): def __init__(self): pass @classmethod def fro ...

  8. GStreamer基础教程13 - 调试Pipeline

    摘要 在很多情况下,我们需要对GStreamer创建的Pipeline进行调试,来了解其运行机制以解决所遇到的问题.为此,GStreamer提供了相应的调试机制,方便我们快速定位问题. 查看调试日志 ...

  9. Day2-Python3基础-文件操作

    1. 字符编码与转码 需知: 1.在python3默认编码是unicode 2.unicode 分为 utf-32(占4个字节),utf-16(占两个字节),utf-8(占1-4个字节), so ut ...

  10. Linux中软件安装包的格式

    一.Linux常用安装包及安装方法 1.安装包一般有四类: 1)tar包,如software-1.2.3-1.tar.gz.他是使用UNIX系统的打包工具tar打包的. 2)rpm包,如softwar ...