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. CuratorFramework使用

    CuratorFrameworkFramework是ZooKeeper Client更高的抽象API 自动连接管理: 1. 当ZooKeeper客户端内部出现异常, 将自动进行重连或重试, 该过程对外 ...

  2. CentOS 7 下网络管理

    如果最小化安装centos7  后会发现,安装完了进去配置网络发现,ifup ,ipconfig 这些命令不见了,那是因为 7 使用了新的网络管理工具NetworkManager ,具体的命令是nmc ...

  3. linux系统坏了,进不了登陆界面怎么办?

    装oracle时,命令弄错了,结果系统崩溃之后就进不去系统了,启动后光标一直在闪烁,就是不显示登陆界面. 网上查了很多,什么grub引导之类的,完全没办法恢复系统. 系统坏了倒是无所谓,主要是系统上还 ...

  4. Asp.net管线事件(页面请求周期)

    在处理该请求时将由 HttpApplication 类执行以下事件. 希望扩展 HttpApplication 类的开发人员尤其需要注意这些事件. . 对请求进行验证,将检查浏览器发送的信息,并确定其 ...

  5. AutoCompleteTextView搭配Poi搜索实现多项选择

    项目需要 需要用到AutoCompleteTextView控件,在输入之后能在下方产生一个推荐结果的列表,就类似于金山词霸一类软件.输入一两个字符就能出来一系列类似的的单词, 这里做的例子是输入城市名 ...

  6. POJ 2643

    #include<iostream> #include<stdio.h> #include<string> #include<algorithm> #d ...

  7. 匿名类、包、权限修饰符_DAY10

    1:内部类(理解) (1)把类定义在一个类的内部. (2)特点:   A:内部类可以直接使用外部类的成员,包括私有.   B:外部类要使用内部类成员,必须创建对象使用.    例子: public c ...

  8. SQL SERVICE中的锁

    SQL SERVICE中的几个锁 SELECT * FROM dbo.TableName WITH (NOLOCK) --不加任何锁的读,脏读SELECT * FROM dbo.TableName W ...

  9. CentOS 6.7 下 MYSQL 5.7 的安装与配置

    安装 #yum源 http://dev.mysql.com/downloads/repo/yum/ #安装 rpm -Uvh http://dev.mysql.com/get/mysql57-comm ...

  10. Android之混淆心得与亲身体验

    project.properties 中设置 proguard.config=proguard-project.txt proguard-project.txt  中设置 -optimizationp ...