So Easy!

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

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的范围推出

其中Cn=ceil(a+sqrt(b))
C0=2,C1=2*a,还有一个坑点就是最后输出的答案要向正方向取模,WA好几次
代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
LL mod;
struct mat
{
LL pos[2][2];
mat(){memset(pos,0,sizeof(pos));}
};
inline mat operator*(const mat &a,const mat &b)
{
mat c;
for (int i=0; i<2; i++)
for (int j=0; j<2; j++)
for (int k=0; k<2; k++)
c.pos[i][j]+=((a.pos[i][k]%mod)*(b.pos[k][j]%mod)+mod)%mod;
return c;
}
inline mat matpow(mat a,int b)
{
mat bas,r;
r.pos[0][0]=r.pos[1][1]=1;
bas=a;
while (b!=0)
{
if(b&1)
r=r*bas;
bas=bas*bas;
b>>=1;
}
return r;
}
int main(void)
{
LL a,b,n;
while (~scanf("%lld%lld%lld%lld",&a,&b,&n,&mod))
{
mat one,t;
one.pos[0][0]=2*a;one.pos[1][0]=2; t.pos[0][0]=2*a;t.pos[0][1]=-(a*a-b);
t.pos[1][0]=1;t.pos[1][1]=0; t=matpow(t,n);
one=t*one;
printf("%lld\n",(one.pos[1][0]%mod+mod)%mod);
}
return 0;
}

HDU——4565So 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. HDU.2640 Queuing (矩阵快速幂)

    HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...

  3. HDU 5667 构造矩阵快速幂

    HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...

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

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

  5. HDU 6185 Covering 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6185 题意:用 1 * 2 的小长方形完全覆盖 4 * n的矩形有多少方案. 解法:小范围是一个经典题 ...

  6. HDU 2157(矩阵快速幂)题解

    How many ways?? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij

    http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others)    Me ...

  8. HDU 6470 【矩阵快速幂】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6470 写这道题是为了让自己不要忘记矩阵快速幂如何推出矩阵式子的. 注意 代码是TLE的!! #incl ...

  9. HDU 5607 graph 矩阵快速幂 + 快速幂

    这道题得到了学长的助攻,其实就是一个马尔科夫链,算出一步转移矩阵进行矩阵快速幂就行了,无奈手残 这是我第一回写矩阵快速幂,写的各种毛病,等到调完了已经8点44了,交了一发,返回PE,(发现是少了换行) ...

  10. HDU 1575(裸矩阵快速幂)

    emmmmm..就是矩阵快速幂,直接附代码: #include <cstdio> using namespace std; ; ; struct Matrix { int m[maxn][ ...

随机推荐

  1. SAP云平台的Document Service

    SAP云平台以微服务的方式提供了Document的CRUD(增删改查)操作.该微服务基于标准的CMIS协议(Content Management Interoperability Service). ...

  2. UVA 690 PipelineScheduling 位运算+dfs+剪枝

    一开始最容易想到间隔最多为n,但是结点还是太多了,需要优化. 预处理:预判一下并保存下一个可以放的位置距离之前的距离.这样可以减少很多判断. 最优化剪枝:如果当前长度+剩下没放的程序*最短间隔如果大于 ...

  3. LeetCode分类-前400题

    1. Array 基础 27 Remove Element 26 Remove Duplicates from Sorted Array 80 Remove Duplicates from Sorte ...

  4. shell的切换

    从zsh切换到bash:在命令行输入bash即可 从bash切换到zsh:在命令行输入zsh即可

  5. (转发)IOS高级开发~Runtime(三)

    11.系统类的方法实现部分替换 - (void) methodExchange { Method m1 = class_getInstanceMethod([NSStringclass],@selec ...

  6. 基于matlab的蓝色车牌定位与识别---识别

    接着昨天的工作,把最后一部分识别讲完. 关于字符识别这块,一种最省事的办法是匹配识别,将所得的字符和自己的标准字符库相减,计算所得结果,值最小的即为识别的结果.不过这种方法是在所得字符较为标准的情况, ...

  7. 【思维题 单调栈】loj#2430. 「POI2014」沙拉餐厅 Salad Bar

    t老师的做法好神…… 题目描述 桌面上有 n 个水果,分别是苹果和橘子.Bytea需要从水果中选择连续的一个区间,并从左到右或从右到左拿水果,且过程中橘子的数量必须始终不小于苹果的数量.求最长的区间大 ...

  8. Golang ioutil读写文件测试

    运用 ioutil.ReadFile .ioutil.WriteFile package main import ( "io/ioutil" "log" &qu ...

  9. 单例模式的几种实现-Java版

    目录 关键点 饿汉式 懒汉式 双检锁 静态内部类单例模式 枚举方式 关键点 私有化构造器 通过静态方法或枚举返回单例类对象 确保单例类对象只有一个,尤其在多线程环境下. 确保每个类被序列化不会重新创建 ...

  10. tab key usage

    QA:gvim编辑ascii文本时由于tabkey的default setting 不合适编写Verilog代码(比如一个tab 代表多少空格) ANS: 1.tab 的自动补齐有两种usage自动补 ...