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. VS2010/MFC对话框:非模态对话框的创建及显示

    非模态对话框的创建及显示 上一节讲了模态对话框及其弹出过程,本节接着讲另一种对话框--非模态对话框的创建及显示. 已经说过,非模态对话框显示后,程序其他窗口仍能正常运行,可以响应用户输入,还可以相互切 ...

  2. javascript - C++, Qt, QtWebKit: How to create an html rendering window so that your application would get callbacks from JS calls? - Stack Overflow

    javascript - C++, Qt, QtWebKit: How to create an html rendering window so that your application woul ...

  3. Android多线程断点续传下载

    这个月接到一个项目.要写一个像360助手一样的对于软件管理的APP:当中.遇到了一个问题:多线程断点下载 这个 ,因为之前没有写过这方面的应用功能.所以.不免要自学了. 然后就在各个昂站上收索并整理了 ...

  4. 具体解释VMware 9.0.1安装MAC OS X 10.8(历时近3日感想篇)

    突然心血来潮,想用VMware 9.0.1安装MAC OS X,但网上的文章多多少少总有点缺陷,不能适合每个人,在综合了近30篇安装MAC OS X的文章后,我决定公布一篇比較大众化,比較详尽的MAC ...

  5. Phoenix——实现向HBase发送标准SQL语句

    写在前面一: 本文总结基于HBase的SQL查询系统--Salesforce phoenix 写在前面二: 环境说明: 一.什么是Phoenix 摘自官网: Phoenix是一个提供hbase的sql ...

  6. Swift调用Objective C的FrameWork

    很多Github的库经过很多年的发展,源码都是OC写的,,所以,用Swift调用OC的库就是开发中难免遇到的的一个问题,本文以AFNetworking为例,讲解如何跨语言调用. 第一步 创建一个空的工 ...

  7. javaweb学习路之三--websocket多人在线聊天

    在之前的项目基础上,加入了一个聊天室的功能,为了界面好看 引入了AmazeUI和umeditor最终效果图如下: 源码在 https://github.com/Zering/MyWeb 目前练习都在这 ...

  8. python字符串格式化符号含义及转义字符含义

    字符串格式化符号含义    符   号    说     明      %c    格式化字符及其ASCII码      %s    格式化字符串      %d    格式化整数      %o   ...

  9. React Native for android 项目驱动教程

    第一节 搭建开发环境 第二节 显示页面标题 第三节 实现页面布局 # React native是什么? React Native,是颠覆性的移动开发技术.它使用js开发,又是原生应用,不同于Hybri ...

  10. Enze fifth day(循环语句2)

    又是新的一周开始了,我还在云和学院继续学习.因为想要急切的想学会更多的知识,所以我有些急.可是我越急就越容易出错,这应该就是所谓的欲速则不达吧.这一周,我要重新把控好自己的一切,尽我最大的努力来学习! ...