So Easy!

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

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
 
Recommend
zhoujiaqi2010   |   We have carefully selected several similar problems for you:  6331 6330 6329 6328 6327 
 
求x=(a+sqrt(b))向上取整
求Sn=x^n%mod
 
记(a+sqrt(b))为An,(a-sqrt(b))为Bn
根据题目中对b的限定( (a-1)2< b < a)
有Sn=An+Bn=((a+sqrt(b))^n+(a-sqrt(b))^n)%mod

综述得到一个递推式S(n) = 2*a*S(n-1)+(b-a*a)*S(n-2)   这个公式对所有如 (a+sqrt(b))^n%mod形式求整数的式子都适应

我们只需要用矩阵快速幂求这个递推式就行

参考博客:https://blog.csdn.net/chen_ze_hua/article/details/52072732

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 2; //数组的大小尽量开的小,开大了会tle!!!
const ll mod = 1e9 + 7;
struct matrix {
ll a[maxn][maxn];
};
matrix ans, base;
ll m;
matrix mul( matrix x, matrix y ) {
matrix tmp;
for( ll i = 0; i < 2; i ++ ) {
for( ll j = 0; j < 2; j ++ ) {
tmp.a[i][j] = 0;
for( ll k = 0; k < 2; k ++ ) {
tmp.a[i][j] = ( tmp.a[i][j] + x.a[i][k]*y.a[k][j] + m ) % m;
}
}
}
return tmp;
}
ll qow( ll n ) {
while( n ) {
if( n&1 ) {
ans = mul( ans, base );
}
base = mul( base, base );
n /= 2;
}
return ans.a[0][0];
}
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
ll a, b, n;
while( cin >> a >> b >> n >> m ) {
double tmp = a + sqrt(b);
ll t1 = (ll)(tmp+1), t2 = (ll)(tmp*tmp+1);
base.a[0][0] = 2*a, base.a[0][1] = 1;
base.a[1][0] = (b-a*a+m)%m, base.a[1][1] = 0;
ans.a[0][0] = t2, ans.a[0][1] = t1;
ans.a[1][0] = 0, ans.a[1][1] = 0;
if( n == 1 ) {
cout << t1 << endl;
} else if( n == 2 ) {
cout << t2 << endl;
} else {
cout << qow(n-2) << endl;
}
}
return 0 ;
}

  

HDU 4565 So Easy! 广义斐波拉数 数论 (a+sqrt(b))^n%mod 模板的更多相关文章

  1. 【斐波拉契+数论+同余】【ZOJ3707】Calculate Prime S

    题目大意: S[n] 表示 集合{1,2,3,4,5.......n} 不存在连续元素的子集个数 Prime S 表示S[n]与之前的所有S[i]互质; 问 找到大于第K个PrimeS 能整除X 的第 ...

  2. HDU 5451 广义斐波那契数列

    这道题目可以先转化: 令f(1) = 5+2√6 f(2) = f(1)*(5+2√6) ... f(n) = f(n-1)*(5+2√6) f(n) = f(n-1)*(10-(5-2√6)) = ...

  3. hdu 2516(斐波拉切博弈)

    题意:容易理解. 分析:通过枚举寻找规律,这就是做1堆或者2堆石子博弈的技巧!当为2或者3时,肯定是第二个人赢,当为4时,先去一个石子,然后当对方面临3,于是第一个人赢, 当为5时,取1时,第二个人赢 ...

  4. hdu 5914(斐波拉契数列)

    Triangle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  5. 关于斐波拉契数列(Fibonacci)

    斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10 ...

  6. HDU-4794:Arnold(斐波拉契循环节 二次剩余)

    本题我只是个搬运工,主要是抢救补板子,所以自己就没写.https://blog.csdn.net/u013534123/article/details/78058997 题意: 大致题意是给你一个N* ...

  7. python迭代器实现斐波拉契求值

    斐波那契数列(Fibonacci sequence),又称黄金分割数列,也称为"兔子数列":F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n≥2,n∈N*).例 ...

  8. 斐波拉契数列加强版——时间复杂度O(1),空间复杂度O(1)

    对于斐波拉契经典问题,我们都非常熟悉,通过递推公式F(n) = F(n - ) + F(n - ),我们可以在线性时间内求出第n项F(n),现在考虑斐波拉契的加强版,我们要求的项数n的范围为int范围 ...

  9. 洛谷P1962 斐波那契数列 || P1349 广义斐波那契数列[矩阵乘法]

    P1962 斐波那契数列 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数 ...

随机推荐

  1. Codeforces Round #219(Div. 2)373 B. Making Sequences is Fun(二分+找规律)

    题目意思大概是给你w,m,k三个数,让你从m开始找 m m+1 m+2 m+3...........m+m', 使得他们的权值之和不超过w,计算权值的方法如下S(n)·k . S(n)表示n有多少位数 ...

  2. vue-cli3.0创建项目报npm install --loglevel error 踩坑的那把辛酸泪

    创建项目 vue create vue-pro 然后如下图 一开始以为是npm的问题,卸载了Mac的node ,安装nvm,然后再安装node (可参考: Mac中nvm的安装和使用   https: ...

  3. Git 服务使用搭建集合

    Git 服务使用搭建集合 一.本地Git 仓库搭建与使用 1.Git 概念介绍 版本控制系统 版本控制是一种记录若干文件内容变化,以便将来查阅特定版本修订情况的系统.大部分时候我们使用最频繁的还是对源 ...

  4. log4net服务启动后没有记录日志

    有时候在用log4net的时候,调试或执行是ok的,但是安装服务后没有记录日志. 这是因为服务启动是在C盘启动,而程序放的位置在别的目录. 这时候需要指定读取配置文件的位置为程序所在的目录 strin ...

  5. Go中sync包学习

    前面刚讲到goroutine和channel,通过goroutine启动一个协程,通过channel的方式在多个goroutine中传递消息来保证并发安全.今天我们来学习sync包,这个包是Go提供的 ...

  6. java学习-NIO(一)简介

    I/O简介 在 Java 编程中,直到最近一直使用 流 的方式完成 I/O.所有 I/O 都被视为单个的字节的移动,通过一个称为 Stream 的对象一次移动一个字节.流 I/O 用于与外部世界接触. ...

  7. GBK和UTF-8的区别

    我们这里将以最简单最容易理解的方式来描述GBK和UTF8的区别,以及它们分别是什么.   GBK编码:是指中国的中文字符,其它它包含了简体中文与繁体中文字符,另外还有一种字符“gb2312”,这种字符 ...

  8. 下拉框spinner

    repositories { flatDir { dirs 'libs' //就是你放aar的目录地址 maven { url "https://jitpack.io" } }}d ...

  9. spring架构解析--入门一

    Spring 框架中的核心组件只有三个:Core.Context 和 Beans.它们构建起了整个 Spring 的骨骼架构.简单理解: spring core是工具,context是环境,而bean ...

  10. js网页倒计时功能(天,时,分,秒)

    给定任何一个时间,然后实现现在到那个时间的倒计时. 下面的例子是显示现在到2019年8月1号0时0分的倒计时: <div class="list"> <span ...