So Easy! hdu4565

So Easy!(点击即可跳转)

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


 

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
 
 
大致题意:已知a,b, 求

以下是ac代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;

typedef long long ll;

struct matrix{
    ll x[20][20];
    ll n, m;
    matrix(){memset(x, 0, sizeof(x));}
};

ll m;
matrix multiply(matrix &mata, matrix &matb)
{
    matrix c;
    c.n = mata.n;
    c.m = matb.m;
    for(int i = 1; i <= mata.n; i++){
        for(int j = 1; j <= mata.m; j++){
            c.x[i][j] = 0;
            for(int k = 1; k <= matb.m; k++){
                c.x[i][j] += mata.x[i][k] * matb.x[k][j];
                c.x[i][j] %= m;
            }
        }
    }
    return c;
}

ll a, matb;

matrix mpow(matrix &origin, ll n)
{
    matrix res;
    res = origin;
    matrix change;
    change.n = change.m = 2;
    change.x[1][1] = 2 * a;
    change.x[1][2] = 1;
    change.x[2][1] = matb - a * a;
    change.x[2][2] = 0;

    while(n > 0){
        if(n & 1)res = multiply(res, change);
        change = multiply(change, change);
        n >>= 1;
    }
    return res;
}

int main()
{
    ll n;
    while(cin>>a>>matb>>n>>m){
        if(n == 1){
            printf("%lld\n", 2 * a);
            continue;
        }else if(n == 2){
            ll ans = (ll)(ceil((a +sqrt(matb)) * (a +sqrt(matb)))) % m;
            printf("%lld\n", ans);
            continue;
        }

        matrix mat;
        mat.n = 1;
        mat.m = 2;
        ll ans = (ll)(ceil((a +sqrt(matb)) * (a +sqrt(matb)))) % m;
        mat.x[1][1] = ans;

        mat.x[1][2] = 2 * a;
        mat = mpow(mat, n - 1);
        printf("%lld\n", (mat.x[1][2] + m) % m);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值