HDU4565 So Easy!
来源:互联网 发布:和邻居做了 知乎 编辑:程序博客网 时间:2024/05/17 07:14
A sequence S n 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 S n.
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 <
Output
For each the case, output an integer S n.
Sample Input
2 3 1 2013
2 3 2 2013
2 2 1 2013
Sample Output
4
14
4
题意就是让你来求:
首先分析一下他给你的一个比较奇怪的范围:
那么这里我们可以想到 a -
我们把它展开后可以得到:
我们知道
到了这一步之后仍旧没法做,但是我们可以发现上面的和其实就是2*(不包含无理数的部分)
那么我们是否能把这个看成一个整体来下手呢?
回到
那么
那么也就是:
那么就可以构造矩阵:
而
剩下的就是矩阵快速幂的知识了。
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;typedef long long LL;const int Size = 2;int A,B,n,mod;struct matrix{ int a[4][4]= {{0}}; matrix operator *(const matrix b)const { matrix ans; for(int i = 0; i < Size; ++i) for(int k = 0; k < Size; ++k) for(int j = 0; j < Size; ++j) { ans.a[i][j] = (ans.a[i][j] + a[i][k]*b.a[k][j])%mod; } return ans; }};int get_ans(){ matrix ans,base; ans.a[0][0] = A; ans.a[0][1] = 1; base.a[0][0] = A; base.a[0][1] = 1; base.a[1][0] = B%mod;//又被这坑了,注意不取模可能就会爆int base.a[1][1] = A; n--; while(n) { if(n & 1)ans = ans*base; base = base*base; n >>= 1; } return 2*ans.a[0][0]%mod;}int main(){ //cout <<INT_MAX<<endl; while(~scanf("%d%d%d%d",&A,&B,&n,&mod)) { printf("%d\n",get_ans()); } return 0;}
- HDU4565 So Easy!
- hdu4565---So Easy!(矩阵)
- HDU4565 So Easy! (矩阵)
- hdu4565 so easy 矩阵
- HDU4565 So Easy!
- HDU4565/2013年长沙赛区So easy
- HDU4565 So Easy!【矩阵连乘】【推导】
- hdu4565 So Easy!(矩阵快速幂)
- HDU4565 So Easy!【矩阵快速幂】
- 20130707 hdu4565 So Easy! 数学头脑+矩阵快速幂
- 2013长沙赛区第一题 HDU4565—So Easy!
- HDU4565 So Easy! 矩阵快速幂外加数学
- HDU4565-So Easy!(共轭运用+矩阵快速幂)
- HDU4565 So Easy!(数学+矩阵快速幂)
- HDU4565-So easy-数学推导化简递推矩阵快速幂
- HDU4565---So Easy!(矩阵快速幂(精度控制))
- hdu4565 So Easy! 广义斐波那契+矩阵快速幂+共轭构造
- So Easy! ——————hdu4565(矩阵快速幂)
- Java学习第十八天
- netty创建udp客户端
- 函数式编程(一) 认识“编程范式”和“函数”(转自:周见智)
- 题目4:统计0~n中有多少个0,1,2,3,4,5,6,7,8,9
- gradle wrapper
- HDU4565 So Easy!
- centos安装mysql
- 2017 暑假艾教集训 day6
- 黑苹果-http://osx.cx
- netty实现tcp客户端
- 百度Echarts-聊城市公交线网
- JavaWeb-JDBC-5-1~4总结
- 约瑟夫环问题
- vc6.0的使用