So Easy!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4673    Accepted Submission(s): 1539

Problem Description

  A sequence Sn is defined as:

Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate Sn.
  You, a top coder, say: So easy! 
 

Input

  There are several test cases, each test case in one line contains four positive integers: a, b, n, m. Where 0< a, m < 215, (a-1)2< b < a2, 0 < b, n < 231.The input will finish with the end of file.
 

Output

  For each the case, output an integer Sn.
 

Sample Input

2 3 1 2013
2 3 2 2013
2 2 1 2013
 

Sample Output

4
14
4
 
根据条件可证(a-√b)n小于1,(a+√b)n向上取整即为求(a+√b)n + (a-√b)n,问题又转换为a^n+b^n的形式。
 //2017-08-05
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long using namespace std; ll a, b, n;
ll p, q;
const int N = ;
ll MOD; struct Matrix
{
ll a[N][N];
int r, c;
}ori, res; void init()
{
memset(res.a, , sizeof(res.a));
res.r = ; res.c = ;
res.a[][] = p;
res.a[][] = ;
ori.r = ; ori.c = ;
ori.a[][] = p;
ori.a[][] = ;
ori.a[][] = -q;
ori.a[][] = ;
} Matrix multi(Matrix x, Matrix y)
{
Matrix z;
memset(z.a, , sizeof(z.a));
z.r = x.r, z.c = y.c;
for(int i = ; i <= x.r; i++)
{
for(int k = ; k <= x.c; k++)
{
if(x.a[i][k] == ) continue;
for(int j = ; j<= y.c; j++)
z.a[i][j] = (z.a[i][j] + (x.a[i][k] * y.a[k][j]) % MOD + MOD) % MOD;
}
}
return z;
} void Matrix_pow(int n)
{
while(n)
{
if(n & )
res = multi(res, ori);
ori = multi(ori, ori);
n >>= ;
}
printf("%lld\n", res.a[][] % MOD);
} int main()
{
while(scanf("%lld%lld%lld%lld", &a, &b, &n, &MOD)!=EOF){
p = *a;
q = a*a-b;
init();
if(n == )printf("2\n");
else if(n == )printf("%lld\n", p);
else Matrix_pow(n-);
} return ;
}
Source

HDU4565(SummerTrainingDay05-C 矩阵快速幂)的更多相关文章

  1. hdu4565 So Easy! 矩阵快速幂

    A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example ...

  2. HDU2256&&HDU4565:给一个式子的求第n项的矩阵快速幂

    HDU2256 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2256 题意:求(sqrt(2)+sqrt(3))^2n%1024是多少. 这个题算是h ...

  3. hdu4565 So Easy!(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4565 题解:(a+√b)^n=xn+yn*√b,(a-√b)^n=xn-yn*√b, (a+√b)^n ...

  4. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  5. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  6. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  7. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  8. HDU5950(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...

  9. 51nod 1126 矩阵快速幂 水

    有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...

  10. hdu2604(递推,矩阵快速幂)

    题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...

随机推荐

  1. Linux基础命令-mkdir

    mkdir 作用:创建目录 主要参数:-p $ cd /home/test $ mkdir caianchun $ ls caianchun -p:递归参数 $ mkdir caianchun2/1/ ...

  2. Web安全测试学习手册-业务逻辑测试

    i春秋作家:Vulkey_Chen 首先感谢朋友倾璇的邀请 http://payloads.online/archivers/2018-03-21/1 ,参与了<web安全测试学习手册>的 ...

  3. java环境的配置与安装(windows、macos、linux)

    一.windows下: 1.下载jdk:https://www.oracle.com2.安装教程:https://www.cnblogs.com/zlslch/p/5658399.html 二.mac ...

  4. Mybatis 逆向工程学习随笔

    一.逆向工程的作用 简单来说,就是替我们生成Java代码. 之前使用Mybatis的Mapper代理方法开发,还需要自己创建实体类,而且属性还得和数据库中的字段对应.这着实是机械化的而且比较麻烦的事, ...

  5. cookie、session的区别

    相信你肯定经常听说cookie和Session,那你有没有好好了解这两个的区别呢?其实,不整理之前,我也是一脸懵. 为什么需要cookie和session呢?---因为Http是无状态的,web开发中 ...

  6. (转)percona的安装、启动、停止

    原文:https://blog.csdn.net/tanliqing2010/article/details/78758878 socket=/percona/3307/data/mysql.sock ...

  7. css实现图片垂直居中

    1.行高 == 高度:已知图片的高宽 <div style="text-align: center; width: 500px;height:200px; line-height:20 ...

  8. error C4996: 'scanf': This function or variable may be unsafe.

    项目属性-配置属性-c/c++-预处理器- 在下面的编辑窗口中添加一句命令:_CRT_SECURE_NO_WARNINGS 添加完成后应用并退出 http://jingyan.baidu.com/al ...

  9. CSS3 Drop-Shadows效果制作教程分享

    要求 必备知识 基本了解CSS语法,初步了解CSS3语法知识. 开发环境 Adobe Dreamweaver CS6/Chrome浏览器 演示地址 演示地址 Drop-Shadow效果,其实就是大家熟 ...

  10. Doxygen自动文档生成工具在Eclipse中的集成及使用举例

    你有为软件编写说明文档的苦恼吗?当别人甩给你一个庞大的系统,让你根据里面的代码注释理解后写出一份完整的开发文档,你会怎么办?一个个的看代码 然后耗时N天来写吗?这既是一份苦差事也极其耗时,有没有更好的 ...