看完这个之后,感觉数学简直太厉害了 转载自:http://blog.csdn.net/acdreamers/article/details/7851144

今天我们学习如何有效地求表达式的值。对于这个问题,用二分解决比较好。

(1)时,

(2)时,那么有

(3)时,那么有

代码:

 
#include <iostream>
#include <string.h>
#include <stdio.h> using namespace std;
const int M = 1000000007;
typedef long long LL; LL power(LL a,LL b)
{
LL ans = 1;
a %= M;
while(b)
{
if(b & 1)
{
ans = ans * a % M;
b--;
}
b >>= 1;
a = a * a % M;
}
return ans;
} LL sum(LL a,LL n)
{
if(n == 1) return a;
LL t = sum(a,n/2);
if(n & 1)
{
LL cur = power(a,n/2+1);
t = (t + t * cur % M) % M;
t = (t + cur) % M;
}
else
{
LL cur = power(a,n/2);
t = (t + t * cur % M) % M;
}
return t;
} int main()
{
LL a,n;
while(cin>>a>>n)
cout<<sum(a,n)<<endl;
return 0;
}

  

题目:http://poj.org/problem?id=3233

题意:矩阵求和

代码:

#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;
}

等比数列二分求和(logn复杂度)的更多相关文章

  1. HDU1588-Gauss Fibonacci(矩阵高速幂+等比数列二分求和)

    题目链接 题意:g(x) = k * x + b.f(x) 为Fibonacci数列.求f(g(x)),从x = 1到n的数字之和sum.并对m取模. 思路:  设A = |(1, 1),(1, 0) ...

  2. 【POJ1845】Sumdiv(数论/约数和定理/等比数列二分求和)

    题目: POJ1845 分析: 首先用线性筛把\(A\)分解质因数,得到: \[A=p_1^{a_1}*p_2^{a_2}...*p_n^{a_n} (p_i是质数且a_i>0) \] 则显然\ ...

  3. SPOJ AMR10E Stocks Prediction --二分求和+矩阵快速幂

    题意:给一个递推式S(n) = a1*S(n-1)+...+aR*S(n-R),要求S(k)+S(2k)+...+S(nk)的值. 分析:看到n的大小和递推式,容易想到矩阵快速幂.但是如何转化呢? 首 ...

  4. POJ - 3233 Matrix Power Series (矩阵等比二分求和)

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

  5. POJ 3233 Matrix Power Series 矩阵快速幂+二分求和

    矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...

  6. [ACM] POJ 3233 Matrix Power Series (求矩阵A+A^2+A^3...+A^k,二分求和或者矩阵转化)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 15417   Accepted:  ...

  7. POJ 1845 Sumdiv [素数分解 快速幂取模 二分求和等比数列]

    传送门:http://poj.org/problem?id=1845 大致题意: 求A^B的所有约数(即因子)之和,并对其取模 9901再输出. 解题基础: 1) 整数的唯一分解定理: 任意正整数都有 ...

  8. POJ 1845 Sumdiv(因子分解+快速幂+二分求和)

    题意:给你A,B,让求A^B所有的因子和模上9901 思路:A可以拆成素因子的乘积: A = p1^x1 * p2^x2 *...* pn^xn 那么A^B = p1^(B*x1) * p2^(B*x ...

  9. 对O(logN)复杂度的推导

    之前一直对O(logN)这个复杂度如何推导出的存在疑问,这段时间看了一些算法相关的内容,正好看到这个问题,大略研究了一下算是基本解答了我的疑惑:现记录如下 假设有一棵高为H的满二叉树,则它的节点共有N ...

随机推荐

  1. [No000012C]WPF(4/7)类型转换器和标记扩展[译]

    介绍 之前讨论了WPF的基础架构,然后逐步开始学习布局面板,转换,介绍了不同的控件,容器,UI转换等.在这篇文章中,我将讨论每个创建XAML应用前的开发人员应该了解的关于XAML最重要的东西. 标记扩 ...

  2. 安装MAC的ReactNative环境

    brew install node brew install watchman npm config set registry https://registry.npm.taobao.org --gl ...

  3. OPTIMIZE TABLE linked list 表优化原理 链表数据结构 空间再利用

    小结: 1.加快读写: 2.对于InnoDB表,在一定条件下,通过复制旧表重建: 3.实践中, 3.1.show processlist;查看线程,发现,认为堵塞读请求: 3.2.数据长度空间不变,索 ...

  4. cv_list

    cv_list hmz http://cse.sjtu.edu.cn/OS/OS_files/page0012.htm 邹恒明博士: 美国密歇根大学(University of Michigan-An ...

  5. [daily] 内存越界的分析与定位

    valgrind 自不必说 1.  Address Sanitize 很好有,只需要在gcc编译的时候,加上选项 -fsanitize=address 它的工程:https://github.com/ ...

  6. 图->连通性->有向图的强连通分量

    文字描述 有向图强连通分量的定义:在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶点强连通(strongly co ...

  7. hotplug 热拔插机制框架

    框架入口源文件: mdev.c (可根据入口源文件,再按着框架到内核走一遍) 内核版本:linux_2.6.22.6     硬件平台:JZ2440 以下是驱动框架:

  8. 报错解决——SSL: CERTIFICATE_VERIFY_FAILED

    SSL: CERTIFICATE_VERIFY_FAILED Python 升级到 2.7.9 之后引入了一个新特性,当使用urllib.urlopen打开一个 https 链接时,会验证一次 SSL ...

  9. PHP数字字符串左侧补0、字符串填充和自动补齐的几种方法

    一.数字补0. 如果要自动生成学号,自动生成某某编号,就像这样的形式“d0000009”.“d0000027”时,那么就会面临一个问题,怎么把左边用0补齐成这样8位数的编码呢?我想到了两种方法实现这个 ...

  10. sap 类的左侧导航栏