快速幂

思想
快速幂

  1. 数的快速幂
  2. 矩阵的快速幂

    1.数的快速幂

上模板

LL quickpow(LL a,int b,LL m)
{
    LL ans = 1;
    if(a == 0) return 0;
    while(b)
    {
        if(b&1)ans = (ans * a)%m;
        b = (b >> 1);
        a = (a*a)%m;
    }
    return ans;
}

撸袖子写题:
poj 1995

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define LL long long

const int maxn = 45005;
LL data[maxn][2];
LL quickpow(LL a,int b,LL m)
{
    LL ans = 1;
    if(a == 0) return 0;
    while(b)
    {
        if(b&1)ans = (ans * a)%m;
        b = (b >> 1);
        a = (a*a)%m;
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&m,&n);
        LL ans = 0;
        for(int i = 0; i < n ; i++)
        {
            LL a;   int b;
            scanf("%I64d%d",&a,&b);
            LL now = quickpow(a,b,m);
            ans = (ans + now)%m;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

cf185A

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
#define LL long long
const int mod = 1000000000+7;
int solve(LL n)
{
    LL ans = 1;
    LL a = 2;
    while(n)
    {
        if(n&1) ans = (ans*a)%mod;
        n = (n>>1);
        a = (a*a)%mod;
    }
    return ans;
}

int main()
{
    LL n;
    while(~scanf("%I64d",&n))
    {
        if(n == 0) {printf("1\n");continue;}
        LL ans = (solve(2*n-1) + solve(n-1))%mod;
        printf("%I64d\n",ans);
    }
    return 0;
}

2.矩阵快速幂
和数的快速幂类似。

撸袖子继续写题:

经典快速幂:
poj3070 求Fibonacci数列。

#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;
const int mod = 10000;

struct Matrix
{
    int data[3][3];
    Matrix()
    {
        for(int i = 0; i < 2 ; i++)
        for(int j = 0; j < 2 ; j++)
        data[i][j] = 0;
    }
};
Matrix multi(Matrix a , Matrix b)
{
    Matrix c;
    for(int i = 0; i < 2 ; i++)
    for(int j = 0; j < 2 ; j++)
    for(int k = 0; k < 2 ; k++)
        c.data[i][j] = (c.data[i][j] +(a.data[i][k])%mod*(b.data[k][j])%mod)%mod;
    return c;
}
int quickpow(int b)
{
    Matrix ans,a;
    ans.data[0][0] = 1,ans.data[0][1] = 0,ans.data[1][0] = 0,ans.data[1][1] = 1;
    a.data[0][0] = 1,a.data[0][1] = 1,a.data[1][0] = 1,a.data[1][1] = 0;
    if(b == 0) return 0;
    else
    {
        while(b)
        {
            if(b&1) ans = multi(ans,a);
            a = multi(a,a);
            b = (b>>1);
        }
    }
    return ans.data[0][1];
}


int main()
{
    int n;
    while(~scanf("%d",&n) && ~n)
        printf("%d\n",quickpow(n));

    return 0;
}

hdu1757
这个的递推和上面那个几乎一毛一样,只不过这个是开到了10的矩阵。

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int k,mod;
int a[10];
struct node
{
    int m[10][10];
    node()
    {
        for(int i = 0; i < 10; i++)
        for(int j = 0; j < 10; j++)
            m[i][j] = 0;
    }
};
node operator*(node mat1,node mat2)
{
    node temp;
    for(int i = 0; i < 10; i++)
    for(int j = 0; j < 10; j++)
    for(int k = 0; k < 10; k++)
        temp.m[i][j] = (temp.m[i][j] + mat1.m[i][k]*mat2.m[k][j])%mod;
    return temp;
}
int quick()
{
    node ans;
    for(int i = 0; i < 10; i++) ans.m[i][i] = 1;
    node ori;
    for(int i = 0; i < 10; i++) {ori.m[0][i] = a[i];if(i > 0)ori.m[i][i-1] = 1;}
    if(k < 10)return a[k];
    else
    {
        k = k-9;
        while(k)
        {
            if(k&1) ans = ans*ori;
            k = (k>>1);
            ori = ori*ori;
        }
    }
    node temp;
    for(int i = 0; i < 10; i++) temp.m[i][0] = 9-i;
    temp = ans*temp;
    printf("%d\n",temp.m[0][0]);
    return 0;
}

int main()
{
    while(~scanf("%d%d",&k,&mod))
    {
        for(int i = 0; i < 10 ; i++)
            scanf("%d",&a[i]);
            quick();
    }
    return 0;
}

hdu2842
求n连环的解法最少几步。

醉了,根本不知道n连环长什么样。。
只能查出来。。
递推式:F(n) = F(n-1) + 2* F(n-2)+1;
所以就可以构造乘的那个矩阵。

110200101 
#include<cstdio>
#include <cstring>
#include <iostream>

using namespace std;
#define LL long long
const int mod = 200907;
struct node
{
    int m[3][3];
    node()
    {
        for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
            m[i][j] = 0;
    }
};
node operator*(node mat1,node mat2)
{
    node temp;
    for(int i = 0; i < 3; i++)
    for(int j = 0; j < 3; j++)
    for(int k = 0; k < 3; k++)
        temp.m[i][j] = (temp.m[i][j] + ((long long)mat1.m[i][k]*mat2.m[k][j])%mod)%mod;
    return temp;
}
int quick(int n)
{
    node ans,a;
    if(n <= 2)  { printf("%d\n",n); return 0; }
    for(int i = 0; i < 3; i++) {ans.m[i][i] = 1;}
    a.m[0][0] = 1;a.m[0][1] = 2,a.m[0][2] = 1;
    a.m[1][0] = 1; a.m[2][2] = 1;
    n = n-2;
    while(n)
    {
        if(n&1) ans = ans*a;
        n = (n>>1);
        a = a*a;
    }
    LL res = 0;
    int x[3] = {2,1,1};
    for(int i = 0; i < 3; i++)
        res = (res + ((long long)ans.m[0][i]*x[i])%mod)%mod;
    printf("%I64d\n",res);
    return 0;
}
int main()
{
    int n;
    while(~scanf("%d",&n) && n)
    {
        quick(n);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值