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. BootStrap有用代码片段(持续总结)

    > 如题.持续总结自己在使用BootStrap中遇到的问题.并记录解决方法.希望能帮到须要的小伙伴 1.bootstrap上下布局.顶部固定下部填充 应用场景:经典上下布局中,顶部导航条固定,下 ...

  2. Import Example Dataset

    Overview The examples in this guide use the restaurants collection in the test database. The followi ...

  3. uwsgi和wsgi

    一个Web应用的本质就是: 浏览器发送一个HTTP请求: 服务器收到请求,生成一个HTML文档: 服务器把HTML文档作为HTTP响应的Body发送给浏览器: 浏览器收到HTTP响应,从HTTP Bo ...

  4. kotlin官方文档-1.0入门

    什么是Kotlin?   图片发自简书App Kotlin是JetBrains开发的基于JVM的语言,JetBrains想必大家应该很熟悉了,他们创造了很多强大的IDE,android studio谷 ...

  5. Android控件postDelayed用法,View自带的定时器

    有一个需求是这样的,点击加关注按钮后,执行关注操作,成功后按钮文字变为“已关注”,保持3秒,三秒后按钮文字便问“取消关注”,点击后执行取消关注的操作 源码: public boolean postDe ...

  6. Activity的启动模式和onNewIntent()

    1:首先,在默认情况下,当您通过Intent启到一个Activity的时候,就算已经存在一个相同的正在运行的Activity,系统都会创建一个新的Activity实例并显示出来.为了不让Activit ...

  7. HDU 5214 Movie【贪心】

    题意:给出n,l[1],r[1],a,b,c,d 其中 l[i]=l[i-1]*a+b,r[i]=r[i-1]*c+d; 问能否从这n个区间中选出不相交不重叠的三个区间 看的题解 先考虑最左边的区间, ...

  8. 每位 Ubuntu 18.04 用户都应该知道的快捷键

    作者: Abhishek Prakash 译者: LCTT XiatianSummer 了解快捷键能够提升您的生产力.这里有一些实用的 Ubuntu 快捷键助您像专业人士一样使用 Ubuntu. 您可 ...

  9. BZOJ 3637: Query on a tree VI LCT_维护子树信息_点权转边权_好题

    非常喜欢这道题. 点权转边权,这样每次在切断一个点的所有儿子的时候只断掉一条边即可. Code: #include <cstring> #include <cstdio> #i ...

  10. 解决zabbix容器中文无法选择的问题

    1.查看zabbiz-web运行的容器 2.进入容器 3.安装语言包(针对centos7) yum install -y kde-l10n-chinese 4.更新gitbc包(因为镜像阉割了该包的部 ...