【2018 Multi-University Training Contest 7】Sequence(分块+矩阵快速幂)

本文介绍了一种解决特定序列计算问题的高效算法。通过矩阵快速幂和分段计算的方法,实现了对序列Fn的求解,并考虑了大数运算中的模块化处理。适用于序列计算中的边界条件变化和大规模数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
Let us define a sequence as below

F1F2Fn===ABCFn2+DFn1+Pn{F1=AF2=BFn=C⋅Fn−2+D⋅Fn−1+⌊Pn⌋

Your job is simple, for each task, you should output Fn module109+7.109+7.

Input
The first line has only one integer T, indicates the number of tasks.

Then, for the next T lines, each line consists of 6 integers, A , B, C, D, P, n.

1T200A,B,C,D1091P,n1091≤T≤200≤A,B,C,D≤1091≤P,n≤109

Sample Input
2
3 3 2 1 3 5
3 2 2 2 1 4

Sample Output
36
24

Source
2018 Multi-University Training Contest 7
代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#define ll long long
#define mod 1000000007
using namespace std;

struct M
{
    ll m[3][3];
    M mul(M o)
    {
        M t;
        for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
        {
            ll temp=0;
            for(int k=0;k<3;k++)
                temp=(temp+m[i][k]*o.m[k][j]%mod)%mod;
            t.m[i][j]=temp;
        }
        return t;
    }
    void zero()
    {
        for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
        m[i][j]=0;
    }
    void unit()
    {
        zero();
        for(int i=0;i<3;i++)m[i][i]=1;
    }
};
M get(M a,int b)
{
    M ans;
    ans.unit();
    while(b)
    {
        if(b&1)ans=ans.mul(a);
        b>>=1;
        a=a.mul(a);
    }
    return ans;
}
int A,B,C,D,P,n;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        scanf("%d%d%d%d%d%d",&A,&B,&C,&D,&P,&n);
        A%=mod;
        B%=mod;
        if(n==1)cout<<A<<endl;
        else if(n==2)cout<<B<<endl;
        else
        {
            M _a;
            _a.zero();
            _a.m[0][0]=D;_a.m[0][1]=C;_a.m[0][2]=1;
            _a.m[1][0]=1;_a.m[2][2]=1;
            for(ll i=3,last;i<=n;i=last+1)
            {
                ll val=P/i;
                if(val==0)
                {
                    int b=n-i+1;
                    M a=_a;
                    M __a=get(a,b);
                    B=(__a.m[0][0]*B%mod+__a.m[0][1]*A%mod)%mod;
                    //cout<<B<<endl;
                    break;
                }
                last=P/val;
                last=min((int)last,n);
                int b=last-i+1;
                M a=_a;
                M __a=get(a,b);
                ll _B=B;
                B=(__a.m[0][0]*B%mod+__a.m[0][1]*A%mod+__a.m[0][2]*val%mod)%mod;
                __a=get(a,b-1);
                A=(__a.m[0][0]*_B%mod+__a.m[0][1]*A%mod+__a.m[0][2]*(P/(last-1))%mod)%mod;
            }
            printf("%d\n",B);
        }
    }
    return 0;
}
用户表: Users +-------------+---------+ | Column Name | Type | +-------------+---------+ | user_id | int | | user_name | varchar | +-------------+---------+ user_id 是该表的主键(具有唯一值的列)。 该表中的每行包括用户 ID 和用户名。 注册表: Register +-------------+---------+ | Column Name | Type | +-------------+---------+ | contest_id | int | | user_id | int | +-------------+---------+ (contest_id, user_id) 是该表的主键(具有唯一值的列的组合)。 该表中的每行包含用户的 ID 和他们注册的赛事。 编写解决方案统计出各赛事的用户注册百分率,保留两位小数。 返回的结果表按 percentage 的 降序 排序,若相同则按 contest_id 的 升序 排序。 返回结果如下示例所示。 示例 1: 输入: Users 表: +---------+-----------+ | user_id | user_name | +---------+-----------+ | 6 | Alice | | 2 | Bob | | 7 | Alex | +---------+-----------+ Register 表: +------------+---------+ | contest_id | user_id | +------------+---------+ | 215 | 6 | | 209 | 2 | | 208 | 2 | | 210 | 6 | | 208 | 6 | | 209 | 7 | | 209 | 6 | | 215 | 7 | | 208 | 7 | | 210 | 2 | | 207 | 2 | | 210 | 7 | +------------+---------+ 输出: +------------+------------+ | contest_id | percentage | +------------+------------+ | 208 | 100.0 | | 209 | 100.0 | | 210 | 100.0 | | 215 | 66.67 | | 207 | 33.33 | +------------+------------+ 解释: 所有用户都注册了 208、209 和 210 赛事,因此这些赛事的注册率为 100% ,我们按 contest_id 的降序排序加入结果表中。 Alice 和 Alex 注册了 215 赛事,注册率为 ((2/3) * 100) = 66.67% Bob 注册了 207 赛事,注册率为 ((1/3) * 100) = 33.33%
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值