hdoj 4565 So Easy!(构造无理数共轭,矩阵快速幂)

博客详细讨论了如何解决hdoj 4565问题,重点在于理解无理数共轭的概念,并应用矩阵快速幂进行求解。文章提醒读者在处理负数取模时的特殊策略,即先加mod再取模。提供了相关题解链接及代码实现。
摘要由CSDN通过智能技术生成

最难的是要想到无理数共轭...

参考题解:点击打开链接

还要注意注意负数的取模,可以先+mod再进行取模.


代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a, b, m;
struct node
{
    ll s[2][2];
    node() {}
    node(ll a, ll b, ll c, ll d)
    {
        s[0][0] = a;
        s[0][1] = b;
        s[1][0] = c;
        s[1][1] = d;
    }
};

node mul(node a, node b)
{
    node t = node(0, 0, 0, 0);
    for(int i = 0; i < 2; i++)
        for(int j = 0; j < 2; j++)
            for(int k = 0; k < 2; k++)
                t.s[i][j] = (t.s[i][j]+a.s[i][k]*b.s[k][j]+m)%m;    //注意负数
    return t;
}

node mt_pow(node p, int n)
{
    node q = node(1, 0, 0, 1);
    while(n)
    {
        if(n&1) q = mul(p, q);
        p = mul(p, p);
        n /= 2;
    }
    return q;
}

int main(void)
{
    ll n;
    while(cin >> a >> b >> n >> m)
    {
        node base = node(2*a, (b-a*a), 1, 0);
        ll c1 = 2*a%m;
        ll c2 = 2*(a*a+b)%m;
        if(n == 1) printf("%lld\n", c1);
        else if(n == 2) printf("%lld\n", c2);
        else
        {
            node ans = mt_pow(base, n-2);
            printf("%lld\n", (ans.s[0][0]*c2+ans.s[0][1]*c1+m)%m);
        }
    }
    return 0;
}


So Easy!

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


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
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值