二分求和+矩阵快速幂二分求和

首先贴一个二分求和。

即求A^1+A^2+A^3+A^4+...+A^N。

参考:https://www.cnblogs.com/stranger-/p/8999382.html

 

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int maxn = 1e6+50;
const int inf = 0x3f3f3f3f,mod = 1000000007;
const double epx = 1e-10;
typedef long long ll;
ll pows(ll n,ll m)
{
    ll ans = 1;
    while(m > 0)
    {
        if(m & 1)ans = (ans * n) % mod;
        m = m >> 1;
        n = (n * n) % mod;
    }
    return ans;
}
ll sum(ll a,ll n)
{
    if(n==1) return a;
    ll t=sum(a,n/2);//递归求解s (n/2)
    if(n&1)
    {
        ll cur=pows(a,n/2+1)%mod;
        t=(t+(t*cur)%mod)%mod;
        t=(t+cur)%mod;
    }
    else
    {
        ll cur=pows(a,n/2)%mod;
        t=(t+(t*cur)%mod)%mod;
    }
    return t;
}
int main()
{
    ll a,n;
    cin>>a>>n;
    cout<<sum(a,n)<<endl;
}

然后再看矩阵快速幂+二分求和。

Matrix Power Series

Time Limit: 3000MS Memory Limit: 131072K
Total Submissions: 31320 Accepted: 12636

Description

Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak.

Input

The input contains exactly one test case. The first line of input contains three positive integers n (n ≤ 30), k (k ≤ 109) and m (m < 104). Then follow n lines each containing n nonnegative integers below 32,768, giving A’s elements in row-major order.

Output

Output the elements of S modulo m in the same way as A is given.

Sample Input

2 2 4
0 1
1 1

Sample Output

1 2
2 3

Source

POJ Monthly--2007.06.03, Huang, Jinsong

#include <iostream>
#include <string.h>
#include <stdio.h>
 
using namespace std;
const int N = 35;
 
struct Matrix
{
    int m[N][N];
};
 
Matrix I;
int n,k,M;
 
Matrix add(Matrix a,Matrix b)
{
    Matrix c;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            c.m[i][j] = a.m[i][j] + b.m[i][j];
            c.m[i][j] %= M;
        }
    }
    return c;
}
 
Matrix multi(Matrix a,Matrix b)
{
    Matrix c;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            c.m[i][j] = 0;
            for(int k=0; k<n; k++)
                c.m[i][j] += a.m[i][k] * b.m[k][j];
            c.m[i][j] %= M;
        }
    }
    return c;
}
 
Matrix power(Matrix A,int n)
{
    Matrix ans = I,p = A;
    while(n)
    {
        if(n & 1)
        {
            ans = multi(ans,p);
            n--;
        }
        n >>= 1;
        p = multi(p,p);
    }
    return ans;
}
 
Matrix sum(Matrix A,int k)
{
    if(k == 1) return A;
    Matrix t = sum(A,k/2);
    if(k & 1)
    {
        Matrix cur = power(A,k/2+1);
        t = add(t,multi(t,cur));
        t = add(t,cur);
    }
    else
    {
        Matrix cur = power(A,k/2);
        t = add(t,multi(t,cur));
    }
    return t;
}
 
int main()
{
    while(scanf("%d%d%d",&n,&k,&M)!=EOF)
    {
        Matrix A;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                scanf("%d",&A.m[i][j]);
                A.m[i][j] %= M;
                I.m[i][j] = (i==j);
            }
        }
        Matrix ans = sum(A,k);
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
                printf("%d ",ans.m[i][j]);
            puts("");
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值