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函数调用了多少次. 我们定义数组为所求 ...
随机推荐
- mysql linux查看配置文件my.cnf位置
原文:mysql linux查看配置文件my.cnf位置 命令: mysql --help | grep 'Default options' -A 1
- document.documentElement与body下clientHeight,scrollHeight等区别
本次说明仅在chrom环境下,ie等其他浏览器可能不同 1获取显示屏高度(pc和移动端,屏幕分辨率px) window.screen.height => 这个好理解,不多说. 2获取浏览器可视窗 ...
- 【codeforces 500E】New Year Domino
[题目链接]:http://codeforces.com/problemset/problem/500/E [题意] 有n个多米诺骨牌; 你知道它们的长度; 然后问你,如果把第i骨牌往后推倒,然后要求 ...
- b.WHERE使用中多行子查询(适用于in,any,all条件)
b.多行子查询(适用于in,any,all条件) //查询与SCOTT和MARTIN在同一个部门的同事的编号和名称 select empno,ename from emp where ...
- Ural 1004 Sightseeing Trip
Sightseeing Trip Time Limit: 2000ms Memory Limit: 16384KB This problem will be judged on Ural. Origi ...
- angular-基础
AngularJs特点: 1.依赖注入 2.模块化 3.双向绑定 4.语义化标签 当网页加载完毕,AngularJS 自动开启. ng-app 指令告诉 AngularJS,<div> 元 ...
- grep常见使用方法总结
grep -E 'l\{2,\}' 2.txt grep -E 'h(ell|a)o' test.txt grep '[a-z]\{5,\}' test.txt grep -xf a.txt b.tx ...
- 跟我学设计模式视频教程——适配器模式,适配器模式VS装饰模式
课程视频 适配器模式 适配器模式VS装饰模式 唠嗑 课程笔记 课程笔记 课程代码 课程代码 新课程火热报名中 课程介绍
- java大数类,两个不超过20位都不为0的十进制字符串相乘,华为笔试题
import java.math.BigInteger; import java.util.*; import java.io.*; public class Main { public static ...
- zzulioj--1799--wrz的压岁钱(贪心)
1799: wrz的压岁钱 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 407 Solved: 71 SubmitStatusWeb Boa ...