题目大意:求(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 矩阵高速幂的更多相关文章

  1. LightOJ 1132 Summing up Powers:矩阵快速幂 + 二项式定理

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1132 题意: 给定n.k,求(1K + 2K + 3K + ... + NK) % 2 ...

  2. lightOJ 1132 Summing up Powers(矩阵 二分)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1132 题意:给出n和m.求sum(i^m)%2^32.(1<=i<=n) ...

  3. LightOJ 1070 Algebraic Problem (推导+矩阵高速幂)

    题目链接:problem=1070">LightOJ 1070 Algebraic Problem 题意:已知a+b和ab的值求a^n+b^n.结果模2^64. 思路: 1.找递推式 ...

  4. UVA 11551 - Experienced Endeavour(矩阵高速幂)

    UVA 11551 - Experienced Endeavour 题目链接 题意:给定一列数,每一个数相应一个变换.变换为原先数列一些位置相加起来的和,问r次变换后的序列是多少 思路:矩阵高速幂,要 ...

  5. UVA10518 - How Many Calls?(矩阵高速幂)

    UVA10518 - How Many Calls?(矩阵高速幂) 题目链接 题目大意:给你fibonacci数列怎么求的.然后问你求f(n) = f(n - 1) + f(n - 2)须要多少次调用 ...

  6. HDU2842-Chinese Rings(递推+矩阵高速幂)

    pid=2842">题目链接 题意:求出最少步骤解出九连环. 取出第k个的条件是,k-2个已被取出,k-1个仍在支架上. 思路:想必九连环都玩过吧,事实上最少步骤就是从最后一个环開始. ...

  7. HDU2276 - Kiki &amp; Little Kiki 2(矩阵高速幂)

    pid=2276">题目链接 题意:有n盏灯.编号从1到n.他们绕成一圈,也就是说.1号灯的左边是n号灯.假设在第t秒的时候,某盏灯左边的灯是亮着的,那么就在第t+1秒的时候改变这盏灯 ...

  8. uva 10655 - Contemplation! Algebra(矩阵高速幂)

    题目连接:uva 10655 - Contemplation! Algebra 题目大意:输入非负整数,p.q,n,求an+bn的值,当中a和b满足a+b=p,ab=q,注意a和b不一定是实数. 解题 ...

  9. hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)

    http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...

随机推荐

  1. CSS3属性之text-overflow:ellipsis,指定多行文本中任意一行显示...

    对于text-overflow:ellipsis,文本超出部分显示...,但要实现这个效果,却有一些必备条件,如下: div{ overflow:hidden; white-space:nowrap; ...

  2. 极路由4pro(HC5962)设置阿里云DDNS

    v2ex有个帖子说用Dnspod的API可以一行搞定,不过我既然买的是阿里云的域名还是想尽量用阿里云的API,感觉比较安全,另外修改解析记录后也会自动发邮件通知,所以还是调用阿里云的API吧.阿里云的 ...

  3. HDU 1023

    卡特兰数.把进栈看成是+1,出栈看成是-1,任何时候部分和都有a1+a2+....ak>=0.求这样的数列的个数.这明显是卡特兰数的一个解释嘛.在<组合数学>这本书就有这样的原本的证 ...

  4. [环境搭建] VS-Visual Studio-IIS Express 支持局域网訪问

    原创作品,转载请注明出处:http://blog.csdn.net/qiujuer/article/details/40350385 使用Visual Studio开发Web网页的时候有这种情况:想要 ...

  5. H2数据库入门使用

    H2数据库入门使用 学习了: https://www.cnblogs.com/xdp-gacl/p/4171024.html http://www.cnblogs.com/xdp-gacl/p/417 ...

  6. 超级简单JS网页倒计时代码

    <script type="text/javascript"> // JavaScript Document function ShowTimes(){ var Aft ...

  7. Hive分区表与分桶

    分区表 在Hive Select查询中.通常会扫描整个表内容,会消耗非常多时间做不是必需的工作. 分区表指的是在创建表时,指定partition的分区空间. 分区语法 create table tab ...

  8. iOS 手势识别器概述

    手势识别器 iOS 手势识别器(UIGestureRecognizer) 点击手势(UITapGestureRecognizer) 滑动手势(UISwipeGestureRecognizer) 旋转手 ...

  9. Mosquito的优化——epoll优化(七)

    本文由逍遥子撰写,转发请标注原址: http://blog.csdn.net/houjixin/article/details/46413583 或 http://houjixin.blog.163. ...

  10. oracle日常维护语句

    1.如何查看数据库的状态    unix下 ps -ef | grep ora windows下 看服务是否起来 是否可以连上数据库 SQL> select status, instance_r ...