2013年湖南大学长沙邀请赛第一题

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
#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;
}
posted @ 2013-06-22 23:54  forevermemory  阅读(176)  评论(0编辑  收藏  举报