17.7.3小结、【矩阵快速幂】2017武大校赛I题:A simple math problem即nyoj2333、 bzoj2326

17.7.3暑假集训小结:

今天早上踩着8点进来了,昨天刚从家里到学校,先收拾了一下电脑到九点半;

从今天开始的计划是上学期考试的时候没刷的题目刷一下,到7.5;


一上午也没有做出这道题目,一直在学习有关这道题目的博客,慢慢的也就理解了;

也知道是个矩阵快速幂,但是切入点不对,说白了不就是需要知道定义的n值然后取余11就好了!

所以就用矩阵快速幂边求得定义的n值边取余啦~;然后下午給A了,下午学习的是容斥定理;


2333: A simple math problem

时间限制: 1 Sec   内存限制: 512 MB
提交: 38   解决: 14
[ 提交][ 状态][ 讨论版]

题目描述

Given a number n, you should calculate 123456 . . . 11121314 . . . n module 11.

输入

A single line with an integer n (0 < n ≤ 10e18)  单组数据测试.

输出

Output one integer, 123456 . . . 11121314 . . . n module 11 

样例输入

1
20
21

样例输出

1
5
4

提示

1 ≡ 1( mod 11) 

1234567891011121314151617181920 ≡ 5( mod 11) 

123456789101112131415161718192021 ≡ 4( mod 11)

来源

[ 提交][ 状态]

思路:要使用矩阵快速幂,首先就要推出递推式,然后构造矩阵-矩阵乘法-矩阵快速幂-主函数;递推式:f(n)=f(n-1)*10^k+n;
可以构造出矩阵:
f(n-1)   0    0          10^k   1   1
n-1       0    0            0     1   1
1          0    0            0     0    1
---------f(n-1)=0、n-1=0----------
本道题的难点上述的构造矩阵是一个,另外一个就是矩阵中的 10^k !!!
1-9                 9               10^1
10-99             90             10^2
100-999         900           10^3
1000-9999     9000         10^4
...........           ......            .....

每一个10的倍数就要对应的矩阵(10^k不同)进行矩阵乘法;
下面的代码处理这个问题十分巧妙,通过来分n的范围进行矩阵乘法;

代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define N 3
typedef long long LL;

LL n,m=11;
struct Matrix
{
    LL mat[N][N];
    Matrix()
    {
        memset(mat,0,sizeof(mat));
    }
};

Matrix mul(Matrix a,Matrix b)
{
    Matrix res;
    for(int i=0; i<N; i++)
        for(int j=0; j<N; j++)
        {
            for(int k=0; k<N; k++)
            {
                res.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
                res.mat[i][j]%=m;
            }
        }
    return res;
}

Matrix pow_matrix(LL n,LL t)
{
    Matrix res;
    for(int i=0; i<N; i++)
        res.mat[i][i]=1;
    Matrix b; //每次矩阵快速幂前对矩阵进行改变,根据数据范围最多快速幂18次;
    b.mat[0][0]=n%m;
    b.mat[0][1]=b.mat[0][2]=b.mat[1][1]=b.mat[1][2]=b.mat[2][2]=1;

    LL y=t+1-n/10;
    while(y)
    {
        if(y&1)
            res=mul(res,b);
        y>>=1;
        b=mul(b,b);
    }
    return res;
}  //模板上的输入变量是:矩阵、数据长度;灵活运用模板,依题目而变(说着轻松)

int main()
{
    while(~scanf("%lld",&n))
    {
        Matrix ans;

        ans.mat[2][0]=1;
        int x[9]= {0,1,1,2,2,3,3,3,4};
        if(n<9)
        {
            printf("%d\n",x[n]);
            continue;
        }

        LL t=10;
        for(int i=1; i<=18; i++)
        {
            ans=mul(pow_matrix(t,t-1),ans);
            t*=10;

            if(t>n)  break;
        }
        ans=mul(pow_matrix(t,n),ans);
        printf("%lld\n",ans.mat[0][0]);
    }
    return 0;
}

是的,这道题目也可以用暴力规律来找,总共18个规律;
题解思路:搁位和相减;

如果不是取余11呢,取余一个变量呢:

2326: [HNOI2011]数学作业

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 2180   Solved: 1269
[ Submit][ Status][ Discuss]

Description

代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define N 3
typedef long long LL;

LL n,m;
struct Matrix
{
    LL mat[N][N];
    Matrix()
    {
        memset(mat,0,sizeof(mat));
    }
};

Matrix mul(Matrix a,Matrix b)
{
    Matrix res;
    for(int i=0; i<N; i++)
        for(int j=0; j<N; j++)
        {
            for(int k=0; k<N; k++)
            {
                res.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
                res.mat[i][j]%=m;
            }
        }
    return res;
}

Matrix pow_matrix(LL n,LL t)
{
    Matrix res;
    for(int i=0; i<N; i++)
        res.mat[i][i]=1;
    Matrix b;
    b.mat[0][0]=n%m;
    b.mat[0][1]=b.mat[0][2]=b.mat[1][1]=b.mat[1][2]=b.mat[2][2]=1;

    LL y=t+1-n/10;
    while(y)
    {
        if(y&1)
            res=mul(res,b);
        y>>=1;
        b=mul(b,b);
    }
    return res;
}

int main()
{
    while(~scanf("%lld",&n))
    {
        Matrix ans;
//        for(int i=0; i<N; i++)
//            ans.mat[i][i]=1;
        ans.mat[2][0]=1;
//        int x[9]= {0,1,1,2,2,3,3,3,4};
//        if(n<9)
//        {
//            printf("%d\n",x[n]);
//            continue;
//        }
    scanf("%lld",&m);
        LL t=10;
        for(int i=1; i<=18; i++)
        {
            ans=mul(pow_matrix(t,t-1),ans);
            t*=10;

            if(t>n)  break;
        }
        ans=mul(pow_matrix(t,n),ans);
        printf("%lld\n",ans.mat[0][0]);
    }
    return 0;
}




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值