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. Linux中du、df显示不一致问题

    Linux中du.df显示不一致问题 最近在做关于Q博士的项目的时候,用到了docker进行部署,对于后端服务可能会经常变动,于是将docker容器的jar包与宿主机目录下的jar包进行绑定,之后每次 ...

  2. Arrays.asList 返回值类型

    public static void main(String[] args) { Integer[] datas = {1,2,3,4,5}; List<Integer> list = A ...

  3. Ado.net总结-Command(指挥官)

    Command 概述 在 System.Data.SqlClient 命名空间下,对应的 Command 类为 SqlCommand,在创建 SqlCommand 实例前必须已经创建了与数据库的连接. ...

  4. i3s 一种开源的三维地理数据规范 简单解读

    i3s,esri主推到ogc的一种三维开源GIS数据标准. 版权声明:原创.博客园/B站/小专栏/知乎/CSDN @秋意正寒 转载请标注原地址并声明转载: https://www.cnblogs.co ...

  5. jav设计模之的动态代理

    在学习Spring的时候,我们知道Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,依赖注入就不用多说了,而对于Spring的核心AOP来说,我们不但要知道怎么通过AOP来满足的 ...

  6. Replacing the deprecated Java JPEG classes for Java 7

    [src: https://blog.idrsolutions.com/2012/05/replacing-the-deprecated-java-jpeg-classes-for-java-7/] ...

  7. CountDownLatch,CyclicBarrier,Semaphore用法

    1.让一些线程阻塞直到另一些线程完成一系列操作后才被唤醒. 2.CountDownLatch主要有两个方法,当一个或多个线程调用await方法时,调用线程会被阻塞.其它线程调用countDown方法会 ...

  8. windows环境下使用python3.x自带的CGI服务器测试cgi脚本(转)

    1.在桌面上新建一个文件夹作为服务器目录文件夹(文件夹名称自定义,文件夹位置自定义),在www文件下再建一个文件夹,文件夹名为“cgi-bin”,须是这个文件名,其他试过不行(原因暂时未知)

  9. msi通过powershell安装、卸载

    function install_msi($url) { $telemetry = @{ DisplayName = "Telemetry Service"; filename = ...

  10. XML学习笔记——关于XML解析器

    本篇文章基于W3C而写 在Firefox及其他浏览器中的XML解析器(除IE) var xmlDoc=document.implementation.createDocument("&quo ...