A Very Simple Problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 589    Accepted Submission(s): 305

Problem Description
This is a very simple problem. Given three integers N, x, and M, your task is to calculate out the following value:

 
Input
There are several test cases. For each case, there is a line with three integers N, x, and M, where 1 ≤ N, M ≤ 2*10
9, and 1 ≤ x ≤ 50.

The input ends up with three negative numbers, which should not be processed as a case.

 
Output
For each test case, print a line with an integer indicating the result.
 
Sample Input
100 1 10000
3 4 1000
-1 -1 -1
 
Sample Output
5050
444
/*分析:
Sn=1^x * x^1 + 2^x * x^2 +...+ n^x * x^n;
Sn+1=1^x * x^1 + 2^x * x^2 +...+ n^x * x^n+(n+1)^x * x^(n+1)=Sn+(n+1)^x * x^(n+1),将(n+1)^x二项式展开然后用矩阵快速幂
构造矩阵:
|1 xC(x,0) xC(x,1) xC(x,2) ... xC(x,x)| |Sn | |S(n+1) |
|0 xC(0,0) 0 0 ... 0 | |x^n * n^0| |x^(n+1) * (n+1)^0|
|0 xC(1,0) xC(1,1) 0 ... 0 | *|x^n * n^1|=|x^(n+1) * (n+1)^1|
|0 xC(2,0) xC(2,1) xC(2,2) ... 0 | |x^n * n^2| |x^(n+1) * (n+1)^2|
|... | |... | |... |
|0 xC(x,0) xC(x,1) xC(x,2) ... xC(x,x)| |x^n * n^x| |x^(n+1) * (n+1)^x|
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std; const int MAX=50+10;
__int64 array[MAX][MAX],sum[MAX][MAX],mod; __int64 C(int n,int m){
if(m<0 || m>n)return 0;
__int64 ans=1;
for(int i=1;i<=m;++i){
ans=ans*(n-m+i)/i;
}
return ans%mod;
} void MatrixInit(__int64 a[MAX][MAX],int &x,bool flag){
a[0][0]=1;
for(int j=1;j<=x+1;++j){
if(flag)a[0][j]=x*C(x,j-1)%mod;
else a[0][j]=0;
}
for(int i=1;i<=x+1;++i){
for(int j=0;j<=x+1;++j){
if(flag)a[i][j]=x*C(i-1,j-1)%mod;
else a[i][j]=(i == j);
}
}
} void MatrixMult(__int64 a[MAX][MAX],__int64 b[MAX][MAX],int &x){
__int64 c[MAX][MAX]={0};
for(int i=0;i<=x+1;++i){
for(int j=0;j<=x+1;++j){
for(int k=0;k<=x+1;++k){
c[i][j]=(c[i][j]+a[i][k]*b[k][j])%mod;
}
}
}
for(int i=0;i<=x+1;++i){
for(int j=0;j<=x+1;++j)a[i][j]=c[i][j];
}
} __int64 MatrixPow(int &x,int &k){
MatrixInit(sum,x,0);
while(k){
if(k&1)MatrixMult(sum,array,x);
MatrixMult(array,array,x);
k>>=1;
}
return sum[0][1];
} int main(){
int n,x;
while(scanf("%d%d%I64d",&n,&x,&mod),n>0){
MatrixInit(array,x,1);
printf("%I64d\n",MatrixPow(x,n));
}
return 0;
}

hdu3483之二项式展开+矩阵快速幂的更多相关文章

  1. hdu3483 A Very Simple Problem 非线性递推方程2 矩阵快速幂

    题目传送门 题目描述:给出n,x,mod.求s[n]. s[n]=s[n-1]+(x^n)*(n^x)%mod; 思路:这道题是hdu5950的进阶版.大家可以看这篇博客hdu5950题解. 由于n很 ...

  2. HDU - 5950 Recursive sequence(二项式+矩阵合并+矩阵快速幂)

    Recursive sequence Farmer John likes to play mathematics games with his N cows. Recently, they are a ...

  3. 广工十四届校赛 count 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6470 题意:求,直接矩阵快速幂得f(n)即可 构造矩阵如下: n^3是肯定得变换的,用二项式展开来一点 ...

  4. 【BZOJ3328】PYXFIB(单位根反演,矩阵快速幂)

    [BZOJ3328]PYXFIB(单位根反演,矩阵快速幂) 题面 BZOJ 题解 首先要求的式子是:\(\displaystyle \sum_{i=0}^n [k|i]{n\choose i}f_i\ ...

  5. 一些特殊的矩阵快速幂 hdu5950 hdu3369 hdu 3483

    思想启发来自, 罗博士的根据递推公式构造系数矩阵用于快速幂 对于矩阵乘法和矩阵快速幂就不多重复了,网上很多博客都有讲解.主要来学习一下系数矩阵的构造 一开始,最一般的矩阵快速幂,要斐波那契数列Fn=F ...

  6. HDU4686——Arc of Dream矩阵快速幂

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4686 题目大意: 已知a0=A0, ai=Ax*ai-1+Ay; b0=B0, bi=Bx*bi-1 ...

  7. 【BZOJ5298】[CQOI2018]交错序列(动态规划,矩阵快速幂)

    [BZOJ5298][CQOI2018]交错序列(动态规划,矩阵快速幂) 题面 BZOJ 洛谷 题解 考虑由\(x\)个\(1\)和\(y\)个\(0\)组成的合法串的个数. 显然就是把\(1\)当做 ...

  8. HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)

    题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...

  9. BZOJ.4180.字符串计数(后缀自动机 二分 矩阵快速幂/倍增Floyd)

    题目链接 先考虑 假设S确定,使构造S操作次数最小的方案应是:对T建SAM,S在SAM上匹配,如果有S的转移就转移,否则操作数++,回到根节点继续匹配S.即每次操作一定是一次极大匹配. 简单证明:假设 ...

随机推荐

  1. CreateFile,ReadFile等API详解(或者说MSDN的翻译)

    一.*****CreateFile***** 这个函数可以创建或打开一个对象的句柄,凭借此句柄就可以控制这些对象:控制台对象.通信资源对象.目录对象(只能打开).磁盘设备对象.文件对象.邮槽对象.管道 ...

  2. MaxSubArray 最大子数列和

    public int maxSubArray(int[] A) { int newsum=A[0]; int max=A[0]; for(int i=1;i<A.length;i++){ new ...

  3. asp.net Page_Load事件加载两次

    Page_Load 即使加上 if(!IsPostBack){ ……} 还时走了2次 这时候 或者看看你的程序和脚本,是不是刷新页面了 或者页面的样式有错误的地方  例如: background:ur ...

  4. Objective-c 类的继承 方法重写 方法重载

    一.类的继承 Objective-c中类的继承与C++类似,不同的是Objective-c不支持多重继承,一个类只能有一个父类,单继承使Objective-c的继承关系很简单,易于管理程序. Obje ...

  5. Sql缓存依赖--数据库缓存

    •依赖于文件内容CacheDependency cDep = new CacheDependency(filePath); •依赖于数据库内容(轮询机制/通知机制)一:轮询机制 1.在数据库新建版本表 ...

  6. ASP.NET页面传值方式

    http://www.cnblogs.com/zhangkai2237/archive/2012/05/06/2486462.html http://www.cnblogs.com/xiaoyusmi ...

  7. C#中接口和方法的运用(Fourteenth Day)

    由于周五我有一些事情没来得及总结当天的知识,所以在今天总结一下周五在云和学院所学到的有关接口和方法的知识. 理论: 接口: •接口的定义:interface关键字,接口中可以有属性.方法(未实现) • ...

  8. [译]Stairway to Integration Services Level 18 – 部署和执行

    介绍 在本文中,我们要创建一个SSIS Catalog 实例,部署我们的项目,并且运行 weather data loader 包. SSIS 2012 部署模型   SSIS 2012 Deploy ...

  9. centos 5.8 x64Jetty的安装以及项目部署配置

    链接地址:http://blog.csdn.net/shuixin536/article/details/9049821 安装环境 centos 5.8 x64 安装前须知 首先在安装Jetty之前要 ...

  10. json在PHP中应用技巧

    一.json_encode() 该函数主要用来将数组和对象,转换为json格式.先看一个数组转换的例子: $arr = array ('a'=>1,'b'=>2,'c'=>3,'d' ...