HDU 4565 So Easy!

So Easy!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 803    Accepted Submission(s): 219


Problem Description
  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.
 

Sample Input
  
  
2 3 1 2013 2 3 2 2013 2 2 1 2013
 

Sample Output
  
  
4 14 4
 

Source
 

Recommend
zhoujiaqi2010
     第一次做与矩阵的题目,将题目化简后就是求矩阵的快速幂
#include <iostream>
#include <cstring>
#include <cstdio>
#define N 3
using namespace std;
__int64 rec1[N][N],rec2[N][N],temp[N][N];
__int64 a,b,n,mod;
int main()
{
    //freopen("data.in","r",stdin);
    void get(int k);
    while(scanf("%I64d %I64d %I64d %I64d",&a,&b,&n,&mod)!=EOF)
    {
        if(n==1)
        {
            printf("%I64d\n",2*a%mod);
            continue;
        }
        rec1[1][1] = 2*a; rec1[1][2] =(b-a*a);
        rec1[2][1] = 1; rec1[2][2] = 0;
        get(n-1);
        printf("%I64d\n",((rec2[1][1]*2*a)%mod+(rec2[1][2]*2)%mod)%mod);
    }
    return 0;
}
void get_rec(__int64 (*p1)[3],__int64 (*p2)[3])
{
    memset(temp,0,sizeof(temp));
    for(int i=1;i<=2;i++)
    {
        for(int j=1;j<=2;j++)
        {
            for(int x=1;x<=2;x++)
            {
                temp[i][j] += (p1[i][x]%mod*p2[x][j]%mod);
                temp[i][j] = (temp[i][j])%mod;
            }
        }
    }
    for(int i=1;i<=2;i++)
    {
        for(int j=1;j<=2;j++)
        {
            p2[i][j] = (temp[i][j]+mod)%mod;
        }
    }
}
void get(int k)
{
    rec2[1][1] =1; rec2[1][2] = 0;
    rec2[2][1] =0; rec2[2][2] = 1;
    while(k>1)
    {
        if(k&1)
        {
            get_rec(rec1,rec2);
        }
        get_rec(rec1,rec1);
        k = k/2;
    }
    get_rec(rec1,rec2);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值