LightOJ - 1132 Summing up Powers 矩阵高速幂
题目大意:求(1^K + 2^K + 3K + … + N^K) % 2^32
解题思路:
借用别人的图
能够先打表,求出Cnm,用杨辉三角能够高速得到
#include<cstdio>
typedef unsigned long long ll;
const int N = 55;
const ll mod = (1LL << 32);
struct Matrix{
ll mat[N][N];
}A, B, tmp;
ll n, num[N];
ll C[N][N];
int K;
void init2() {
C[0][0] = 1;
for(int i = 1; i <= 50; i++) {
C[i][i] = C[i][0] = 1;
for(int j = 1; j < i; j++)
C[i][j] = (C[i-1][j-1] + C[i-1][j]) % mod;
}
}
Matrix matMul(const Matrix &x, const Matrix &y) {
for(int i = 0; i < K + 2; i++)
for(int j = 0; j < K + 2; j++) {
tmp.mat[i][j] = 0;
for(int k = 0; k < K + 2; k++) {
tmp.mat[i][j] = (tmp.mat[i][j] + x.mat[i][k] * y.mat[k][j]) % mod;
}
}
return tmp;
}
void solve() {
while(n) {
if(n & 1)
B = matMul(B,A);
A = matMul(A,A);
n >>= 1;
}
}
void init() {
for(int i = 0; i < K + 2; i++)
for(int j = 0; j < K + 2; j++) {
B.mat[i][j] = A.mat[i][j] = 0;
if(i == j)
B.mat[i][j] = 1;
}
A.mat[0][0] = 1;
for(int i = 1; i < K + 2; i++)
A.mat[i][0] = A.mat[i][1] = C[K][i-1];
for(int i = 2; i < K + 2; i++)
for(int j = i; j < K + 2; j++) {
A.mat[j][i] = C[K-i+1][j-i];
}
}
int main() {
int test, cas = 1;
scanf("%d", &test);
init2();
while(test--) {
scanf("%lld%d", &n, &K);
printf("Case %d: ", cas++);
ll ans = ( (n % mod) * ( (n + 1) % mod) / 2) % mod;
if(K == 1) {
printf("%lld\n", ans);
continue;
}
init();
n--;
solve();
ans = 0;
for(int i = 0; i < K + 2; i++)
ans = (ans + B.mat[i][0] ) % mod;
printf("%lld\n", ans);
}
return 0;
}
LightOJ - 1132 Summing up Powers 矩阵高速幂的更多相关文章
- LightOJ 1132 Summing up Powers:矩阵快速幂 + 二项式定理
题目链接:http://lightoj.com/volume_showproblem.php?problem=1132 题意: 给定n.k,求(1K + 2K + 3K + ... + NK) % 2 ...
- lightOJ 1132 Summing up Powers(矩阵 二分)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1132 题意:给出n和m.求sum(i^m)%2^32.(1<=i<=n) ...
- LightOJ 1070 Algebraic Problem (推导+矩阵高速幂)
题目链接:problem=1070">LightOJ 1070 Algebraic Problem 题意:已知a+b和ab的值求a^n+b^n.结果模2^64. 思路: 1.找递推式 ...
- UVA 11551 - Experienced Endeavour(矩阵高速幂)
UVA 11551 - Experienced Endeavour 题目链接 题意:给定一列数,每一个数相应一个变换.变换为原先数列一些位置相加起来的和,问r次变换后的序列是多少 思路:矩阵高速幂,要 ...
- UVA10518 - How Many Calls?(矩阵高速幂)
UVA10518 - How Many Calls?(矩阵高速幂) 题目链接 题目大意:给你fibonacci数列怎么求的.然后问你求f(n) = f(n - 1) + f(n - 2)须要多少次调用 ...
- HDU2842-Chinese Rings(递推+矩阵高速幂)
pid=2842">题目链接 题意:求出最少步骤解出九连环. 取出第k个的条件是,k-2个已被取出,k-1个仍在支架上. 思路:想必九连环都玩过吧,事实上最少步骤就是从最后一个环開始. ...
- HDU2276 - Kiki & Little Kiki 2(矩阵高速幂)
pid=2276">题目链接 题意:有n盏灯.编号从1到n.他们绕成一圈,也就是说.1号灯的左边是n号灯.假设在第t秒的时候,某盏灯左边的灯是亮着的,那么就在第t+1秒的时候改变这盏灯 ...
- uva 10655 - Contemplation! Algebra(矩阵高速幂)
题目连接:uva 10655 - Contemplation! Algebra 题目大意:输入非负整数,p.q,n,求an+bn的值,当中a和b满足a+b=p,ab=q,注意a和b不一定是实数. 解题 ...
- hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)
http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...
随机推荐
- python hashlib、configparse、logging
一.hashlib 1.Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 2.摘要算法 通过摘要函数f()对任意长度的数据data计算出固定长度的摘要digest,目 ...
- 安装NexT主题
Hexo 安装主题的方式非常简单,只需要将主题文件拷贝至站点目录的 themes 目录下, 然后修改下配置文件即可. 下载主题包 在终端窗口下,定位到 Hexo 站点目录下.使用 Git checko ...
- GitHub上传项目,使用desktop(客户端)教程
GitHub上传项目,使用desktop(客户端)教程 搜索“GitHub上传项目”,能得到很多相关的文章教程,里面讲的都特别麻烦,要弄什么ssh之类的,可算是吓坏了我,使我非常的怀疑为什么GitH ...
- 使用hbase遇到的问题
1.在使用hbase的时候 有很多问题,其中一个 使用sqoop import 从mysql 向hbase导入数据,报错:Error: java.lang.RuntimeException: Coul ...
- POJ 3695
可以用容斥原理来求.求两个矩形的并的时候可以使用条件 x1=max(p.x1,q.x1);y1=max(p.y1,q.y1);x2=min(p.x2,q.x2);y2=min(p.y2,q.y2); ...
- Android Touch事件传递机制全面解析(从WMS到View树)
转眼间近一年没更新博客了,工作一忙起来.非常难有时间来写博客了,因为如今也在从事Android开发相关的工作,因此以后的博文也会很多其它地专注于这一块. 这篇文章准备从源代码层面为大家带来Touch事 ...
- HDU - 4758 Walk Through Squares (AC自己主动机+DP)
Description On the beaming day of 60th anniversary of NJUST, as a military college which was Secon ...
- python部分
读取骨骼数据相关的多个json,拼接到一起 # -- coding: utf-8 -- import os path = "./test" #文件夹目录 files= os.lis ...
- poj_3071概率dp
确定好对手就简单了. #include<iostream> #include<cstdio> #include<cstring> #include<algor ...
- 简单的floyd——初学
前言: (摘自https://www.cnblogs.com/aininot260/p/9388103.html): 在最短路问题中,如果我们面对的是稠密图(十分稠密的那种,比如说全连接图),计算多 ...