HDU6395 多校赛Sequence

传送门----http://acm.hdu.edu.cn/showproblem.php?pid=6395

思路:由于有后半部分的下取整,可以联想到整除分块去做。再用矩阵快速幂优化递推即可。

Code:

//2
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define LL long long
using namespace std;
const LL mod = 1000000007;
LL a, b, c, d, p, n;
struct martix
{
    LL m[4][4];
    martix()
    {
        memset(m, 0, sizeof(m));
    }
    martix operator*(const martix b)const
    {
        LL i, j, k;
        martix ans;
        for(i = 1; i <= 3; i++)
            for(j = 1; j <= 3; j++)
            {
                ans.m[i][j] = 0;
                for(k = 1; k <= 3; k++)
                {
                    ans.m[i][j] += m[i][k] * b.m[k][j];
                    ans.m[i][j] %= mod;
                }
            }
        return ans;
    }
};
void Debug (martix a)
{
    for(int i = 1; i <= 3; i++)
    {
        for(int j = 1; j <= 3; j++)
            cout << a.m[i][j] << ' ';
        cout << endl;
    }
}
martix martix_pow(LL n,LL x)
{
    martix temp, res;
    temp.m[1][1] = d;
    temp.m[1][2] = c;
    temp.m[1][3] = x;
    temp.m[2][1] = 1;
    temp.m[3][3] = 1;
    res.m[1][1] = res.m[2][2] = res.m[3][3] = 1;
    while(n)
    {
        if(n & 1)
            res = res * temp;
        temp = temp * temp;
        n >>= 1;
    }
    return res;
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%lld%lld%lld%lld%lld%lld", &a, &b, &c, &d, &p, &n);
        if(n == 1)
            printf("%lld\n", a);
        else if(n == 2)
            printf("%lld\n", b);
        else
        {
            martix A, B;
            LL l, r;
            A.m[1][1] = A.m[2][2] = A.m[3][3] = 1;
            for(l = 3; l <= n; ++l)
            {
                r = (l <= p) ? min(p / (p / l), n) : n;
                A = (martix_pow(r - l + 1,p/l))*A;
                l=r;
               // Debug(A);
            }
            printf("%lld\n",(A.m[1][1]*b%mod+A.m[1][2]*a%mod+A.m[1][3])%mod);
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值