2013年湖南大学长沙邀请赛第一题
Problem Description
A sequence Sn is defined as:![](https://www.cnblogs.com/data/images/C468-1001-1.jpg)
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!![](https://www.cnblogs.com/data/images/C468-1001-2.jpg)
![](https://www.cnblogs.com/data/images/C468-1001-1.jpg)
![](https://www.cnblogs.com/data/images/C468-1001-2.jpg)
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
#include<stdio.h>
#include<iostream>
using namespace std;
#define ll __int64
ll lmx(ll a,ll b,ll n,ll k )
{
ll t1=a,t2=1,temp1=1,temp2=0,x,y,temp=n;
while(temp)
{
if(temp&1)
{
y=temp1;
temp1=(temp1*t1+t2*temp2*b)%k;
temp2=(y*t2+temp2*t1)%k;
}
temp>>=1;
x=t1;
t1=(t1*t1+t2*t2*b)%k;
t2=(2*t2*x)%k;
}
return (temp1*2)%k;
}
int main()
{
ll a,b,c,k;
while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k)!=EOF)
{
printf("%I64d\n",lmx(a,b,c,k));
}
return 0;
}
#include<iostream>
using namespace std;
#define ll __int64
ll lmx(ll a,ll b,ll n,ll k )
{
ll t1=a,t2=1,temp1=1,temp2=0,x,y,temp=n;
while(temp)
{
if(temp&1)
{
y=temp1;
temp1=(temp1*t1+t2*temp2*b)%k;
temp2=(y*t2+temp2*t1)%k;
}
temp>>=1;
x=t1;
t1=(t1*t1+t2*t2*b)%k;
t2=(2*t2*x)%k;
}
return (temp1*2)%k;
}
int main()
{
ll a,b,c,k;
while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k)!=EOF)
{
printf("%I64d\n",lmx(a,b,c,k));
}
return 0;
}
为了明天所以选择坚定的执着今天。