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. Python 常用的内置函数

    1. str.isdigit( ) 作用:检测字符串是否有数字组成 2. strip( ) 作用:除去首尾指定的字符,包括空格,换行符,不能除去中间的字符 3. slice( ) 作用:以指定参数切割 ...

  2. BZOJ 5281--[Usaco2018 Open]Talent Show(分数规划&单调队列&DP)

    5281: [Usaco2018 Open]Talent Show Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 79  Solved: 58[Sub ...

  3. 利用koa实现mongodb数据库的增删改查

    概述 使用koa免不了要操纵数据库,现阶段流行的数据库是mongoDB,所以我研究了一下koa里面mongoDB数据库的增删改查,记录下来,供以后开发时参考,相信对其他人也有用. 源代码请看:我的gi ...

  4. CSV Data Set Config设置

    Jmeter参数化常用的两种方法: 1.使用函数助手 2.CSV Data Set Config 本章主要讲解CSV Data Set Config设置 1.Filename:文件名,指保存信息的文件 ...

  5. 开发ASP.NET MVC 在线录音录像(音视频录制并上传)

    最近有个在线招聘录音的开发需求,需要在招聘网站上让招聘者上传录音和视频. 找到两个不错的javascript开源,可以在除了IE以外的浏览器运行. https://github.com/mattdia ...

  6. SpringMVC初探-HelloWorld

    MVC的概念 MVC是一种设计模式,即Model--View-Controller,模型--视图--控制器 Model(模型)表示应用程序核心(比如数据库记录列表). View(视图)显示数据(数据库 ...

  7. #Python学习#python虚拟环境——virtualenv

    前言 在Ubuntu系统中,系统一般会默认安装python2.x和3.x,像我近期买的阿里云ECS默认安装了2.7.2和3.5.2,所有pip安装的第三方包都会被放在默认的site-apckages目 ...

  8. odoo开发笔记 -- 视图继承扩展

    参考: http://www.jeffzhang.cn/Odoo-Notes-2/ http://blog.csdn.net/zhangfeng1133/article/details/4693517 ...

  9. keytool和openssl生成的证书转换

    keytool和openssl生成的证书转换 keytool生成证书示例 生成私钥+证书: keytool -genkey -alias client -keysize 2048 -validity ...

  10. 02-02:springboot 整合filter

    1.通过注解扫描完成Filter组件的注册 1.1编写filter (添加拦截的servlet) //@WebFilter(filterName = "FirstFilter",u ...