题目大意:求(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. django-7-django模型系统

    <<<常用的模型字段类型>>>https://docs.djangoproject.com/en/2.1/ref/models/fields/#field-type ...

  2. NOIP2018提高组金牌训练营——数论专题

    地址 https://www.51nod.com/live/liveDescription.html#!liveId=23 1187 寻找分数 给出 a,b,c,d, 找一个分数p/q,使得a/b & ...

  3. POJ 1942

    开始时竟然用了分情况讨论. 仔细思考一下,哈哈,发现不过是多重集合的组合数而已. #include <iostream> #include <cstdio> #include ...

  4. JDK1.7中的ThreadPoolExecutor源代码剖析

    JDK1. 7中的ThreadPoolExecutor 线程池,顾名思义一个线程的池子,池子里存放了非常多能够复用的线程,假设不用线程池相似的容器,每当我们须要创建新的线程时都须要去new Threa ...

  5. GNU Linux中的SO_RCVLOWAT和SO_SNDLOWAT说明

    /*********************************************************************  * Author  : Samson  * Date   ...

  6. oracle刚開始学习的人经常使用操作100问

    oracle刚開始学习的人经常使用操作100问 1. Oracle安装完毕后的初始口令?   internal/oracle sys/change_on_install system/manager ...

  7. linux下线程

    linux下线程 线程与进程的关系: 之前转载的微信文章,进程与线程的差别已经说得比較清楚了.能够查看之前转载的文章.linux进程与线程的差别. 创建一个线程: #include<pthrea ...

  8. UVA - 1476 Error Curves 三分

                                           Error Curves Josephina is a clever girl and addicted to Machi ...

  9. Elasticsearch yellow 意味着主分片可用,副本不可用

    摘自:http://unasm.com/2016/11/644/ 在通过 /_cluster/state 命令查看es 状态的时候,发现es 处于一个yellow的状态, 这个很奇怪,按照官方的解释, ...

  10. spring boot多数据源配置示例

    1. application.properties #\u4E3B\u5E93\u914D\u7F6E spring.datasource.primary.url=jdbc:mysql://mysql ...