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

Source

2013 ACM-ICPC长沙赛区全国邀请赛——题目重现

Recommend

zhoujiaqi2010 | We have carefully selected several similar problems for you: 5185 5184 5181 5180 5177

(a+sqrt(b))^n最后的形式一定形如

X+Y * sqrt(b)

n范围非常大,须要用矩阵来加速

推出转移矩阵为

a 1

b a

(a+sqrt(b))^n = X+Y * sqrt(b)

(a-sqrt(b))^n = X-Y * sqrt(b)

a - 1< sqrt(b) < a

所以z = (a-sqrt(b))^n 大于0 小于1

设ans = (a+sqrt(b))^n

ans+z = 2 * X

2 * X - 1 < ans = 2 * X - z < 2 * X

那么向上取整就是2 * X

/*************************************************************************
> File Name: hdu4565.cpp
> Author: ALex
> Mail: zchao1995@gmail.com
> Created Time: 2015年03月14日 星期六 11时01分21秒
************************************************************************/ #include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL; LL mod; class MARTIX
{
public:
LL mat[5][5];
MARTIX();
MARTIX operator * (const MARTIX &b)const;
MARTIX& operator = (const MARTIX &b);
}; MARTIX::MARTIX()
{
memset (mat, 0, sizeof(mat));
} MARTIX MARTIX :: operator * (const MARTIX &b)const
{
MARTIX ret;
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 2; ++j)
{
for (int k = 0; k < 2; ++k)
{
ret.mat[i][j] += this -> mat[i][k] * b.mat[k][j];
ret.mat[i][j] %= mod;
}
}
}
return ret;
} MARTIX& MARTIX :: operator = (const MARTIX &b)
{
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 2; ++j)
{
this -> mat[i][j] = b.mat[i][j];
}
}
return *this;
} MARTIX fastpow(MARTIX ret, LL n)
{
MARTIX ans;
for (int i = 0; i < 2; ++i)
{
ans.mat[i][i] = 1;
}
while (n)
{
if (n & 1)
{
ans = ans * ret;
}
ret = ret * ret;
n >>= 1;
}
return ans;
} void Debug(MARTIX A)
{
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 2; ++j)
{
printf("%lld ", A.mat[i][j]);
}
printf("\n");
}
} int main ()
{
LL a, b, n;
while (~scanf("%lld%lld%lld%lld", &a, &b, &n, &mod))
{
MARTIX A;
A.mat[0][0] = a;
A.mat[0][1] = 1;
A.mat[1][0] = b;
A.mat[1][1] = a;
A = fastpow(A, n - 1);
MARTIX F;
F.mat[0][0] = a;
F.mat[0][1] = 1;
F = F * A;
printf("%lld\n", (2 * F.mat[0][0]) % mod);
}
return 0;
}

hdu4565---So Easy!(矩阵)的更多相关文章

  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. HDU4565 So Easy! 矩阵高速幂外加数学

    easy 个屁啊,一点都不easy,题目就是要求公式的值,但是要求公式在最后的取模前的值向上取整.再取模,无脑的先试了高速幂 double  fmod来做,结果发现是有问题的.这题要做肯定得凑整数,凑 ...

  3. HDU4565 So Easy! —— 共轭构造、二阶递推数列、矩阵快速幂

    题目链接:https://vjudge.net/problem/HDU-4565 So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory L ...

  4. HDU4565 So Easy!

    /* HDU4565 So Easy! http://acm.hdu.edu.cn/showproblem.php?pid=4565 数论 快速幂 矩阵快速幂 题意:求[(a+sqrt(b))^n ] ...

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

  6. 2013长沙邀请赛A So Easy!(矩阵快速幂,共轭)

    So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. HDU 4565 So Easy(矩阵解公式)

    So Easy [题目链接]So Easy [题目类型]矩阵解公式 &题解: 感觉这种类型的题都是一个套路,这题和hdu 2256就几乎是一样的. 所以最后2Xn就是答案 [时间复杂度]\(O ...

  8. HDU 4565 So Easy!(矩阵)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4565 题意: 题意: #include <iostream>#include <cs ...

  9. HDU 4565 So Easy! 矩阵快速幂

    题意: 求\(S_n=\left \lceil (a+\sqrt{b})^n \right \rceil mod \, m\)的值. 分析: 设\((a+\sqrt{b})^n=A_n+B_n \sq ...

随机推荐

  1. FZOJ--2212--Super Mobile Charger(水题)

    Problem 2212 Super Mobile Charger Accept: 3    Submit: 11 Time Limit: 1000 mSec    Memory Limit : 32 ...

  2. rsync同步操作命令

    在本地磁盘同步数据 将/home做个备份 # rsync -a --delete /home /backups -a 归档模式,表示以递归方式传输文件, -delete 删除那些接收端还有而发送端已经 ...

  3. vue --- 路由传参的几种方式

    方案一: getDescribe(id) { // 直接调用$router.push 实现携带参数的跳转 this.$router.push({ path: `/describe/${id}`, }) ...

  4. 30.algorithm排序小结

    如果容器中是类,如果要调用sort则需要重载操作符 "<" 包含头文件 #define _CRT_SECURE_NO_WARNINGS #include <vector ...

  5. java9新特性-5-Java的REPL工具: jShell命令

    1.官方Feature 222: jshell: The Java Shell (Read-Eval-Print Loop) 2.产生背景 像Python 和 Scala 之类的语言早就有交互式编程环 ...

  6. ItemTouchHelper(实现RecyclerView上添加拖动排序与滑动删除的所有事情)

    简单介绍: ItemTouchHelper是一个强大的工具,它处理好了关于在RecyclerView上添加拖动排序与滑动删除的所有事情.它是RecyclerView.ItemDecoration的子类 ...

  7. Bayes++ Library入门学习之熟悉namespace

    Bayes++是一个开源的C++类库.这些类表示并实现了用于离散系统的贝叶斯滤波的各种数值算法.该库中的类提供测试和一致的数值方法,并且用层次明确的结构表明了各种滤波算法和系统模型类型. 接下来,我们 ...

  8. Chrome Foundation Services

    Chrome Foundation Services Contents Overview API Standards Service Directory Structure Dependencies ...

  9. Division Game UVA - 11859 Nim

    Code: #include<cstdio> #include<algorithm> using namespace std; #define maxn 10005 int n ...

  10. CF402E Strictly Positive Matrix(矩阵,强联通分量)

    题意 给定一个 n∗n 的矩阵 A,每个元素都非负判断是否存在一个整数 k 使得 A^k 的所有元素 >0 n≤2000(矩阵中[i][i]保证为1) 题解 考虑矩阵$A*A$的意义 ,设得到的 ...