hdu 4565 So Easy!
来源:互联网 发布:孤独大脑 喻颖正 知乎 编辑:程序博客网 时间:2024/06/10 00:46
原题目
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 < 2 15, (a-1) 2< b < a 2, 0 < b, n < 2 31.The input will finish with the end of file.
Output
For each the case, output an integer S n.
解释:给出一个公式:
要求小数点后一但有数往上进位(只保留整数位上的数);求出Sn
题解:简单的矩阵快速幂,可以直接套模板过;
本题主要事考察能否在短时间内观察出题目所给的公式的规律:通过找共轭来规避小数点后的计算;
建立矩阵 e :{2*a | -(a^2-b)}
:{1 | 0 }
矩阵 c {2*a}
{1}
本题要求的便是e^(n-1)*矩阵 c的结果;
给出代码
#include <stdio.h>#include <math.h>#include <stdlib.h>#include <time.h>#include <string.h>#define maxn 100007using namespace std;typedef unsigned long long ull;typedef long long ll;ll a,b,n,m;struct mat{ ll val[5][5];};mat matmult(mat a,mat b){ mat ms; memset(ms.val,0,sizeof(ms.val)); for(int i=1;i<=2;i++) { for(int j=1;j<=2;j++) { for(int k=1;k<=2;k++) { ms.val[i][j]+=a.val[i][k]*b.val[k][j]; ms.val[i][j]%=m; } } } return ms;}mat matpow(mat a,ll k){ mat ans; memset(ans.val,0,sizeof(ans.val)); for(int i=1;i<=2;i++) ans.val[i][i]=1; while(k) { if(k&1) ans=matmult(a,ans); a=matmult(a,a); k>>=1; } return ans;}int main(){ while(scanf("%lld%lld%lld%lld",&a,&b,&n,&m)==4) { mat c; mat e; e.val[1][1]=2*a%m; e.val[1][2]=-(a*a-b)%m; e.val[2][1]=1; e.val[2][2]=0; if(n==1) { printf("%lld\n",2*a%m); continue; } e=matpow(e,n-1); printf("%lld\n",((e.val[1][1]*2*a+e.val[1][2]*2)%m+m)%m); } return 0;}
- HDU 4565 So Easy!
- hdu 4565 So Easy!
- HDU 4565 So Easy!
- hdu 4565 So Easy!
- HDU 4565 So Easy!
- hdu 4565 So Easy!
- HDU 4565 So Easy!
- hdu 4565 So Easy!
- hdu 4565So Easy!
- hdu 4565 So Easy!
- HDU 4565 So Easy!
- HDU 4565 So Easy!
- hdu 4565 So Easy!
- HDU 4565 SO easy!
- HDU 4565 So Easy!
- hdu 4565 So Easy!
- HDU 4565 So Easy!
- HDU 4565 So Easy!
- 三种人工智能开源框架
- 程序的灵魂----算法(一)
- NYOJ 257 郁闷的C小加(一)
- 高级数据结构 | 线段树的入门与入门
- Java8之Stream
- hdu 4565 So Easy!
- [bzoj4916] 神犇和蒟蒻
- 进程控制1——关于进程控制的一些基础知识
- POJ 2236 Wireless Network(并查集基础)
- 为什么程序员的价值总是被严重的低估?
- 20170809
- ios二维码条码扫描问题
- C# 输入用空格隔开的数字 比较其大小
- 笛卡尔积与多表查询