Number Sequence

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

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

 
 
Sample Input
1 1 3
1 2 1
0
0 0 0
 
Sample Output
2
5
 因为数据量比较大,可以打表找到循环规律,但这种类型的题发现都可以构造矩阵求解
f[n]      =  a  b   *   f[n-1]
f[n-1]                  1  0    f[n-2]
 
 
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int a,b,n;
int m[];
struct Matrix
{
int mat[][];
}p;
Matrix mul(Matrix a,Matrix b)
{
Matrix c;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
c.mat[i][j]=;
for(int k=;k<;k++)
c.mat[i][j]=(c.mat[i][j]+a.mat[i][k]*b.mat[k][j])%;
}
}
return c;
}
Matrix mod_pow(Matrix x,int n)
{
Matrix res;
memset(res.mat,,sizeof(res.mat));
for(int i=;i<;i++)
res.mat[i][i]=;
while(n)
{
if(n&)
res=mul(res,x);
x=mul(x,x);
n>>=;
}
return res;
}
int main()
{
freopen("in.txt","r",stdin);
while(scanf("%d%d%d",&a,&b,&n)!=EOF)
{
if(a==&&b==&&n==)
break;
if(n<)
{
printf("1\n");
continue;
}
p.mat[][]=a;
p.mat[][]=;
p.mat[][]=b;
p.mat[][]=;
Matrix ans= mod_pow(p,n-);
printf("%d\n",(ans.mat[][]+ans.mat[][])%);
}
}
 

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

  1. HDU 5667 构造矩阵快速幂

    HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...

  2. 【 CodeForces - 392C】 Yet Another Number Sequence (二项式展开+矩阵加速)

    Yet Another Number Sequence Description Everyone knows what the Fibonacci sequence is. This sequence ...

  3. (KMP 模板)Number Sequence -- Hdu -- 1711

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Other ...

  4. AC日记——Number Sequence hdu 1711

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. Number Sequence HDU - 5014

    There is a special number sequence which has n+1 integers. For each number in sequence, we have two ...

  6. HDUOJ Number Sequence 题目1005

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

  7. E - Recursive sequence HDU - 5950 (矩阵快速幂)

    题目链接:https://vjudge.net/problem/HDU-5950 思路: 构造矩阵,然后利用矩阵快速幂. 1 #include <bits/stdc++.h> 2 #inc ...

  8. Number Sequence HDU 1711(KMP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 首次接触KMP,自己都不是特别理解.在网上百度看了好几个帖子之后,对KMP也有了初步的理解. #inclu ...

  9. Number Sequence - HDU 1711(KMP模板题)

    题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1   分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...

随机推荐

  1. uva 10026 Shoemaker's Problem

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  2. BZOJ3391: [Usaco2004 Dec]Tree Cutting网络破坏

    3391: [Usaco2004 Dec]Tree Cutting网络破坏 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 47  Solved: 37[ ...

  3. 使用MapReduce将HDFS数据导入到HBase(二)

    package com.bank.service; import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf. ...

  4. http常见错误

    100:继续  客户端应当继续发送请求.客户端应当继续发送请求的剩余部分,或者如果请求已经完成,忽略这个响应. 101: 转换协议  在发送完这个响应最后的空行后,服务器将会切换到在Upgrade 消 ...

  5. JS浏览器对象-window对象

    代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title ...

  6. final(最终、修饰符)

    /* final(最终.修饰符) final关键字的用法: 1. final关键字修饰一个基本类型的变量时,该变量不能重新赋值,第一次的值为最终的. 2. fianl关键字修饰一个引用类型变量时,该变 ...

  7. MVC VIEW 时间格式控制

    @Convert.ToDateTime(Model.CheckPatronExclusionResults.RequestTime).ToString("yyyy-MM-dd HH:mm:s ...

  8. libsvm java 调用说明

    libsvm是著名的SVM开源组件,目前有JAVA.C/C++,.NET 等多个版本,本人使用的是2.9libsvm命名空间下主要使用类:svm_model 为模型类,通过训练或加载训练好的模型文件获 ...

  9. 导航条上UIBarButtonItem的更改方法(使用initWithCustomView:btn)

    UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:self.newMe ...

  10. UVA10765-Doves and bombs(BCC)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=1706">题目链接 题意:给定一个n个点的 ...