题目链接:http://lightoj.com/volume_showproblem.php?problem=1132

题意:

  给定n、k,求(1K + 2K + 3K + ... + NK) % 232

题解:

  设sum(i) = 1K + 2K + 3K + ... + iK

  所以要从sum(1)一直推到sum(n)。

  所以要找出sum(i)和sum(i+1)之间的关系:

  

  

  

  好了可以造矩阵了。

  (n = 6时)

  矩阵表示(大小为 1 * (k+2)):

  

  

  初始矩阵start:

  

  也就是:

  

  

  特殊矩阵special:

  

AC Code:

#include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX_L 60
#define MAX_K 55 using namespace std; struct Mat
{
int n;
int m;
unsigned val[MAX_L][MAX_L];
Mat()
{
n=;
m=;
memset(val,,sizeof(val));
}
void print_mat()
{
cout<<"--------"<<endl;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cout<<val[i][j]<<" ";
}
cout<<endl;
}
cout<<"--------"<<endl;
}
}; int k,t;
long long n;
unsigned c[MAX_K][MAX_K]; void cal_combination()
{
memset(c,,sizeof(c));
c[][]=;
for(int i=;i<MAX_K;i++)
{
c[i][]=;
for(int j=;j<=i;j++)
{
c[i][j]=c[i-][j]+c[i-][j-];
}
}
} Mat make_unit(int n)
{
Mat mat;
mat.n=n;
mat.m=n;
for(int i=;i<n;i++)
{
mat.val[i][i]=;
}
return mat;
} Mat make_start(int k)
{
Mat mat;
mat.n=;
mat.m=k+;
for(int i=;i<k+;i++)
{
mat.val[][i]=;
}
return mat;
} Mat make_special(int k)
{
Mat mat;
mat.n=k+;
mat.m=k+;
for(int j=;j<k+;j++)
{
for(int i=j;i<k+;i++)
{
mat.val[i][j]=c[k-j+][i-j];
}
}
for(int i=;i<k+;i++)
{
mat.val[i][]=mat.val[i][];
}
mat.val[][]=;
return mat;
} Mat mul_mat(const Mat &a,const Mat &b)
{
Mat c;
if(a.m!=b.n)
{
cout<<"Error: mul_mat"<<endl;
return c;
}
c.n=a.n;
c.m=b.m;
for(int i=;i<a.n;i++)
{
for(int j=;j<b.m;j++)
{
for(int k=;k<a.m;k++)
{
c.val[i][j]+=a.val[i][k]*b.val[k][j];
}
}
}
return c;
} Mat quick_pow_mat(Mat mat,long long k)
{
Mat ans;
if(mat.n!=mat.m)
{
cout<<"Error: quick_pow_mat"<<endl;
return ans;
}
ans=make_unit(mat.n);
while(k)
{
if(k&)
{
ans=mul_mat(ans,mat);
}
mat=mul_mat(mat,mat);
k>>=;
}
return ans;
} int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
cal_combination();
cin>>t;
for(int cas=;cas<=t;cas++)
{
cin>>n>>k;
Mat start=make_start(k);
Mat special=make_special(k);
Mat ans=mul_mat(start,quick_pow_mat(special,n-));
cout<<"Case "<<cas<<": "<<ans.val[][]<<endl;
}
}

LightOJ 1132 Summing up Powers:矩阵快速幂 + 二项式定理的更多相关文章

  1. LightOJ - 1132 Summing up Powers 矩阵高速幂

    题目大意:求(1^K + 2^K + 3K + - + N^K) % 2^32 解题思路: 借用别人的图 能够先打表,求出Cnm,用杨辉三角能够高速得到 #include<cstdio> ...

  2. LightOj 1065 - Number Sequence (矩阵快速幂,简单)

    题目 和 LightOj 1096 - nth Term 差不多的题目和解法,这道相对更简单些,万幸,这道比赛时没把模版给抽风坏. #include<stdio.h> #include&l ...

  3. LightOJ 1070 Algebraic Problem:矩阵快速幂 + 数学推导

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1070 题意: 给你a+b和ab的值,给定一个n,让你求a^n + b^n的值(MOD ...

  4. LightOj 1096 - nth Term (矩阵快速幂,简单)

    题目 这道题是很简单的矩阵快速幂,可惜,在队内比赛时我不知什么时候抽风把模版中二分时判断的 ==1改成了==0 ,明明觉得自己想得没错,却一直过不了案例,唉,苦逼的比赛状态真让人抓狂!!! #incl ...

  5. LightOJ 1268 Unlucky Strings (KMP+矩阵快速幂)

    题意:给出一个字符集和一个字符串和正整数n,问由给定字符集组成的所有长度为n的串中不以给定字符串为连续子串的有多少个? 析:n 实在是太大了,如果小的话,就可以用动态规划做了,所以只能用矩阵快速幂来做 ...

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

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

  7. LightOJ 1070 - Algebraic Problem 推导+矩阵快速幂

    http://www.lightoj.com/volume_showproblem.php?problem=1070 思路:\({(a+b)}^n =(a+b){(a+b)}^{n-1} \) \(( ...

  8. LightOJ 1244 - Tiles 猜递推+矩阵快速幂

    http://www.lightoj.com/volume_showproblem.php?problem=1244 题意:给出六种积木,不能旋转,翻转,问填充2XN的格子有几种方法.\(N < ...

  9. lightoj 1096【矩阵快速幂(作为以后的模板)】

    基础矩阵快速幂何必看题解 #include <bits/stdc++.h> using namespace std; /* 0 1 2 3 4 5 6 7 0 0 0 */ const i ...

随机推荐

  1. vue2.0 仿手机新闻站(四)axios

    1.axios的配置 main.js import Vue from 'vue' import App from './App.vue' // 引入 路由 import VueRouter from ...

  2. 移动端去掉按钮button默认样式

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  3. poj 1328 Radar Installation 【贪心】【区间选点问题】

    Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54798   Accepted: 12 ...

  4. Oracle 中session和processes的初始设置

    http://blog.163.com/succu/blog/static/193917174201252911727149/ 1.sessions   在初始化参数所设定的限制中,最为人所知的估计就 ...

  5. 监听iOS检测屏幕旋转状态,不需开启屏幕旋转

    -(void)rotation_icon:(float)n { UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>] ...

  6. 封装GetQueryString()方法来获取URL的value值

    首先测试URL:http://192.168.1.82:8020/juzhong/daojishi.html?name=xiangruding&sex=nuuu&age=90 代码如下 ...

  7. 讲真,你是因为什么才买华为P20系列手机!

    华为P20系列手机上市两个半月发货600万台!600万台?!看到这个亮瞎我钛合金狗眼的数据,且容我掰着手指脚趾算一下,算了,还是容我毫不夸张的感叹一句吧:华为做手机不用桨,不需风,全靠“浪”……. 两 ...

  8. Linux下实现RAID

    一.实验目的 1.掌握Linux系统下软RAID的实现方法: 2.掌握RAID5的配置过程: 3. 通过实验熟悉RAID.5的特点. 二.实验内容及步骤 1.在VMware中创建一台Linux. 2. ...

  9. 一个用消息队列 的人,不知道为啥用 MQ,这就有点尴尬

    消息队列 为什么写这篇文章? 博主有两位朋友分别是小A和小B: 小A,工作于传统软件行业(某社保局的软件外包公司),每天工作内容就是和产品聊聊需求,改改业务逻辑.再不然就是和运营聊聊天,写几个SQL, ...

  10. CPI

    CPI (Consumer Price Index 物价指数) 是政府用来衡量通货膨胀的其中一个数据.通俗的讲,CPI就是市场上的货物价格增长百分比.一般市场经济国家认为CPI在2-3%属于可接受范围 ...