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!
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 ",(one.pos[1][0]%mod+mod)%mod); } return 0; }